Aligning a Plane to the Terrain

Aligning a Plane to the Terrain##

So. I am a jMonkeyEngine newbie and I have a decent program built up over the last week, and I just added ray casting. Upon contact with an object, it places a Sphere. I want it to place a textured quad onto whatever it hits and make the quad align with the face of the object it hit, like so:

I basically need a function to get the angle of the face of the object the ray hits, and rotate the plane to that angle.
Thanks in advance.

- WickedJet

Angles are almost always the wrong approach for stuff like this.

http://javadoc.jmonkeyengine.org/com/jme3/math/Quaternion.html#lookAt(com.jme3.math.Vector3f,%20com.jme3.math.Vector3f)

What would you recommend I do this with then?
I’m using this to create a bullet hole texture on an object that i shoot, so there is probably a better way of doing this…

(Thanks for the speedy reply!)

Well, I mean angles are usually wrong because inevitably you turn that angle back into something else. The angle was almost never necessary in the first place as it’s generally possible to go from what you have to what you want.

In this case of the originally stated (non) solution, you’d use a Quaternion to rotate the quad so you need to know what quaternion. lookAt() is a good start for that.

But what it sounds like you really want is projective texturing and that’s a whole other subject. I know some users posted some implementation a long time ago but I don’t remember who are anything like that.

The quad approach will have problems because it’s flat and your sphere is round.

Sorry if I wasn’t clear in the OP. I mean to say that i want to replace the Sphere with an image of a bullet hole, and the only method of doing that my dumb mind could come up with is creating a plane. But this projective texture mapping thing seems promising, so it’s time to do some research! Thanks for your help!

Could you do something like this?

Geometry bulletTexture;
CollisionResults cr = getCollision(rootNode);
Vector3f normal = cr.getClosestCollision().getContactNormal();
bulletTexture.setLocalTranslation(cr.getClosestCollision().getContactPoint());
bulletTexture.lookAt(bulletTexture.getLocalTranslation().add(normal), Vector3f.UNIT_Y);

“decals” is the term you are looking for. I don’t know if jME has it out of the box.
“projective textures” or “projective texturing” might be used to create decals - I don’t really know how they are made. You could also use CSG to extract the surface and then push the geometry upwards a little bit - don’t know if this would actually work with the current CSG implementation.