2

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!

Olivier Moindrot
  • 27,908
  • 11
  • 92
  • 91
Chiller
  • 21
  • 2
  • There is IMHO huge amount of math behing raycasting/tracing. Depends on scene, on types of objects you'd like to trace with (triangle meshes, geometric shapes, ...). Then you have to make some spatial space division, otherwise you'll check ray vs. object for all objects in scene a it's always really slow. So you have to use BSP tree or K-d tree for scene definition. – Sorceror Nov 12 '12 at 15:14
  • You may find some useful info how to implement ray picking in LWJGL at http://gamedev.stackexchange.com/questions/31070/ray-picking-problems and http://stackoverflow.com/questions/10809021/lwjgl-3d-picking – Sorceror Nov 12 '12 at 15:17
  • If you are wanting to determine if a ray hits a cube, you need to understand that you are testing collisions for 6 planes. I would start by looking at plane intersect code. – seesharper Nov 28 '12 at 17:26
  • @Sorceror: You don't need complicated data structures to get started; in the beginning, a list with just up to a few ten objects is enough (and might even be superiour to very small BSP trees and other acceleration structures) – Sebastian Mach Jan 22 '13 at 05:35
  • @phresnel sure depends on scene size, but determine intersection with some mesh based object is quite expensive (all triangles without any AABB test or something ahead), so eventually you'll end with some space partitioning solution anyway.. – Sorceror Jan 23 '13 at 09:17
  • @Sorcerer: Indeed, except when you are just having fun with some geometric objects (classy spheres for example). Just wanted to take a bit away of the _Chilling Math Needed_ comment; because as a newbie, you really do not need much math (and datastructures). – Sebastian Mach Jan 23 '13 at 11:40

1 Answers1

1

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

Tara
  • 1,673
  • 22
  • 30