Attraction of objects in python

3

I'm writing a code where one image goes towards another one that for some reason is not working.

I take the distance between them by calculating the force of gravitational% with% of attraction between them and decompose the resulting vector in Fg = G*M1*M2/D^2 and x thus increasing y and x every 0.1 seconds oo value divided by 10. I believe my error should be in the calculations regarding the variable type long, int, and float. But I can not solve it.

Code:

# -*- coding: cp1252 -*-
from __future__ import division
from pygame import *
from random import *
from math import *

class ob():
    def __init__(self,x,y,m,caminho = "x.png"):
        self.vx = 0
        self.vy = 0
        self.x = x
        self.y = y
        self.m = m
        self.img = image.load(caminho)

def main():
    init()
    screen = display.set_mode((800, 400))
    display.set_caption('Basic Pygame program')
    background = Surface(screen.get_size())
    background = background.convert()
    background.fill((250, 250, 250))
    MOVEEVENT, t = USEREVENT+1, 10
    time.set_timer(MOVEEVENT, t)

    c = time.Clock()
    screen.blit(background, (0, 0))
    display.flip()

    G = 6.67384*(pow(10,-11))

    ob1 = ob(50,50,4)
    ob2 = ob(100,100,4)    


    while 1:
        dx = ob2.x - ob1.x
        dy = ob2.y - ob1.x 
        dt = pow(pow(dx,2)+pow(dy,2),1/2)
        background.fill((250,250,250))
        background.blit(ob1.img,(ob1.x,ob1.y))
        background.blit(ob2.img,(ob2.x,ob2.y))

        F = (G*ob1.m*ob2.m)/(pow(dt,2))
        for y in event.get():
            if y.type == QUIT:
                display.quit()
            if y.type == MOVEEVENT:
                if ob1.x <100 and ob1.y < 100:
                    ob1.vx = F*0.01
                    ob2.vy = F*0.01
                    ob1.x = ob1.x +ob1.vx*0.01+((F*pow(0.01,2))/2)
                    ob1.y = ob1.y +ob1.vy*0.01+((F*pow(0.01,2))/2)
                    print ob1.vx,ob1.vy , ob1.x,ob1.y
        c.tick(100)
        screen.blit(background,(0,0))
        display.flip()



if __name__ == "__main__": main()

my line of reasoning and that their position will be given by the formula S = S0 + V0 * T + A * T ^ 2/2 their velocities will be calculated at Vx = F * 0.1 and Vy = F * 0.1 where F and F = G * M1 * M2 / Dt ^ 2

    
asked by anonymous 08.05.2015 / 22:03

1 answer

3

First of all, you are calculating the absolute value of the force between the two objects (ie composing the distance from the positions x and y ), but it is not decomposing this force again in coordinates using sine and cosine. In addition to the result going wrong - and identical to the directions x and y - it will always be positive regardless of the relative positions between the two objects.

F = (G*ob1.m*ob2.m)/(pow(dt,2)) if dt else 0

Fx = F * (ob2.x - ob1.x) / dt if dt else 0
Fy = F * (ob2.y - ob1.y) / dt if dt else 0

...

Secondly, you are using force between objects as acceleration. Failed to apply formula F = m * a :

if ob1.x <100 and ob1.y < 100:

    ax = Fx / ob1.m
    ay = Fy / ob1.m

Third, the position formula you cited is correct, but its implementation is not - you are updating the speed before position, so it is Vf being used in calculation, not V0 :

    ob1.x = ob1.x +ob1.vx*0.01+((ax*pow(0.01,2))/2)
    ob1.y = ob1.y +ob1.vy*0.01+((ay*pow(0.01,2))/2)

Finally, a*0.01 is delta v; you need to add it to the initial speed instead of replacing it:

    ob1.vx += ax*0.01
    ob2.vy += ay*0.01
    
09.05.2015 / 05:06