Chiller
Chiller

Reputation: 21

LWJGL - How to trace a ray effectively and efficiently?

I have got the origin and a direction vector but I have no clue how to follow the ray and check for collisions...

Here is a picture of the ray, it goes out about 6 blocks.

Vector3f cam = camera.getPosition();
Vector3f dir = getDirection();

dir.x *= 40;
dir.y *= 40;
dir.z *= 40;

Vector3f dest = new Vector3f(cam.x + dir.x, cam.y + dir.y, cam.z + dir.z);


public Vector3f getDirection()
{
    Vector3f vector = new Vector3f();

    float rotX = camera.yaw;
    float rotY = camera.pitch;

    vector.y = (float) -Math.sin(Math.toRadians(rotY));

    float h = (float) Math.cos(Math.toRadians(rotY));

    vector.x = (float) (h * Math.sin(Math.toRadians(rotX)));
    vector.z = (float) (-h * Math.cos(Math.toRadians(rotX)));

    return vector;
}

I have tried using gluUnProject and it worked a little bit but like when you picked a face of a block it wasn't very precise.

BTW: I am using display lists for the chunks and I am just rendering block quads inside that display list. I get 60 FPS. I have been searching and searching but I cannot find ANYTHING on ray tracing and or ray picking... Thanks!

Upvotes: 2

Views: 1665

Answers (1)

Tara
Tara

Reputation: 1796

Your question is very unprecise.
Since your scene seems to consist of a grid, I suggest you to look into "3D Digital Differential Analyzer":
http://www.cse.chalmers.se/edu/course/_MY_MISSING_COURSE_2012/_courses_2011/TDA361_Computer_Graphics/grid.pdf
and:
http://en.wikipedia.org/wiki/Digital_differential_analyzer_%28graphics_algorithm%29

Upvotes: 1

Related Questions