Transform.equals? or isIdentity() ? or how to check if not transformed ? jme3 r7249

How to check if a certain Node is “transformed” or not?

Like this won’t work for different but equal-contents instances of Transform (ie. cloned)

pre type="java"
subNode.getLocalTransform().equals(Transform.IDENTITY)
/pre

Do we(meaning you xD) need to implement Transform.equals ? or at least Transform.isIdentity() ? (can’t remember where I saw this method already)

Example code, press Space to set transform to Transform.IDENTITY and then pressing Space again to transform it to what it was by checking if it’s already Transform.IDENTITY will fail:

http://i.imgur.com/H4YLJ.png

pre type="java"
package org.jme3.tests;





import com.jme3.app.SimpleApplication;


import com.jme3.collision.CollisionResult;


import com.jme3.collision.CollisionResults;


import com.jme3.font.BitmapText;


import com.jme3.input.KeyInput;


import com.jme3.input.controls.ActionListener;


import com.jme3.input.controls.KeyTrigger;


import com.jme3.material.Material;


import com.jme3.math.ColorRGBA;


import com.jme3.math.FastMath;


import com.jme3.math.Ray;


import com.jme3.math.Transform;


import com.jme3.math.Vector3f;


import com.jme3.scene.Geometry;


import com.jme3.scene.Mesh;


import com.jme3.scene.Node;


import com.jme3.scene.debug.Arrow;


import com.jme3.scene.shape.Box;


import com.jme3.scene.shape.Line;


import com.jme3.system.AppSettings;





public class TranslationWickedtry extends SimpleApplication {


private Node subNode;


private Node nodeLine;


private final static String mapToggleSubNodeTransform = “mapToggleSubNodeTransform”;


private static final float lineWidth = 5f;


private BitmapText helloText;





public static void main(String[] args) {


TranslationWickedtry app = new TranslationWickedtry();


AppSettings aps = new AppSettings(true);


// aps.load(aps.getTitle());


aps.setVSync(true);


app.setShowSettings(false);


app.setSettings(aps);


app.start();


}





@Override


public void simpleInitApp() {


flyCam.setDragToRotate(true);


flyCam.setMoveSpeed(20f);





nodeLine = new Node();


rootNode.attachChild(nodeLine);


/** create four colored boxes and a floor to shoot at: */


subNode = new Node(“Shootables”);


rootNode.attachChild(subNode);


Box meshBox = new Box(new Vector3f(-2, 0, 1), 1, 1, 1);


Geometry geoBox = new Geometry(“Boxy”, meshBox);


Material matBox = new Material(assetManager,


“Common/MatDefs/Misc/WireColor.j3md”);


matBox.setColor(“Color”, ColorRGBA.Orange);


geoBox.setMaterial(matBox);


subNode.attachChild(geoBox);


transformSubNode();





Node coord = new Node();


attachCoordinateAxes(Vector3f.ZERO, coord);


rootNode.attachChild(coord);





inputManager.addMapping(mapToggleSubNodeTransform, new KeyTrigger(


KeyInput.KEY_SPACE));


inputManager.addListener(actionListener, mapToggleSubNodeTransform);





// ========


helloText = new BitmapText(guiFont, false);


helloText.setSize(guiFont.getCharSet().getRenderedSize());


helloText.setLocalTranslation(300, helloText.getLineHeight(), 0);


helloText.setText(“press SPACE to toggle subNode’s transform”);


guiNode.attachChild(helloText);


}





private void transformSubNode() {





subNode.setLocalTranslation(11, 17, -29);


subNode.scale(2.3f, 4.2f, 7.1f);


subNode.rotate(FastMath.DEG_TO_RAD * 35f, FastMath.DEG_TO_RAD * 35f,


FastMath.DEG_TO_RAD * 35f);


}





private final ActionListener actionListener = new ActionListener() {





@SuppressWarnings(“synthetic-access”)


@Override


public void onAction(String name, boolean isPressed, float tpf) {


do {


if ((mapToggleSubNodeTransform == name) && (isPressed)) {


// FIXME: implement Transform.equals() or .isIdentity()?


if (subNode.getLocalTransform().equals(Transform.IDENTITY)) {


// if


// (subNode.getLocalTransform().getTranslation().getX()


// == 0) {


helloText.setText(“subNode is transformed”);


transformSubNode();


} else {


helloText.setText(“subNode is NOT transformed”);


subNode.setLocalTransform(Transform.IDENTITY);


}


break;


}


} while (false);


}


};





@Override


public void simpleUpdate(float tpf) {


Vector3f origin = cam.getWorldCoordinates(


inputManager.getCursorPosition(), 0.0f);


Vector3f direction = cam.getWorldCoordinates(


inputManager.getCursorPosition(), 0.3f);





direction.subtractLocal(origin).normalizeLocal();





Ray ray = new Ray(origin, direction);


CollisionResults results = new CollisionResults();


subNode.collideWith(ray, results);


if (results.size() > 0) {


CollisionResult closest = results.getClosestCollision();


// world coord of the contact


Vector3f contact = closest.getContactPoint().clone();


// the direction of the normal, normalized already:


Vector3f normalSdirection = closest.getContactNormal();


if (!normalSdirection.isUnitVector()) {


// was not normalized?! impossible


throw null;


}


// normalVec is a point on the normal to the surface


Vector3f normalVec = contact.add(normalSdirection.mult(4f));


// the vector perpendicular on both contact and normal vectors


// Vector3f upVec = contact.cross(normalVec).normalizeLocal();


Line line = new Line(contact, normalVec);


Geometry geoLine = new Geometry(“line1”, line);


Material mark_mat = new Material(assetManager,


“Common/MatDefs/Misc/SolidColor.j3md”);


mark_mat.setColor(“Color”, ColorRGBA.randomColor());


geoLine.setMaterial(mark_mat);


nodeLine.attachChild(geoLine);


// rootNode.attachChild(geoLine);


}


}





private void attachCoordinateAxes(Vector3f pos, Node toNode) {


Arrow arrow = new Arrow(Vector3f.UNIT_X);


// make arrow thicker,


arrow.setLineWidth(lineWidth);


putShape(arrow, ColorRGBA.Red, toNode).setLocalTranslation(pos);





arrow = new Arrow(Vector3f.UNIT_Y);


arrow.setLineWidth(lineWidth); // make arrow thicker


putShape(arrow, ColorRGBA.Green, toNode).setLocalTranslation(pos);





arrow = new Arrow(Vector3f.UNIT_Z);


arrow.setLineWidth(lineWidth); // make arrow thicker


putShape(arrow, ColorRGBA.Blue, toNode).setLocalTranslation(pos);


}





private Geometry putShape(Mesh shape, ColorRGBA color, Node onNode) {


Geometry g = new Geometry(“coordinate axis”, shape);


Material mat = new Material(assetManager,


“Common/MatDefs/Misc/Unshaded.j3md”);


mat.getAdditionalRenderState().setWireframe(true);


mat.setColor(“Color”, color);


g.setMaterial(mat);


onNode.attachChild(g);


// g.setCullHint(CullHint.Inherit);


return g;


}





}



/pre

maybe we could implement this method yeah

but as a workaround you can compare localTranslation, localRotation and localScale, they all have an equal method overridden.

1 Like