File: tutorial1.py

package info (click to toggle)
ode 2%3A0.16-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,088 kB
  • sloc: cpp: 89,737; ansic: 14,658; sh: 4,346; makefile: 761; python: 330
file content (49 lines) | stat: -rwxr-xr-x 1,234 bytes parent folder | download | duplicates (5)
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