Collision Point


Ive done mouse picking work correctly, however something is missing. I want to make a simple box appear where collision has take part. It seems simple but it doesnt work !

The collision results are returned fine !

[java]if (results.size() > 0) {

// The closest result is the target that the player picked:

Geometry target = results.getClosestCollision().getGeometry();

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

this.clickedObject = target;

this.clickedPoint = point;

clicked = true;

System.out.println("FINAL: "+target.getName());


So, clickedObject is getting the right value, however clickedPoint doesnt ! It creates a box ‘near’ where ive clicked but not where.

This is the collision ray test:

[java] CollisionResults results = new CollisionResults();

// Convert screen click to 3d position

Vector2f click2d = Tosko.getHanndler().getInputManager().getCursorPosition();//new Vector2f(mouseX, mouseY);

Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();

Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d);

// Aim the ray from the clicked spot forwards.

Ray ray = new Ray(click3d, dir);

// Collect intersections between ray and all nodes in results list.

scene.collideWith(ray, results);[/java]

Would apreciate any help ! Thanks alot for your attention !

normalize your direction

[java]Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalize();[/java]

same thing :frowning:

thanks for the attention !

lol XD

what ? :frowning: Dont troll me

That is not the cam direction its the inverted vector. Subtract the cam position from the constructed point :roll:

1 Like

thanks for the attention normen, but im sorry i didnt got it :frowning:

you subtract the constructed point from the cam position, you get the vector thats “the other way round”

1 Like

Im sorry i feel an complete idiot lol

im so bad at math i cant get this

[java]Vector3f dir = click3d.subtract(cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f)).normalize();[/java] ?

Confused :X

Thanks for your attention, and specially, your patience.

you…your house

xyou=0 xhouse=10

To go from you to your house is the distance xyou-xhouse=-10 or xhouse-xyou=10?

1 Like

i understand that

ive tryed making it simpler, but still didnt work

[java] Vector3f pos = cam.getWorldCoordinates(new Vector2f(mouseX, mouseY), 0f).clone();

Vector3f dir = cam.getWorldCoordinates(new Vector2f(mouseX, mouseY), 1f).clone();




scene.collideWith(ray, results);[/java]

What amazes me is that i can work with geometryes correctly (i know witch geometry has been clicked), the only part amazes me is that the position of the click is not correct !

[java] Vector3f point = results.getClosestCollision().getContactPoint();[/java]

Thanks for the attention and patience.

Try breakpointing and/or printing out that collision location and matching it against what you know of the scene.

If the number makes sense then you know the problem is later on in the chain, keep working down checking the values at each step until you see something go unexpected. If the number doesn’t make sense already then things get more complicated but at least you’ve narrowed down the search area for the problem.

Hard time testing, but something intresting

this was caused because i rotated the rootnode, wtf i removed the rotation and it was done… and i still cant understand why

Why would you rotate the rootNode?

cause im a noob, guess theres no other explanation

made another node attached to the root node to do the trick

So, when you create the box are you setting it’s location to “point”?