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
I did not remember much about vector decomposition long to see, but I could understand perfectly explained thank you well. @mgibsonbr
– user3896400