Attraction of objects in python

Asked

Viewed 112 times

3

I’m writing a code where one image goes toward another only for some reason it’s not working.

I take the distance between them calculate the gravitational force Fg = G*M1*M2/D^2 of attraction between them and decompose the resulting vector into x and y thus increasing the x and the y every 0.1 according to the value divided by 10. I believe my error must be in the calculations regarding the type of variable long, int and float. However I cannot solve.

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 rationale is that their position will be given by the formula S=S0+V0*T+A*T 2/2 its speeds will be calculated in Vx= F*0.1 and Vy = F*0.1 where F and F=G*M1*M2/Dt 2

1 answer

3


First, you are calculating the absolute value of the force between the two objects (i.e. making up the distance from the positions x and y), but is not decomposing this force again in coordinates using sine and cosine. Besides the result go wrong - and identical to directions x and y - it will always be positive regardless of the relative positions between the two objects.

inserir a descrição da imagem aqui

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

...

Second, you are using the force between the objects as acceleration. Failed to apply the 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 quoted is correct, but its implementation is not - you are updating the speed before position, so that is Vf which is being used in the calculation, and 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
  • I did not remember much about vector decomposition long to see, but I could understand perfectly explained thank you well. @mgibsonbr

Browser other questions tagged

You are not signed in. Login or sign up in order to post.