Python PhysicsSimulator Example

说明

python physicssimulator示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Python

命名空间/包名称: physicsphysics_simulator

示例#1
文件: main.py项目: tarcisiofischer/newt_examples

from gui.paintables.paintable_connector import PaintableConnector
from gui.paintables.paintable_polyhedron import PaintablePolyhedron
from physics.force_generator.spring_force_generator import SpringForceGenerator
from physics.particle import Particle
from physics.physics_simulator import PhysicsSimulator
import numpy as np


# As in reality, infinite equals one million :)
INFINITE = 1000000.0

if __name__ == '__main__':
    main_window = MainWindow("Equivalent Springs Example", 400, 400)
    main_window.setInputListener(GameLikeInputListener(main_window))

    simulator = PhysicsSimulator()

    #===========================================================================

    k0 = 3.5
    k1 = 0.5

    particle1 = Particle(mass=1.0)
    particle2 = Particle(mass=INFINITE)
    simulator.addBody(particle1)
    simulator.addBody(particle2)
    particle1_painter = PaintablePolyhedron(particle1.geometry)
    particle2_painter = PaintablePolyhedron(particle2.geometry)
    connector1_2_painter = PaintableConnector(particle1, particle2)
    main_window.addObject(particle1_painter)
    main_window.addObject(particle2_painter)

示例#2
文件: main.py项目: tarcisiofischer/newt_examples

from example_interactive_water_particles.water_particle import WaterParticle
from gui.input_listeners.game_like_input_listener import GameLikeInputListener
from gui.main_window import MainWindow
from gui.paintables.paintable_polyhedron import PaintablePolyhedron
from physics.force_generator.gravity_force_generator import GravityForceGenerator
from physics.physics_simulator import PhysicsSimulator


# Controls the number of simulated particles.
N_PARTICLES = 500


if __name__ == '__main__':
    main_window = MainWindow("Water Particles Example", 400, 400)
    main_window.setInputListener(GameLikeInputListener(main_window))

    simulator = PhysicsSimulator()
    for i in xrange(N_PARTICLES):
        # Add particle to simulator
        particle = WaterParticle()
        simulator.addForceGenerator(GravityForceGenerator(particle))
        simulator.addBody(particle)
        # Add particle to scene
        particle_painter = PaintablePolyhedron(particle.geometry)
        main_window.addObject(particle_painter)

    main_window.addBeforeDrawSceneCallback(simulator.update)
    main_window.mainLoop()

示例#3
文件: main.py项目: tarcisiofischer/newt_examples

import numpy as np


def createParticle(mass, color):
    particle = Particle(mass)
    particle.setGeometry(readPly('../objects/sphere.ply'))
    particle.geometry.scale(np.repeat(mass / 2.0, 3))
    particle.geometry.setColor(color)
    return particle


if __name__ == '__main__':
    main_window = MainWindow("Spring Example", 400, 400)
    main_window.setInputListener(GameLikeInputListener(main_window))

    simulator = PhysicsSimulator()

    particle1 = createParticle(0.1, np.array([0.6, 0.2, 0.6]))
    particle2 = createParticle(0.5, np.array([0.2, 0.6, 0.6]))
    particle3 = createParticle(0.2, np.array([0.8, 0.2, 0.2]))

    simulator.addBody(particle1)
    simulator.addBody(particle2)
    simulator.addBody(particle3)

    main_window.addObject(PaintablePolyhedron(particle1.geometry))
    main_window.addObject(PaintablePolyhedron(particle2.geometry))
    main_window.addObject(PaintablePolyhedron(particle3.geometry))
    main_window.addObject(PaintableConnector(particle1, particle2))
    main_window.addObject(PaintableConnector(particle2, particle3))
    main_window.addObject(PaintableConnector(particle1, particle3))

展开阅读全文