python - 模拟重力作用下的两个粒子

标签 python physics-engine

我有一些代码,我认为它大部分有效。我应该对两个粒子在二维重力作用下相互作用的方式进行建模。它一直有效,直到 update 函数的第二个循环为止,所以我认为 update 函数有问题,但我看不到它。

from __future__ import print_function, division
import numpy as np # imports the scientific computing package
G = 10 # of course G is not really 10 but this make it easier to track the accuracy of calculations.  
t=5 # defines the timestep

class Particle:
    def __init__(self,mass):
        self.mass = mass # gives the particle mass
        self.position = np.array([0,0],int) # gives the particle a position defined by a vector. Therefore all answers will now be given as vectors.    

    def calculateForce(self,other):
        '''
        this function calculates the force acting on the particles
        '''
        force = (G*self.mass*other.mass)/(other.position - self.position)**2
        print('The force acting on the particles is', force)
        return(force)

    def calculateAcceleration(self,force):
        '''
        calculates the acceleration of the first particle
        '''
        acceleration = (force/self.mass)    
        print('Acceleration of particle is', acceleration)
        return acceleration   

    def calculateAcceleration1(other, force):
        '''
        calculates the acceleration of the second particle
        '''
        acceleration1 = (force/other.mass)
        print('Acceleration of particle1 is', acceleration1)
        return acceleration1        

    def calculateVelocity(self, acceleration):
        '''
        calculates the velocity of the first particle
        '''
        velocity = (acceleration*t)
        print('Velocity of particle is', velocity)
        return velocity

    def calculateVelocity1(self, acceleration1):
        '''
        calculates the velocity of the second particle
        '''
        velocity1 = (acceleration1*t)
        print('Velocity of particle1 is', velocity1)
        return velocity1

    def calculatePosition(self, velocity):
        '''
        calculates the position of the first particle
        '''
        position = (velocity*t)
        print('Position of particle is', position)
        return position

    def calculatePosition1(self,velocity1):
        '''
        calculates the position of the second particle
        '''
        position1 = (velocity1*t)
        print('Position of particle1 is', position1)
        return position1


    def update(self,other):
       # for x in range(0,1):
            self.position = position
            other.position = position1
            print(self.position)
            print(other.position)
            return self.position
            return other.position

    def repeat(self,other):
        for x in range(0,5):
            force = p.calculateForce(p1) 
            acceleration = p.calculateAcceleration(force)
            acceleration1 = p1.calculateAcceleration1(force)
            velocity = p.calculateVelocity(acceleration)
            velocity1 = p1.calculateVelocity1(acceleration1)
            position = p.calculatePosition(velocity)
            position1 = p1.calculatePosition1(velocity1)
            p.update(p1)

p = Particle(10) # gives first particle a mass of 10
p1 = Particle(20) # gives second particle a mass of 20      
p1.position[0] = 5 # x coordinate of second particle is 5
p1.position[1] = 5 # y coordinate of second particle is 5  
print(p1.position)

force = p.calculateForce(p1)
acceleration = p.calculateAcceleration(force)
acceleration1 = p1.calculateAcceleration1(force)
velocity = p.calculateVelocity(acceleration)
velocity1 = p1.calculateVelocity1(acceleration1)
position = p.calculatePosition(velocity)
position1 = p1.calculatePosition1(velocity1)
p.update(p1)
p.repeat(p1)

最佳答案

更新中无法访问您的第二个返回:

 def update(self,other):
       # for x in range(0,1):
            self.position = position
            other.position = position1
            print(self.position)
            print(other.position)
            return self.position
            return other.position <- unreachable

如果您想返回两个值,您可以返回两个值的元组并适当更改代码以使用这些值:

 return self.position, other.position

如果应返回其中之一,那么您需要添加一些逻辑。

关于python - 模拟重力作用下的两个粒子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30698701/

相关文章:

python - 机器学习的扩展功能

python - 使用 uwsgi 运行时无法访问 .env 文件

python - 使用 PyQt5 设置 IPython Qtconsole

actionscript-3 - 用于 Nape 中船舶和子弹的 sensorGroup 和 sensorMask 组合

ios - 有没有办法在Box2D中绘制凹多边形并检测与其他形状的碰撞

javascript - 基于增量切换 boolean 值

javascript - AJAX 请求复制整个页面

physics-engine - 编写物理引擎需要什么?

java - 2D 圆低速碰撞导致重叠

java - 我从哪里开始编写/使用 3D 物理模拟引擎?