Jump to content
  • Advertisement
Sign in to follow this  
swilkewitz

Vector(0,0,-1) leading to math problems

This topic is 2641 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I currently have a system that allows me to place thruster objects onto certain surfaces of my ship's model. Most surfaces work fine, but those that face perfectly forward cause problems with my math library because their normal lines are either vectors of (0,0,-1) or (0,0,1). I probably also have some minor math misconceptions that need to be fixed... but for the most part my library works everywhere else.

I have created classes to represent points, vectors, shapes, models, and quaternions, which can be found here:
http://code.google.com/p/triax-bridge-command/
Go to source, trunk, math.

The point where my code has problems originates in the allign() method of my Thruster class which I will post below. Basically, the method sets the thruster onto the shape by copying its position and orientation. I have bypassed the problem temporarily by setting the components to 0 (or 1) plus an insignificant amount.


void Thruster::allign()
{

normal = shape.normal;

//Position
position = shape.getCenter();

//Bypass
if(shape.normal.i == 0.0f && shape.normal.j == 0.0f && shape.normal.k == 1.0f){
shape.normal.i = 0.00000001f;
shape.normal.j = 0.00000001f;
shape.normal.k = 1.00000001f;
}

//Direction
Vector v(shape.normal);
orientation = Quaternion();
Quaternion q(Quaternion(v).toCamera());
orientation = q * orientation;
}


Also, is it necessary for me to have a .toCamera() method to convert my quaternions for my camera class? In this method I use it outside of the camera class, but should I need it in the first place?

Share this post


Link to post
Share on other sites
Advertisement
Though I suspect its related to your workaround, and not the actual problem, I figure I should mention that under most conditions, direct comparisons of floating point values aren't reliable. What I mean by that is that way the computer represents numbers such as 1.5f, is not necessarily exact.

The most commonly used way of checking the value of a float is to check the difference between the float you're testing and the value to test against and see if that's close to 0.
for example:


if (Abs(value - 1.5f) < 0.0000001)
{

}

instead of :


if (value == 1.5f)
{

}

Share this post


Link to post
Share on other sites
There's nothing special about that vector that should be causing you any problems.

Your problem is a bug in your Quaternion constructor. //Cross v2 with Default
Vector v1(0.0f, 0.0f, -1.0f);
Vector f(v1.cross(dv));//uh oh, what if dv == (0,0,-1)? Will f == (0,0,0)? That doesn't sound valid.


While I've got my head in your code, this:
[font="Courier New"](v1.getLength() * v1.getLength())[/font]
Expands to:
[font="Courier New"]sqrt(i*i + j*j + k*k) * sqrt(i*i + j*j + k*k);[/font]
Which is mathematically equivalent to:
[font="'Courier New"]i*i + j*j + k*k[/font]
So, you should also add a [font="Courier New"]getLengthSquared()[/font] method to your vector class to avoid two unnecessary sqrt calls here wink.gif

Share this post


Link to post
Share on other sites

There's nothing special about that vector that should be causing you any problems.

Your problem is a bug in your Quaternion constructor. //Cross v2 with Default
Vector v1(0.0f, 0.0f, -1.0f);
Vector f(v1.cross(dv));//uh oh, what if dv == (0,0,-1)? Will f == (0,0,0)? That doesn't sound valid.


While I've got my head in your code, this:
[font="Courier New"](v1.getLength() * v1.getLength())[/font]
Expands to:
[font="Courier New"]sqrt(i*i + j*j + k*k) * sqrt(i*i + j*j + k*k);[/font]
Which is mathematically equivalent to:
[font="Courier New"]i*i + j*j + k*k[/font]
So, you should also add a [font="Courier New"]getLengthSquared()[/font] method to your vector class to avoid two unnecessary sqrt calls here wink.gif


Does crossing identical vectors result in an undefined vector? Should I return an error flag and handle it in the calling method?
Thanks for the suggestions!

-Scott

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!