   Gravity/Attractor field simulation?
  Hello and thanks for reading! I'm trying to make a simple "thing", in which i have two objects. An attractor, and a ball. I want the attractor to gradually attract(d'oh) the ball from any side, with the speed increasing when the ball is near the attractor, like planets do to asteroids. I have tried using a very crude inverse distance squared implementation, but it only works in one direction per dimension. Can anyone guide me through what I should to to achieve the effect mentioned above? I'd be grateful.   
  You're right about gravity being an inverse square function.I would find the distance between the two objects' centers (with the Pythagorean Theorem), then put that distance into a inverse square function to determine the net force on the object, then add to the object's x,y, and/or z speeds.  I'm not sure if this was your question...Anyway, here's some Python code:#I'm assuming you're doing this in 2D for this example.#The two objects' centers are represented by Object1Pos and Object2Pos#The two objects' speeds are represented by Object1Speed and Object2SpeedXDifference = Object1Pos[0] - Object2Pos[0]YDifference = Object1Pos[1] - Object2Pos[1]Distance = math.sqrt(  ((XDifference)**2)  +  ((YDifference )**2)  )Acceleration = 1.0 / (Distance**2)Theta = math.atan(XDifference/YDifference)Object1Speed[0] += Acceleration * math.sin(Theta)Object1Speed[1] += Acceleration * math.cos(Theta)Object2Speed[0] += Acceleration * math.sin(Theta)Object2Speed[1] += Acceleration * math.cos(Theta)Obviously, you add the speed to the objects' positions.  This only works if the objects have the same masses.  You can cut the last two lines if only one object moves (e.g. the other is infinitely massive).  Anyway, this should get you started.Geometrian   ;  Ooh, i get it! After i get the net force (which i've done) i multiply it with an unit vector pointing to the attractor object and get my vf.x and vf.y... Heh, Thanks alot :)EDIT: Well, i've done it, and it works... sort of. It only starts to "move" the ball when it gets within 5px of it, and if i multiply the force with any value, it starts going haywire when within 5px, and still nothing when not. Any suggestions?[Edited by - Dest1ny on February 2, 2008 12:49:18 PM];  HA!  Its great that you gave the example in python because thats what im using and the gravity code was something I was looking into.  Thx.For the problem with your gravity code Dest1ny... maybe increase the mass of the attracting body?  IIRC gravity in this case is represented by Acceleration = 1.0 / (Distance**2) right?  Mass divided by distance squared?  So make your mass higher then 1.Then again I could be totally off base.  I apologize if I am.   ;Quote:HA! Its great that you gave the example in python because thats what im using and the gravity code was something I was looking into. Thx.Sure!  Python's my favorite language.I've been working on a demo like this, just because this question piqued my curiosity.  I can explain your erratic results:You can't divide by zero, and if you're familiar with the graph of 'y = 1/x' you know that at x = 0, y = +- infinity.  So, as you get closer and closer, your gravity gets higher and higher.  Eventually, your acceleration will go to infinity when you're right next to it, and the object will move an infinite distance away by the next frame.  When the next gravity calculation happens, the force involved is tiny.  So, you get two objects moving slowly towards each other, then they go faster and faster until when they touch, they shoot off the screen.  I solved this problem by checking if the distance is less than 10, and, if so, changing the distance value for the calculation to 10.If your object is not moving at all, your float precision is too low.  Quote:i multiply it with an unit vector pointing to the attractor object and get my vf.x and vf.y...I did it by finding the angles involved and finding sines and cosines.  I think your way is far better.I think the code I provided needs to be modified to fix these problems...G   ;  Yeah, i was thinking of something like that, but thought there was a better way. And about the 5px margin, it's gone now.. i multiplied the net force by 5 and it's  better now.and Ahl, that wasn't mass. the formula is INVERSE distance squared, so it equals one per distance^2   ;  Here's my new movement code:def Move():    for P in Particles:   for P2 in Particles:  if P != P2: XDiff = P.x - P2.x YDiff = P.y - P2.y Distance = math.sqrt((XDiff**2)+(YDiff**2)) if Distance < 10: Distance = 10 Acceleration = (16.0/P.mass) / (Distance**2) XComponent = XDiff/Distance YComponent = YDiff/Distance P.speedx -= Acceleration * XComponent P.speedy -= Acceleration * YComponent    for P in Particles:   P.x += P.speedx   P.y += P.speedy;  Here's my mostly finished demo:Python Source: http://geometrian.com/Programs/Gravity%20Source.zip.exe File:http://geometrian.com/Programs/Gravity.zip   ;  If objects could collide the algorithm is simply:va = B-AAvelocity += n(va)*k*massB/(va DOT va)Bvelocity += n(-va)*k*massA/(va DOT va)(    -= n(va)*k*massA/(va DOT va))n(va) is normalized va.A = (1,0,0) B = (11,0,0)va = (10,0,0)Avelocity += (1,0,0)*k...You get the idea.Majority of problems happens because 1.0D - 1.0D doesn't always become 0. So it's highly likely to degrade quickly.IIRC softsurfer.net had an introduction into vectors and linear algebera.   ;  I am satisfied with an adaptation of Geometrian's code. It works fluidly and "realistically", and fits my game perfectly. I thank you all for your support ^_^   