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