Does somebody have tried to use the new c_intersect function to do a ray/box intersection test, yet? I can't get it working.

c_intersect is described here: http://www.conitec.net/beta/c_intersect.htm

According to the manual "for determining if a ray collides with a box, set the min1 and max1 parameters to the ray start position and vel1 to the ray distance vector". I'm not sure what the "ray distance vector" is supposed to be, though. A direction vector or the ray's endpoint? I tested it with both, but neither gives the correct result.

This is my code:

Code:
#include <acknex.h>

void main () {
	VECTOR cube_min, cube_max;
	VECTOR ray_start, ray_end, ray_dir;
	ENTITY *cube;

	level_load(NULL);
	vec_set(&sky_color, COLOR_WHITE);
	
	cube = ent_create(CUBE_MDL, vector(100, 0, 0), NULL);
	c_setminmax(cube);
	
	// set up ray
	vec_set(&ray_start, vector(100, -32, 0));
	vec_set(&ray_end,   vector(100,  32, 0));
	vec_diff(&ray_dir, &ray_end, &ray_start);
	
	// set up bounding box
	vec_set(&cube_min, &(cube->x));
	vec_add(&cube_min, &(cube->min_x));
	vec_set(&cube_max, &(cube->x));
	vec_add(&cube_max, &(cube->max_x));
	
	while (1) {
		var intersection1, intersection2;
		
		// move ray
		ray_start.z -= time_step;
		if (ray_start.z < (cube_min.z - 10))
			ray_start.z *= -1;
		ray_end.z = ray_start.z;
		
		// intersection test with ray endpoint
		intersection1 = c_intersect(&ray_start, &ray_start, &ray_end, &cube_min, &cube_max, NULL);
		DEBUG_VAR(intersection1, 10);
		
		// intersection test with ray direction vector
		intersection2 = c_intersect(&ray_start, &ray_start, &ray_dir, &cube_min, &cube_max, NULL);
		DEBUG_VAR(intersection2, 50);
		
		if ((intersection1 == -1) || (intersection2 == -1)) {
			draw_text("intersection!", 50, 10, COLOR_BLACK);
		}
		
		// draw ray
		draw_line3d(&ray_start, NULL, 100);
		draw_line3d(&ray_start, COLOR_BLACK, 100);
		draw_line3d(&ray_end, COLOR_BLACK, 100);
		
		wait(1);
	}
}



The first intersection test returns 0 and the second intersection test returns 24 (which is, interestingly, the distance between the ray's start point and the bounding box along the x-axis y-axis).

So, does somebody know how to use this function for doing a ray/box intersection test?

Last edited by Saturnus; 12/20/10 13:08. Reason: x-axis should read y-axis