General Discussions

Gravity! – kurtp2003

kurtp2003
Member

Posts: 48
From: minnesota, US
Registered: 01-20-2006
I'm new here, so I don't know how many of you already know how to do this, but over the summer I did some work on making one object follow another at a rate that isn't affected by distance. This has many applications, but, in my case, the original reason was creating a source of gravity at the center of the screen for a space war (the very first video game) remake.

Anyway, I after a lot of thinking I did come up with a process to do this.

First we will need some generic variables to work with.

x1(the x position of the source of gravity)
y1(the y position of the source of gravity)

x2(the x position of the object being affected by the gravity)
y2(the y position of the object being affected by the gravity)

okay that's all you need to start. Now let's use these to set the values of some new variables.

xd=x2-x1
yd=y2-y1

these will be the first two legs of an imaginary right triangle

h=Sqr(Abs(xd)^2+Abs(yd)^2)

now we have the last piece of the triangle; the hypotonuse. Next we find the sine and cosine.

sin=xd/h
cos=yd/h

now, you subtract those number from the speed of object#2. You can multipy or divide them by whatever you want to change the force of the gravity.

Xspeed=Xspeed-sin
Yspeed=Yspeed+cos


Anyway, I hope this helps someone. Next I will post the source for an example of this in action.

kurtp2003
Member

Posts: 48
From: minnesota, US
Registered: 01-20-2006
Okay, here is an example:

Const upkey=200,rightkey=205,downkey=208,leftkey=203,space=57,bkey=48,f10=68
Graphics 800,600
SetBuffer BackBuffer()
SeedRnd(MilliSecs())

Type particle
Field x#,y#
Field h#
Field xd#,yd#
Field s#,c#
Field xs#,ys#
Field angle#
End Type

Type gravity
Field x#,y#
End Type

g.gravity=New gravity
g\x=400
g\y=300

While Not KeyDown(1)

Cls

If MouseDown(1)
p.particle=New particle
p\x#=MouseX()
p\y#=MouseY()
p\angle#=Rand(0,359)
p\xs#=Sin(p\angle#)*3
p\ys#=Cos(p\angle#)*3
EndIf

For p.particle=Each particle
p\x#=p\x#+p\xs#
p\y#=p\y#+p\ys#
For g.gravity=Each gravity
p\xd#=g\x-p\x#
p\yd#=g\y-p\y#
Oval g\x-1,g\y-1,3,3,1
Next

p\h#=Sqr(Abs(p\xd#)^2+Abs(p\yd#)^2)

p\s#=p\xd#/p\h#
p\c#=p\yd#/p\h#

p\xs#=p\xs#+p\s#
p\ys#=p\ys#+p\c#


Color 255,255,255
Next

Text 10,10,"this works"
Oval MouseX()-1,MouseY()-1,3,3,1

For p.particle=Each particle
Oval p\x-1,p\y-1,2,2,0
Next

Flip

Wend

End


Realm Master

Member

Posts: 1971
From: USA
Registered: 05-15-2005
veerrryyy interesting...

cool!

your a heck of a lot better in math than i am! (well, im good, but I just can't find any part of math to tie into my programming...)

------------------
(yes, i know im stupid)

Blessed are those who suffer for doing what is right.
The kingdom of hevan bleongs to them.-Matthew 5:10

PM ME YOUR DESCRIPTION OF ME! ILL PUT IT HERE!

Here's all the comments!

firemaker103

Member

Posts: 643
From:
Registered: 07-13-2005
Relam, the magic functions: Sin and Cos

------------------
"Be nice to the nerds because later on, you'll be working for them" - Bill Gates

kurtp2003
Member

Posts: 48
From: minnesota, US
Registered: 01-20-2006
Thanks. Sorry I accidentally posted this on the wrong board, but I'm new here, so I'm probably going to make a few mistakes.

Anyway, though this program looks nice (despite the simple interface) I'm really not a particularly good programmer- or kindof anyway. Because I'm completely sef taught I haven't learned to do a lot of basic things, but the stuff I do know how to do I can do as well as most people.

buddboy

Member

Posts: 2220
From: New Albany, Indiana, U.S.
Registered: 10-08-2004
wow... you just TOTALLY lost me in only 2 posts!! ;p;!!


P.S. about the ;p;, i'm trying to start a new internet trendy thingy, to get people to say ;p; instead of lol, because i did it on accident once and it stuck... so please help me if you want!

------------------
In the stock market, you must buy high and sell low...Wait! That's not right!
--------------
Yes, I can be intelligent at times!!

kurtp2003
Member

Posts: 48
From: minnesota, US
Registered: 01-20-2006
quote:
Originally posted by buddboy:
wow... you just [b]TOTALLY lost me in only 2 posts!! ;p;!!


P.S. about the ;p;, i'm trying to start a new internet trendy thingy, to get people to say ;p; instead of lol, because i did it on accident once and it stuck... so please help me if you want!

[/B]



Do you have Blitz? If you do, than copy the code from my second post, and run it, and, hopefully you'll get it.

firemaker103

Member

Posts: 643
From:
Registered: 07-13-2005
Yea, another blitz user!
--

Anyway, besides from that teaching people, it can also make some good desgins

------------------
"Be nice to the nerds because later on, you'll be working for them" - Bill Gates