planet attraction code

BlitzMax Forums/BlitzMax Beginners Area/planet attraction code

hub(Posted 2006) [#1]
Hi !
See this code.
when the white ship is near the planet (green zone) it should be attracted. In some situations it is not the case... so Could you help me to understand what'is wrong with my code ! Thanks !

Graphics 800,600

Const cte_div = 5

HideMouse()

' init

MoveMouse GraphicsWidth()/2, GraphicsHeight() / 2

px# = MouseX()
py# = MouseY()

cx = 200
cy = 150
cr1  = 50
cr2 = 120
speed# = 0.5

While Not KeyDown (KEy_ESCAPE)

Cls

' player movement (rockout style)

	xdist# = MouseX() - px
	ydist# = MouseY() - py
	xs = xdist / cte_div
	ys = ydist / cte_div
	px = px + xs
	py = py + ys
	
' display planet

	SetColor 0,255,0
	DrawOval cx - cr2, cy- cr2, cr2 * 2,cr2 * 2
	SetColor 0,0,255
	DrawOval cx - cr1,cy - cr1 ,cr1 * 2, cr1 * 2
	SetColor 255,255,255
	Plot ccx,ccy
	

' planet attraction code

	d = Int (Sqr((px-cx) * (px-cx) + (py-cy) * (py-cy)))
	
	If d <= cr2 And d > cr1 Then
	
		SetColor 255,255,255
		DrawText "attracted !",cx,cy
	
	    angle = ATan2((cy - py),(cx - px))
	
		xmov# = Cos (angle) * speed#
		ymov# = Sin (angle) * speed#
		px = px + xmov#
		py = py + ymov#
		
		MoveMouse MouseX() + xmov#, MouseY() + ymov#		
		DrawLine cx,cy, px,py
		
		
	End If

' display player

	SetColor 255,255,255
	DrawOval px-25, py-25, 50,50
	SetColor  255,0,0
	DrawOval MouseX()-3, MouseY()-3,6,6
	
' display infos
	
	DrawText "dist=" + String (d),0,0
	DrawText "x=" + String (px),0,10
	DrawText "y=" + String (py),0,20
	DrawText "xmov=" + String (xmov),0,30
	DrawText "ymov=" + String (ymov),0,40
	DrawText "angle=" + String (angle),0,50

Flip

Wend

ShowMouse()



Dreamora(Posted 2006) [#2]
General attraction formula for mass bodies:

force = mass * acceleration = (mShip * mPlanet) / (distance * distance)

So if you combine those two, the acceleration is force/mShip =>mPlanet/(distance*distance), targeted to the center of the planets mass (normally its center :))


hub(Posted 2006) [#3]
Thanks, but what'is mShip or mPlanet ? planet mass and ship mass ? could you post a sample adapted to my code variables ?


hub(Posted 2006) [#4]
this ?



Moogles(Posted 2006) [#5]
actually the force equation is
F=(Gm1m2)/r^2
Where G is the gravitational constant = 6.67300 × 10^-11 m^3 kg^-1 s^-2 and m1 and m2 are the masses in kg, r is the distance from the centre of gravity of the two masses.
dreamora is just missing the G part.


Dreamora(Posted 2006) [#6]
mShip and mPlanet are the masses of the ship and the planet.

Moogles: Did not miss it, but G is only a linear physical scaling size, which is why I did not enter it to the formula. Linear can be applied on will at any time ...
1/FPS for example could be seen as a G as well in frame independent programming ... :-)


Eric(Posted 2006) [#7]
I played around with this too.
GravitySum:TVector.Equals(0,0)

For Local Planet:TPlanet=EachIn PlanetList
	Range.Between(Planet.Position,Self.Position)
	Local Distance:Float=Self.Position.DistanceTo(Planet.Position) 
	Local GravityPull:Float=(Mass*Planet.Mass*G)/(Distance^2)
	Local Angle:Float=ATan2(Range.Y,Range.X)  
	Local GravityNormal:TVector=Planet.Position.Copy()
	GravitySum.AddPolar(Angle,GravityPull)
Next


This code won't run but it's the idea.

The loop is because I had many planets all exerting a pull on my "Ship"


Dreamora(Posted 2006) [#8]
2 suggestions:
- use distance*distance instead of ^2. Its faster
- I would consider a maximum distance at which the gravity has an effect. If the distance becomes to large, the output becomes wrong.


Eric(Posted 2006) [#9]
Yeah that's true. Thanks.

.. In my program.. When you get too close GravityPull would get ridiculously huge. My ship would go hurling off into space at the speed of light.

So I clamped it to a maximum that worked in my program.


Moogles(Posted 2006) [#10]
ah k. i thought it would be handy to know it as well ;).
you do physics dreamora? :P


Dreamora(Posted 2006) [#11]
Its a sideeffect of study of eletrotechnics and informationtechnology, that you are forced to learn physics (for building integrated circuits and the like :)).

But this specific topic here is a different thing: Have a project thats on ice atm, that focuses around gravitic / coulomb physics only.


hub(Posted 2006) [#12]
uhm... is the code posted in my previous message is ok with dreamora formulae ? don't blame me i crap with maths and physics ! Thanks for your help


Moogles(Posted 2006) [#13]
if you use the formula that dreamora posted it would work. G is just a constant you can include it if you like or not. id say include it if you want it to be real life physics.


hub(Posted 2006) [#14]
in fact, there are something strange with my code. it seems not be realistic... (if ship enter into the green zone by the left). Maybe a bad implementation of dreamora formulae ?


Dreamora(Posted 2006) [#15]
An implementation of the code needs the following check
local radius_factor:float  = 1.0 / (distance * distance)
if radius_factor < 0.001   radius_factor = 0.001


Otherwise you might see the "gets near center and accelerates to infinity" behavior ;)

Can you describe your problem a little or even provide some running code?


hub(Posted 2006) [#16]


try to enter into the green area from the left.


hub(Posted 2006) [#17]
bump ;-)


hub(Posted 2006) [#18]



SillyPutty(Posted 2006) [#19]
That is very cool indeed ! :)