0

I am trying to move a object to it's destination (both represented by Vector3) at a constant speed.

Setup:

destination = gkVector3(1.0f, 0.0f, 1.0f);
myObj = gkVector3(0.0f, 0.0f, 0.0f);
velocity = 0.1;

Loop:

gkVector3 direction = destination - myObj;
myObj = myObj + direction * velocity;

if(myObj == destination) {
    gkPrintf("THE SAME!!!!");
}

Then, I want to check if the object has reached it's destination. If yes, a message will be showed. When velocity is set to 1.0, there is no problem at all; but when I set the velocity for example to 0.1 the object will never reach it's destination and the message will not be showed. So, how can I move an object precisely to it's destination at a constant speed?

*gkVector3 represents Ogre3D Vector3

Sergiu Dumitriu
  • 11,455
  • 3
  • 39
  • 62
user1491657
  • 81
  • 1
  • 1
  • 3

2 Answers2

0

I think that you should read a little about floating point arithmetic. In general it is a bad idea to test equality of floats using ==. More info about comparing floats in c++ can be found here here.

Community
  • 1
  • 1
Paperback Writer
  • 1,995
  • 2
  • 16
  • 27
0

I think the problem is that gkVector3 uses float point precision and you get a round off error ( since 0.1 can not be measured exact in the binary system.) This mean that myObj will be not exactly equal to destination. It will have an error of order 1e-16.

One solution is make a buffer. Example:

epsilon = gkVector3(0.00000001f, 0.00000001f, 0.00000001f);

if(abs(myObj - destination) <= epsilon)

where abs() is the absolute value

Erex
  • 11
  • 1
  • 2