forums

TNT Basic Forums > Programming Q&A
moving in a circle
< Last Thread     Next Thread >
Author
Thread        Post A Reply

01-23-2004 00:21

Posted by:
puffin

Click Here to Email puffin   Find more posts by puffin

anyone know how to make a sprite move in a circle? i don't want the sprite to rotate, i just want it to move in a circle and i'm just having an unreasonably hard time doing it.

01-23-2004 00:29

Posted by:
puffin

Click Here to Email puffin   Find more posts by puffin

also, does anyone know a way to connect two sprites. i was considering rotating a sprite and having another sprite attached to it. that way the second sprite would move around in a circle without any change in rotation.

01-23-2004 01:19

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Equation of a circle

You can use the equation of a circle. Although the relation of a circle is not a function (it has more than one value of y for every x) you can get around it.

When r is the radius,
x^2+y^2=r^2

You can manipulate this to get
y=±Squareroot(r^2-x^2)
All you have to do is see if the y should be positive or negative with an if statement.

I'll try to make an example in a sec.

01-23-2004 01:23

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Complete equation

Actually, I left out the variables for the horizontal/vertical shift, assuming that you could just change the center later. However, if you want it all in the equation, here it is:

When h=the number shifted to the right
and k=the number shifted down

(x-h)^2+(y-k)^2=r^2

01-23-2004 01:34

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Easier way

Actually, I just realized a much simpler way: use cosine, sine, and degrees.

graphics mode 800,600
int x,y,degree=0,h=200,k=200,radius=50
int increment=2,c

repeat
c=wrap(c+10,100,250)
degree=wrap(degree+increment,0,359)
x=radius*cos(degree)
y=radius*sin(degree)
set pen color make color(c,c,c)
fill rect x-5+h,y-5+k to x+5+h,y+5+k
'set pen color black
'paint canvas
draw frame
until mouse button

To change the direction, just make the increment negative.

01-23-2004 02:53

Posted by:
puffin

Click Here to Email puffin   Find more posts by puffin

thanks, it works nicely. i was getting there, just slowly. i was able to figure out the formula for a circle from the distance formula, which is about the only real math i know. thanks again.

01-23-2004 02:54

Posted by:
puffin

Click Here to Email puffin   Find more posts by puffin

oh yeah, what the hell does wrap() do?

01-23-2004 02:58

Posted by:
DanLurie

Location:
Earth>USA>New Jersey>Clifton>My Chair

Click Here to Email DanLurie   Find more posts by DanLurie

quote:
oh yeah, what the hell does wrap() do?

I think it makes you a sandwich.

01-23-2004 03:55

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

You give it a number, a minimum range, and a max range, and it wraps it-in other words, if the number is past the maximum number, it starts from the minimum number again.

Just check the TNT Help.

01-23-2004 14:34

Posted by:
puffin

Click Here to Email puffin   Find more posts by puffin

sweet jesus! why haven't i ever heard of this function before? it sounds to me like it would have been just what i needed for another game i was making. i ended up quitting that project. might start it up again. might also give the tnt basic documentation a closer look.

01-23-2004 18:39

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Well, you could just as easily make your own procedure for it.

procedure WrapNum(int number,int min,int max)
while number>max
number=number-max
wend
while number<min
number=number+max
wend
end proc(number)

01-23-2004 21:21

Posted by:
DanLurie

Location:
Earth>USA>New Jersey>Clifton>My Chair

Click Here to Email DanLurie   Find more posts by DanLurie

Or use mod creatively.

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.