 TNT Basic Forums > Programming Q&A Problems with velocity w/ rotation < Last Thread     Next Thread >
Author
 Thread  02-04-2002 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*5-90)
xvel=vel*Cos(spriteNum*5-90)

--spriteNum is the rotation (72 frames)

Anybody? 02-05-2002 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 polygon-ish 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 02-05-2002 06:12

Posted by:
bgroves

hmmm...

That's not quite the problem. The motion of the ship going in a circle is actually pretty good--the 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 02-05-2002 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 02-05-2002 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. 02-06-2002 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 Forum Jump: Please select one: Games In Progress Programming Q&A Beyond Basic Feedback Game-In-60 Tutorials Q&A Bugs! TNT Basic - The Open Source Edition < Last Thread     Next Thread >