How to make character yaw face mouse pointer ray intersect

Hey guys, I’ve gotten really close, but my code is obviously a little off. I’m trying to make the model face where the mouse intersects the ground, but only for the Y axis (yaw).

Here is my code so far:

[java]public void onAnalog(String name, float keyPressed, float tpf) {

Vector3f origin = cam.getWorldCoordinates(BaseGame.getInputManager().getCursorPosition(), 0.0f);

Vector3f direction = cam.getWorldCoordinates(BaseGame.getInputManager().getCursorPosition(), 0.3f);


CollisionResults results = new CollisionResults();

rootNode.collideWith(new Ray(origin, direction), results);

if (results.size() > 0) {

Vector3f p1 = results.getClosestCollision().getContactPoint().normalizeLocal();

Vector3f p2 = model.getLocalTranslation().clone().normalizeLocal();

float angle = p1.angleBetween(p2);




Thank you guys, I’m really liking jme3.

Solution if any one else has this question:


Vector3f p1 = results.getClosestCollision().getContactPoint();

p1 = p1.subtract(model.getLocalTranslation());

p1.y = 0;

p1 = p1.normalizeLocal().mult(2f);

Quaternion q1 = model.getLocalRotation();

q1.lookAt(p1, Vector3f.UNIT_Y);



1 Like

Looks good. I hope you check that the results weren’t empty before you go into the code above though as otherwise it’s open to crashing at some random point in the future.