[solved] Problem with Ray in TPP game

Hi. Is there a possible in jme to get a coordinates of point on which my spatial looking at the moment? Cause I use lookAt to change position of Spatial, but also I need to remember a previous position.

It looks at a whole ray of points. The ray starts at the spatial world position and goes into this direction:

Vector3f dir = spatial.getWorldRotation().mult(Vector3f.UNIT_Z);

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

@normen said:
The ray starts at the spatial world position and goes into this direction:


So maybe I should be more specific. I need vector of this direction.
@caro said:
So maybe I should be more specific. I need vector of this direction.

Thats why I wrote the code to get it :roll:. Link to tutorial = brain off, sigh.
@normen said:
Thats why I wrote the code to get it :roll:. Link to tutorial = brain off, sigh.


What normen wanted to say is: this is already the code to get the direction the spatial "looks at":

@normen said:
Vector3f dir = spatial.getWorldRotation().mult(Vector3f.UNIT_Z);

Ok, I have another problem, cause I change a little my conception (I suppose I can use this topic cause problem is really similar than previous). I want to check collision between hero and NPC, cause I want to make some interaction with NPC. So I used the example from Hello Picking tutorial, and for my case I change code a little (cause my game is TPP and camera is for example like Silent Hill 2 and I don’t want to make a dative or something like that).

[java]CollisionResults results = new CollisionResults();

Vector3f position = new Vector3f(hero.getWorldTranslation().x + 1, hero.getWorldTranslation().y + 1, hero.getWorldTranslation().z + 1);

Ray ray = new Ray(position, new Vector3f(0f, 0f, 1f));

node.collideWith(ray, results);

System.out.println(results.size());[/java]

But when I write in that way program doesn’t detect any collision (results.size() is 0). I tried lots of combination of origin and direction in Ray constructor but without success. Any idea?

Have you tryed:

Ray ray = new Ray(position, hero.getWorldRotation().mult(Vector3f(0f, 1f, 0f)));

Unfortunately it also doesn’t work :frowning: result.size() always is 0. When I changed position vector into hero.getWorldTranslation() I have a few object but this works strange (sometimes 4, sometimes 6) and it looks like hero has collision with himself.

@caro said:
Unfortunately it also doesn't work :( result.size() always is 0. When I changed position vector into hero.getWorldTranslation() I have a few object but this works strange (sometimes 4, sometimes 6) and it looks like hero has collision with himself.


Why don't inspect every collision result? Check if the collision result is not the hero,
The nearest collision which is not the hero should be the collision you are looking after

Ok, my fault because I use node.collideWith(ray, results); only on my hero node. Now it works on NPC node:

Ray ray = new Ray(cam.getLocation(), cam.getDirection());

But still when I used my solution (for example like zzueqq said: Ray ray = new Ray(position, hero.getWorldRotation().mult(Vector3f(0f, 1f, 0f))):wink: doesn’t work and I don’t have any idea why.



I show you how I think about this situation, maybe then you can help me.

Here is code:

[java]CollisionResults results = new CollisionResults();

Ray ray = new Ray(hero.getWorldTranslation(), new Vector3f(0, 0, 1));

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



And image, how I imagine this situation.

http://imageshack.us/a/img513/8200/gameoc.png

Also keep in mind that if your ray is being cast from the center of your hero… it will collide with any geometries there as well. You’ll need to account for this. Just an FYI

@caro said:
Ok, my fault because I use node.collideWith(ray, results); only on my hero node. Now it works on NPC node:
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
But still when I used my solution (for example like zzueqq said: Ray ray = new Ray(position, hero.getWorldRotation().mult(Vector3f(0f, 1f, 0f)));) doesn't work and I don't have any idea why.

I show you how I think about this situation, maybe then you can help me.
Here is code:
[java]CollisionResults results = new CollisionResults();
Ray ray = new Ray(hero.getWorldTranslation(), new Vector3f(0, 0, 1));
NPC.collideWith(ray, results);[/java]

And image, how I imagine this situation.
http://imageshack.us/a/img513/8200/gameoc.png


You could substitue the ray with a actual geometry, maybe a line, or a debug arrow, set it's location and rotation the same way as you would cast your ray, in that way, you could at least debug whats going wrong
2 Likes

Thanks, I found a solution when I’m looking for how to draw a line :smiley: I found a tutorial on youtube which helped me. Here is it:

http://www.youtube.com/watch?v=749Y3Lla7oI&feature=plcp

Thanks for your help :slight_smile: