12-02-2002, 02:07 AM

I'm having some issues with some math in my code, and am totally stuck. Here is my cry for help!

Problem:

I have a point in space, and a sphere. The sphere moves, the point in space [at this point] does not. What I need, is to know if any of the tris on this sphere, are facing the point. Note, I'm calculating this every frame, and the sphere could move anywhere in the world.

My Approach So Far:

For every face [tri] of this sphere, I am calculating a plane. (foot#1 for code). After calculating this plane for every face, I test each plane for being 'facing' the point or not (foot#2 for code). I am testing to see results, by coloring the tris different colors if they are facing or not.

The problem:

This doesn't seem to work really at all. I get a few positives, but they don't make sense at all really, and they don't change when I move my sphere around.

I got the general idea of this from an article on nvidia's developer site, the code they use looks something like this (foot #3), but the variable names they are using don't seem to make a lot of sense to me.

Thanks very much in advance to anybody that can respond to this.

// footnote #1

// calculating a plane from the verts

// of the 3 verticies that form a face

// **NOTE**

// the 3 verticies being passed, are the

// local values I am keeping for this

// object in memory, NOT their position

// in world coordinates, maybe this is

// a problem??

void Plane::calculate (float t1[3], float t2[3], float t3[3]) {

float v0[3];float v1[3];float cr[3];float k;

v0[X] = t2[X] - t1[X];

v0[Y] = t2[Y] - t1[Y];

v0[Z] = t2[Z] - t1[Z];

v1[X] = t3[X] - t1[X];

v1[Y] = t3[Y] - t1[Y];

v1[Z] = t3[Z] - t1[Z];

cr[X] = v0[Y] * v1[Z] - v0[Z] * v1[Y];

cr[Y] = v0[Z] * v1[X] - v0[X] * v1[Z];

cr[Z] = v0[X] * v1[Y] - v0[Y] * v1[X];

k = sqrt(cr[X] * cr[X] + cr[Y] * cr[Y] + cr[Z] * cr[Z]);

a = cr[X] / k;

b = cr[Y] / k;

c = cr[Z] / k;

d = - (a * t1[X] + b * t1[Y] + c * t1[Z]);

}

// footnote #2

bool facingplane =

(plane.a * pointposition[X] +

plane.b * pointposition[Y] +

plane.c * pointposition[Z] +

plane.d * 1); //1 being W

// footnote #3

// from infinite shadow volumes.cpp

// (c) somebody ;) only relevant parts pasted

//

// **NOTE**

// what I'm mostly confused about, is how

// 'olight' is derived.. it seems that

// it comes from 4x4 matrix

// 'object[in my case, the sphere] .get_inverse_transform() [?????]

// multiplying light [the point in my case, maybe?] .get_transform() [??], and then

// multiplying the resulting matrix against

// the light source [point in my case]'s location

vec4f olight;

matrix4f ml = object[mindex].get_inverse_transform() * light.get_transform();

ml.mult_matrix_vec(light_position, olight);

object[mindex].apply_transform();

bool facing_light = (( p.a * olight[0] +

p.b * olight[1] +

p.c * olight[2] +

p.d * olight[3] )

>= 0 );

Problem:

I have a point in space, and a sphere. The sphere moves, the point in space [at this point] does not. What I need, is to know if any of the tris on this sphere, are facing the point. Note, I'm calculating this every frame, and the sphere could move anywhere in the world.

My Approach So Far:

For every face [tri] of this sphere, I am calculating a plane. (foot#1 for code). After calculating this plane for every face, I test each plane for being 'facing' the point or not (foot#2 for code). I am testing to see results, by coloring the tris different colors if they are facing or not.

The problem:

This doesn't seem to work really at all. I get a few positives, but they don't make sense at all really, and they don't change when I move my sphere around.

I got the general idea of this from an article on nvidia's developer site, the code they use looks something like this (foot #3), but the variable names they are using don't seem to make a lot of sense to me.

Thanks very much in advance to anybody that can respond to this.

// footnote #1

// calculating a plane from the verts

// of the 3 verticies that form a face

// **NOTE**

// the 3 verticies being passed, are the

// local values I am keeping for this

// object in memory, NOT their position

// in world coordinates, maybe this is

// a problem??

void Plane::calculate (float t1[3], float t2[3], float t3[3]) {

float v0[3];float v1[3];float cr[3];float k;

v0[X] = t2[X] - t1[X];

v0[Y] = t2[Y] - t1[Y];

v0[Z] = t2[Z] - t1[Z];

v1[X] = t3[X] - t1[X];

v1[Y] = t3[Y] - t1[Y];

v1[Z] = t3[Z] - t1[Z];

cr[X] = v0[Y] * v1[Z] - v0[Z] * v1[Y];

cr[Y] = v0[Z] * v1[X] - v0[X] * v1[Z];

cr[Z] = v0[X] * v1[Y] - v0[Y] * v1[X];

k = sqrt(cr[X] * cr[X] + cr[Y] * cr[Y] + cr[Z] * cr[Z]);

a = cr[X] / k;

b = cr[Y] / k;

c = cr[Z] / k;

d = - (a * t1[X] + b * t1[Y] + c * t1[Z]);

}

// footnote #2

bool facingplane =

(plane.a * pointposition[X] +

plane.b * pointposition[Y] +

plane.c * pointposition[Z] +

plane.d * 1); //1 being W

// footnote #3

// from infinite shadow volumes.cpp

// (c) somebody ;) only relevant parts pasted

//

// **NOTE**

// what I'm mostly confused about, is how

// 'olight' is derived.. it seems that

// it comes from 4x4 matrix

// 'object[in my case, the sphere] .get_inverse_transform() [?????]

// multiplying light [the point in my case, maybe?] .get_transform() [??], and then

// multiplying the resulting matrix against

// the light source [point in my case]'s location

vec4f olight;

matrix4f ml = object[mindex].get_inverse_transform() * light.get_transform();

ml.mult_matrix_vec(light_position, olight);

object[mindex].apply_transform();

bool facing_light = (( p.a * olight[0] +

p.b * olight[1] +

p.c * olight[2] +

p.d * olight[3] )

>= 0 );