Hit Scan Colllsion

Started by
16 comments, last by s_cloudx 20 years, 10 months ago
Okay, I understood how the second order is solved.

But, I''m still confused. I thought when a math involves a Vector, the result is a vector. Like so:

Vector RayPos;
Vector SpherPos;
float SphereRad;

wouldn''t the following create a vector?

c = ((RayPos - SpherePos) * (RayPos - SpherePos)) - (SphereRad * SphereRad);

like so,

c = ((Vector - Vector) * (Vector - Vector)) - (float * float)
c = (Vector * Vector) - float
c = Vector

thus, the result is a vector?

Or am I doing something terribly wrong here?
--------------------------Sony "Mr. Valdez" ValdezPinoyforum.net Technical EditorPinoyforum.net - the breeding ground of the Filipino Computer Talents
Advertisement
You can''t subtract a scalar from a vector, so that expression only makes sense if the * is a dot product, which gives you a scalar from two vectors.
You know what I never noticed before?
So, it''s not correct to subtract a scalar value from a vector? I always thought that:

Vector - scalar = Vector(Vector.x - scalar, Vector.y - scalar, Vector.z - scalar)

So, what is the correct way to subtract scalar value from a vector?
--------------------------Sony "Mr. Valdez" ValdezPinoyforum.net Technical EditorPinoyforum.net - the breeding ground of the Filipino Computer Talents
you can substract vectors to vectors so you''d do

Vector V (V.x, V.y, V.z)
Vector S (scalar, scalar, scalar)

Vector W = V - S

would achieve what you want

Everything is better with Metal.

But then, the result would be a vector and t would have to be a vector, too. Oh, dear.

EDIT: I sounded like a ninnynack, so I'll try to say it in another way. *AHEM*

Currently, this is what my code looks like:

Vector c = ((RayPos - SpherePos) * (RayPos - SpherePos)) - (SphereRad * SphereRad);Vector b = 2.0f * (RayDir * (RayPos - SpherePos)); Vector a = (RayDir * RayDir); Vector d = b*b - 4*a*c; 


Later in the code, I have this:

t0 = (-b - sqrt(d)) / (2*a); /*...*/t = t0; //where t is a Vector 


If I make t a float, I would have to decide if I should put in t0's x, y, or z value. But since you said t is the distance of how far an object is, I knew I made a mistake somewhere. I suspect that my Vector maths are wrong. Could someone please aid me here?

BTW, oliii, I really appreciate your help. Thank you!

[edited by - s_cloudx on June 4, 2003 8:45:34 PM]
--------------------------Sony "Mr. Valdez" ValdezPinoyforum.net Technical EditorPinoyforum.net - the breeding ground of the Filipino Computer Talents
quote:Original post by s_cloudx
But then, the result would be a vector and t would have to be a vector, too. Oh, dear.

If I make t a float, I would have to decide if I should put in t0''s x, y, or z value. But since you said t is the distance of how far an object is, I knew I made a mistake somewhere. I suspect that my Vector maths are wrong. Could someone please aid me here?

BTW, oliii, I really appreciate your help. Thank you!





You got it all wrong

t is a float and CANNOT be equal to a vector. A vector length maybe, or a vector component (either x, y or z), or a dot product of 2 vectors, or any vector operations that return a float.

a, b, c are floats too. They are NOT vectors. Look into the equations for ''c'' for example.

(RayPos - SpherePos)*(RayPos - SpherePos) is a dot product of two vector substractions. the substractions produce a vector, but the dot products of the two substractions will turn the result into a scalar. (SphereRad * SphereRad) is a product of scalars, which return a scalar. so a = scalar + scalar = scalar.

a, b, c are lowercase names. This usually means they are either a float or a int. Basically a scalar.

t is a float and is the parametric component for the line equation

P = RayPos + t * RayDir;

Raypos is a vector, Raydir is a vector, (t * raydir) is also a vector (t * RayDir = Vector(RayDir.x*t, Raydir.y*t, RayDir.z*t)).

I use equations in parametric form, which applies into any dimensions (2D, 3D, whatever), and you solve the set of equations for a parameter which is a scalar, with no units attached to it. It''s like a percentage, it''s just a number.


OK, to clear things up, I''ll note the vectors as vVectorName and floats as fFloatName.

Vector vRelPos = vRayPos - vSpherePos;float fC = (vRelPos * vRelPos) - (fSphereRad * fSphereRad);float fB = 2.0f * (vRayDir * vRelPos); float fA = (RayDir * RayDir); float fD = fB*fB - 4*fA*fC; float fT0 = (-fB - sqrt(fD)) / (2*fA);float fT1 = (-fB + sqrt(fD)) / (2*fA);if (fT1 < 0.0f && fT0 < 0.0f)    return false;fT = MinimumButPositive(fT0, fT1);vPointOfIntersection = vRayPos + (vRayDir * fT);

Everything is better with Metal.

BTW, if you have any specific questions, you can email me. I won''t bite And you''ll get your answers quicker. This thread has been going on for more than a month now, and you''re still stuck

first thing though, if you want to make a FPS, I strongly suggest you get fluent in vector / matrix / quaternions / geometry. If you don''t get it, you won''t go anywhere :D

Everything is better with Metal.

quote:Original post by s_cloudx
So, it''s not correct to subtract a scalar value from a vector? I always thought that:

Vector - scalar = Vector(Vector.x - scalar, Vector.y - scalar, Vector.z - scalar)

So, what is the correct way to subtract scalar value from a vector?


well, you could implement an operator which would do this, but it''s really not recommended. at all.

class Vector{..............    friend Vector operator-(float k, const Vector& V) { return Vector(V.x-k, V.y-k, V.z-k); }    Vector operator-(float k) { return Vector(x-k, y-k, z-k); }};


this would be a perfectly valid operator, but it would be too confusing. Avoid stuff like that at all costs. C++ is great in the sense that it lets you do pretty much whatever you want with operators, but it can introduce confusion if you over-do it.

for the cross product, try

    Vector operator ^ (const Vector^ V)     {         return Vector(y*V.z-z*V.y, z*V.x-x*V.z, x*V.y-y*V.x);     }


and you can do a cross product like this

Vector vCrossProduct = vFirstVector ^ vSecondVector;

Everything is better with Metal.

This topic is closed to new replies.

Advertisement