Hi guys,
I am trying finding an object in the scene using ray and collusion. What I want to do is to find the world location of the object that the ray collides, That is, I dont want the collisionPoint but the world coordinates of the center of the Geometry.
I understand that collideWith() method requires the ray in worldCoordinates and return the collisionPoint in world coordinates. Here is the catch, the Geometry I am trying to collide with is attached to a node which has its on world transform (rotation and translation). I believe that is creating the problem.
I have tried
targetPoint = results.getClosestCollision().getGeometry().getWorldTranslation()
targetPoint = getWorldTransform().transformVector(results.getClosestCollision().getGeometry().getLocalTranslation(),null);
and many others.
BTW is there a class that sweeps an area/volume and finds collision objects. I have checked SweepSphere class but it does not seem to collide with a Node, i.e. all the geometries in a Node.
Any help appreciated
You are not supposed to get().set() any of the location or rotation values. use setLocalTranslation(vector) and setLocalTranslation(vector).
I am not setting any values. I am getting them. My problem is following two calls below return coordinates far from each other (in my application the collision point is very close to the center of the colliding geometry)
results.getClosestCollision().getGeometry().getWorldTranslation()
results.getClosestCollision().getContactPoint()
Here “results” is the result of collision of a Node with a Ray.
My second questions is “is there a class that sweeps an area/volume and finds collision objects. I have checked SweepSphere class but it does not seem to collide with a Node, i.e. all the geometries in a Node.”
I found the problem. I guess this should be added somewhere in the tutorials.
When a Geometry is created for example using a Box you can set the coordinate of the Box or the Geometry. either case the Geometry will be rendered where it should be. However, if you set the location of the box, it does not update the location of the Geometry and thus getting the local translation of the geometry will always return (0,0)!!!.
In order to avoid confusion create the Box at (0,0) and change the location of the Geometry. I recommend creating geometries as the following code;
[java]public static Geometry createBox(ColorRGBA color, AssetManager assetManager, Vector3f location, Vector3f size) {
//WARNING: Box is located at (0,0)
Box b = new Box(Vector3f.ZERO, size.x, size.y, size.z);
Geometry geom = new Geometry("Box", b);
//// WARNING: if you set the location of the box instead of Geometry then it will appear in the
//place it is supposed to be but the location of the geom cannot be used form outside.
geom.setLocalTranslation(location);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", color);
geom.setMaterial(mat);
return geom;
}[/java]
Uh, why would one expect the geometry move when you move the mesh? Its like expecting a parent node to move when you move a child…
You are right for those who understand the inner workings. On the other hand, you can understand that mesh is not introduced as a node or geometry in the tutorials. So it is not easy to see that mesh is a child of geometry as Spatials are such that they have their own local translation with respect to the parent Geometry. That is why I said it might be put in the tutorials as notification.
All being said, I am a new bee and this might not apply to others. Yet I can see some questions that was the consequence of this misconception, such as camera rotations (ie lookAt) etc. You are free to use this stance as you wish.
Still my second question prevails. I would appreciate if you can tell me "is there a class that sweeps an area/volume and finds collision objects. I have checked SweepSphere class but it does not seem to collide with a Node, i.e. all the geometries in a Node.”
Another point, I am using Nodes as logical separation of concepts in scene graph. That leads to lots of translations and rotations, which are not covered in the tutorials understandably. In my case debugging the results using mathematics and looking at the rendered scene is really confusing, again this is my view. So adding a small notification can help others to understand the inner workings hence debugging their application.