summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/bindings/python/demos/tutorial1.py
blob: c4266d47f54804e0ecb5acb236f120f1eef46b74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python

# http://pyode.sourceforge.net/tutorials/tutorial1.html

# pyODE example 1: Getting started

# modified by Gideon Klompje (removed literals and using
# 'ode.Mass.setSphereTotal' instead of 'ode.Mass.setSphere')

import ode

# Simulation constants
GRAVITY = (0, -9.81, 0)

SPHERE_RADIUS = 0.05
SPHERE_MASS = 1.0
SPHERE_START_POS = (0, 2, 0)
SPHERE_FORCE = (0, 200, 0) # Initial force to apply to the sphere

TIME_STEP = 0.04
TIME_STOP = 2.0 # When to stop the simulation

# Create a world object
world = ode.World()
world.setGravity(GRAVITY)

# Create a spherical body inside the world
body = ode.Body(world)
mass = ode.Mass()
mass.setSphereTotal(SPHERE_MASS, SPHERE_RADIUS)
body.setMass(mass)

body.setPosition(SPHERE_START_POS)
body.addForce(SPHERE_FORCE)

# Do the simulation...
if __name__ == "__main__":
    total_time = 0.0
    while total_time < TIME_STOP:
        # output the body's position and velocity
        x, y, z = body.getPosition()
        u, v, w = body.getLinearVel()
        print "%1.2fsec: pos=(%6.3f, %6.3f, %6.3f)  vel=(%6.3f, %6.3f, %6.3f)" % \
            (total_time, x, y, z, u, v, w)

        # advance the simulation
        world.step(TIME_STEP)
        total_time += TIME_STEP