Author

Thread





02042002 09:12
Posted by:
bgroves

I'm trying to make a ship move in 2D using rotation and velocity (á la EV or WingNuts), and have found that, if the velocity is pretty small, the motion is off (and also the ship doesn't make a perfect circle). Would this motion problem be a function of my code, or some kind of rounding that TNT does?
Here's a piece of my code:
yvel=vel*Sin(spriteNum*590)
xvel=vel*Cos(spriteNum*590)
spriteNum is the rotation (72 frames)
Anybody?

02052002 04:05
Posted by:
Alex Ni

Using velocities
If you are using that code for a velocity,
then this is not TNT Basic's fault, but
instead that of using an approximation.
Remember that you are only approximating a
circle using a bunch of lines, (albeit small
ones) so the ship will actually move in a
sort of polygonish shape (that does not
necessarily close on itself.) This is
especially bad when the velocity is small
because the number of sides of this polygon
shrinks.
If you want exact circular motion, directly
generate a position instead of a velocity
that way the ship will still travel in an
approximation of a circle, but it will at
least end up in the same location.
Best of luck!
A. Ni

02052002 06:12
Posted by:
bgroves

hmmm...
That's not quite the problem. The motion of the ship going in a circle is actually pretty goodthe problem being that it drifts (continually but slowly) to the upper left. But that doesn't bother me so much as the low velocity problem. When the velocity is low, it does not move in the direction the ship is pointing, even though the sin and cos values are coming out right. Can't seem to figure it out.
B

02052002 12:48
Posted by:
Mark Tully

Underflow
I'm assuming that all variables related to these calculations a floats and not ints.
It's most likely that the numbers in the floats get too close to zero and get rounded out. TNT Basic uses single precision floating point variables because they're faster and are generally good enough for game performance. Double precision might solve this, but there could be others ways of solving it too, such as working with numbers a hundred times larger than you need and dividing down when you come to use them. I'm not sure if that would work due to the nature of floating points, but you could try...
Anyone got any other suggestions?
Mark

02052002 20:55
Posted by:
Heiko Hartmann

Probably you need to round...
This may indeed be a rounding problem.
To illustrate this problem take a look at the following small TNT Basic program:
int i
float sum = 0
for i = 1 to 10
sum = sum + 0.1
next i
print sum
if sum = 1
print "Sum is 1"
else
print "Sum is NOT 1"
end if
First think about it. Which output do you expect?
Then you should open a new project, paste this code in and let it run.
Surpise!
Don't be afraid  this is not a bug in TNT Basic, this is simply a poblem with floats. Computers have problems with numbers like '0.1' (they cannot represent it correctly with the binary system).
The solution to this problem is to
a.) Round the results values, e.g. in the if statement use Round(sum) instead of sum.
b.) use larger numbers and divide them whenever you use them, e.g. add 1 to sum instead of 0.1 and whenever you need the value of sum use sum/10.
Ciao, Heiko.

02062002 18:59
Posted by:
bgroves

wow..
I feel pretty dumb. I changed my position variables to floats and now it works fine <duh>. Why I didn't think of this a long time ago, dunno. Thanks y'all.
B

All times are GMT




