physics

Description

The physics system used in fluxus is based on the ode library, which allows you to add physical properties to objects and set them in motion. Since ODE is designed for rigid-body simulations, structures are described in terms of objects, joints and forces. A much more comprehensive explanation of these concepts can be found in the ODE documentation, which you have probably downloaded if you have compiled fluxus, or can be found at @url{http://ode.org/ode-docs.html} To help with debugging joints, try calling (render-physics) every frame, which will render locators showing you positions and axes of joints that have positional information.

(collisions on/off-number)

Returns void

Enables or disables collision detection. Defaults to off.

Example

 (collisions 1)

(ground-plane plane-vector offset-number)

Returns void

Create an infinite passive plane for use as the 'ground'

Example

 (ground-plane (vector 0 1 0) 0)

(active-box primitiveid-number)

Returns void

Enable the object to be acted upon by the physics system, using a box as the bounding volume. As an active object, it will be transformed by ode. Note: rotations only work correctly if you specify your transforms scale first, then rotate (translate doesn't matter) basically, ode can't deal with shearing transforms.

Example

 (define mycube (build-cube))
 (active-box mycube)

(active-cylinder primitiveid-number)

Returns void

Enable the object to be acted upon by the physics system, using a cylinder as the bounding volume. As an active object, it will be transformed by ode. Note: rotations only work correctly if you specify your transforms scale first, then rotate (translate doesn't matter) basically, ode can't deal with shearing transforms.

Example

 (define mycube (build-cube))
 (active-cylinder mycube)

(active-sphere primitiveid-number)

Returns void

Enable the object to be acted upon by the physics system, using a sphere as the bounding volume. As an active object, it will be transformed by ode. Note: rotations only work correctly if you specify your transforms scale first, then rotate (translate doesn't matter) basically, ode can't deal with shearing transforms.

Example

 (define mycube (build-cube))
 (active-sphere mycube)

(active-mesh primitiveid-number)

Returns void

Enable the object to be acted upon by the physics system, using the mesh as the collision volume. This function only works on indexed, triangle-list poly primitives. As an active object, it will be transformed by ode. Note: rotations only work correctly if you specify your transforms scale first, then rotate (translate doesn't matter) basically, ode can't deal with shearing transforms.

Example

 (define myshape (load-primitive "bot.obj"))
 (active-mesh myshape)

(passive-box primitiveid-number)

Returns void

Enable the object to be acted upon by the physics system, using a box as the bounding volume. As a passive object, active objects will collide with it, but it will not be transformed. Note: rotations only work correctly if you specify your transforms scale first, then rotate (translate doesn't matter) basically, ode can't deal with shearing transforms.

Example

 (define mycube (build-cube))
 (passive-box mycube)

(passive-cylinder primitiveid-number)

Returns void

Enable the object to be acted upon by the physics system, using a cylinder as the bounding volume. As a passive object, active objects will collide with it, but it will not be transformed. Note: rotations only work correctly if you specify your transforms scale first, then rotate (translate doesn't matter) basically, ode can't deal with shearing transforms.

Example

 (define mycube (build-cube))
 (passive-cylinder mycube)

(passive-sphere primitiveid-number)

Returns void

Enable the object to be acted upon by the physics system, using a sphere as the bounding volume. As a passive object, active objects will collide with it, but it will not be transformed. Note: rotations only work correctly if you specify your transforms scale first, then rotate (translate doesn't matter) basically, ode can't deal with shearing transforms.

Example

 (define mycube (build-cube))
 (passive-sphere mycube)

(passive-mesh primitiveid-number)

Returns void

Enable the object to be acted upon by the physics system, using the mesh as the collision volume. This function only works on indexed, triangle-list poly primitives. As a passive object, active objects will collide with it, but it will not be transformed. Note: rotations only work correctly if you specify your transforms scale first, then rotate (translate doesn't matter) basically, ode can't deal with shearing transforms.

Example

 (define myshape (load-primitive "bot.obj"))
 (passive-mesh myshape)

(physics-remove primitiveid-number)

Returns void

Remove the object from the physics system.

Example

 (define mycube (build-cube))
 (active-box mycube)
 (physics-remove mycube)

(surface-params slip1-number slip2-number softerp-number softcfm-number)

Returns void

Sets some global surface attributes that affect friction and bouncyness. see section 7.3.7 of the ODE docs for an explanation of these parameters

Example

 (surface-params 0.1 0.1 0.1 0.1)

(build-balljoint primitiveid-number primitiveid-number axis-vector)

Returns void

Creates a balljoint to connect two objects (see the ode docs for a detailed description of the differences between the joint types). ODE considers joints to be a constraint that is enforced between two objects. When creating a joint, it is important to have the two primitives being joined in the desired positions before creating the joint. Joints can be created, modified and indexed in a similar way to other primitives.

Example

 (clear)
 (ground-plane (vector 0 1 0) -1)
 (collisions 1)
 
 (define shape1 (with-state 
         (translate (vector -1 0 0))
         (build-cube)))
 (active-box shape1)
 
 (define shape2 (with-state 
         (translate (vector 1 0 0))
         (build-cube)))
 (active-box shape2)
 
 (build-balljoint shape1 shape2 (vector 0 0 0)) 
 (kick shape1 (vector 0 2 0))
 
 (set-physics-debug #t)

(build-fixedjoint primitiveid-number)

Returns void

Creates a joint to connect an object to the global environment. This locks the object in place.

Example

 (clear)
 (define shape1 (with-state 
         (translate (vector 0 1 0))
         (build-cube)))
 (active-box shape1)
 
 (build-fixedjoint shape1) ; not very exciting... 

(build-hingejoint primitiveid1-number primitiveid2-number anchor-vector axis-vector)

Returns hingeid-number

Creates a ball joint to connect two objects (see the ode docs for a detailed description of the differences between the joint types). ODE considers joints to be a constraint that is enforced between two objects. When creating a joint, it is important to have the two primitives being joined in the desired positions before creating the joint. Joints can be created, modified and indexed in a similar way to other primitives.

Example

 (clear)
 (ground-plane (vector 0 1 0) -1)
 (collisions 1)
 
 (define shape1 (with-state 
         (translate (vector -1 0 0))
         (build-cube)))
 (active-box shape1)
 
 (define shape2 (with-state 
         (translate (vector 1 0 0))
         (build-cube)))
 (active-box shape2)
 
 (build-hingejoint shape1 shape2 (vector 0 0 0) (vector 0 0 1)) 
 (kick shape1 (vector 0 2 0))
 
 (set-physics-debug #t)

(build-sliderjoint primitiveid1-number primitiveid2-number axis-vector)

Returns hingeid-number

Creates a slider joint to connect two objects (see the ode docs for a detailed description of the differences between the joint types). ODE considers joints to be a constraint that is enforced between two objects. When creating a joint, it is important to have the two primitives being joined in the desired positions before creating the joint. Joints can be created, modified and indexed in a similar way to other primitives.

Example

 (clear)
 (ground-plane (vector 0 1 0) -1)
 (collisions 1)
 
 (define shape1 (with-state 
         (translate (vector -1 0 0))
         (build-cube)))
 (active-box shape1)
 
 (define shape2 (with-state 
         (translate (vector 1 0 0))
         (build-cube)))
 (active-box shape2)
 
 (build-sliderjoint shape1 shape2 (vector 1 0 0)) 
 (kick shape1 (vector 0 2 0))
 
 (set-physics-debug #t)

(build-hinge2joint primitiveid1-number primitiveid2-number anchor-vector axis1-vector axis2-vector)

Returns hingeid-number

Creates a hinge2 joint to connect two objects (see the ode docs for a detailed description of the differences between the joint types). ODE considers joints to be a constraint that is enforced between two objects. When creating a joint, it is important to have the two primitives being joined in the desired positions before creating the joint. Joints can be created, modified and indexed in a similar way to other primitives.

Example

 (clear)
 (ground-plane (vector 0 1 0) -1)
 (collisions 1)
 
 (define shape1 (with-state 
         (translate (vector -1 0 0))
         (build-cube)))
 (active-box shape1)
 
 (define shape2 (with-state 
         (translate (vector 1 0 0))
         (build-cube)))
 (active-box shape2)
 
 (build-hinge2joint shape1 shape2 (vector 0 0 0) (vector 1 0 0) (vector 0 1 0)) 
 (kick shape1 (vector 0 2 0))
 
 (set-physics-debug #t)

(build-amotorjoint primitiveid1-number primitiveid2-number axis-vector)

Returns hingeid-number

Creates a angular motor joint to connect two objects (see the ode docs for a detailed description of the differences between the joint types). ODE considers joints to be a constraint that is enforced between two objects. When creating a joint, it is important to have the two primitives being joined in the desired positions before creating the joint. Joints can be created, modified and indexed in a similar way to other primitives.

Example

 (clear)
 (ground-plane (vector 0 1 0) -1)
 (collisions 1)
 
 (define shape1 (with-state 
         (translate (vector -1 0 0))
         (build-cube)))
 (active-box shape1)
 
 (define shape2 (with-state 
         (translate (vector 1 0 0))
         (build-cube)))
 (active-box shape2)
 
 (build-amotorjoint shape1 shape2 (vector 1 0 0)) 
 (kick shape1 (vector 0 2 0))
 
 (set-physics-debug #t)

(joint-param jointid-number param-string value-number)

Returns hingeid-number

Sets the joint parameter for a joint where param is one of the following: "HiStop", "Vel", "FMax", "FudgeFactor", "Bounce", "CFM", "StopERP", "StopCFM","SuspensionERP", "SuspensionCFM", "Vel2", "FMax2". see section 7.5.1 of the ODE docs for an explanation of each of these parameters, and which joint types they apply to.

Example

 (clear)
 (ground-plane (vector 0 1 0) -1)
 (collisions 1)
 
 (define shape1 (with-state 
         (translate (vector -1 0 0))
         (build-cube)))
 (active-box shape1)
 
 (define shape2 (with-state 
         (translate (vector 1 0 0))
         (build-cube)))
 (active-box shape2)
 
 (define j (build-hinge2joint shape1 shape2 (vector 0 0 0) (vector 1 0 0) (vector 0 1 0)))
 (joint-param j "Vel2" 0.1)
 (joint-param j "FMax2" 0.2)
 (joint-param j "LoStop" -0.75)
 (joint-param j "HiStop" 0.75)
 
 (set-physics-debug #t)

(joint-angle jointid-number angle-number vel-number)

Returns void

Set a new angle for this joint, with a given velocity taken to get there

Example

 (clear)
 (ground-plane (vector 0 1 0) -1)
 (collisions 1)
 
 (define shape1 (with-state 
         (translate (vector -1 0 0))
         (build-cube)))
 (active-box shape1)
 
 (define shape2 (with-state 
         (translate (vector 1 0 0))
         (build-cube)))
 (active-box shape2)
 
 (define j (build-hingejoint shape1 shape2 (vector 0 0 0) (vector 0 1 0)))
 (joint-param j "FMax" 20)
 (joint-param j "LoStop" -1)
 (joint-param j "HiStop" 1)
 
 (set-physics-debug #t)
 
 (define (animate)
     (joint-angle j 0.1 (* 5 (sin (time)))))

(joint-slide jointid-number force)

Returns void

Applies the given force in the slider's direction. That is, it applies a force with magnitude force, in the direction slider's axis, to body1, and with the same magnitude but opposite direction to body2.

Example

 (clear)
 (ground-plane (vector 0 1 0) -1)
 (collisions 1)
 
 (define shape1 (with-state 
         (translate (vector -1 0 0))
         (build-cube)))
 (active-box shape1)
 
 (define shape2 (with-state 
         (translate (vector 1 0 0))
         (build-cube)))
 (active-box shape2)
 
 (define j (build-sliderjoint shape1 shape2 (vector 1 0 0)))
 (joint-param j "FMax" 20)
 (joint-param j "LoStop" -1)
 (joint-param j "HiStop" 1)
 
 (set-physics-debug #t)
 
 (define (animate)
     (joint-slide j (* 5 (sin (time)))))

(set-max-physical max-number)

Returns void

Sets the maximum number of objects the physics system can deal with. When the max level has been reached the oldest objects are automatically destroyed.

Example

 (clear)
 (set-max-physical 200)
 
 (every-frame
     (with-state
     (translate (vector 0 5 0))
         (scale (srndvec))
         (colour (rndvec))
         (let ((ob (build-cube)))    
             (active-box ob)
             (kick ob (vmul (srndvec) 3))
             (twist ob (vmul (srndvec) 2)))))

(set-mass primitiveid-number mass-number)

Returns void

Sets the mass of an active object

Example

 (clear)
 (ground-plane (vector 0 1 0) 0)
 (collisions 1)
 (set-max-physical 20)
 
 ; not a great example, but these boxes will have 
 ; different mass, so behave a bit differently.
 
 (every-frame
     (when (> (rndf) 0.92)
         (with-state
             (translate (vector 0 5 0))
             (scale (vmul (rndvec) 5))
             (colour (rndvec))
             (let ((ob (build-cube)))    
                 (active-box ob)
                 (set-mass ob (* (rndf) 10))
                 (kick ob (vmul (srndvec) 3))
                 (twist ob (vmul (srndvec) 2))))))    

(gravity gravity-vector)

Returns void

Sets the strength and direction of gravity.

Example

 (clear)
 (ground-plane (vector 0 1 0) 0)
 (collisions 1)
 (set-max-physical 20)
 
 (every-frame
     (begin
         (gravity (vector 0 (sin (time)) 0)) ; change gravity! :)
         (when (> (rndf) 0.92)
             (with-state
                 (translate (vector 0 5 0))
                 (scale (rndvec))
                 (colour (rndvec))
                 (let ((ob (build-cube)))    
                     (active-box ob)
                     (kick ob (vmul (srndvec) 3))
                     (twist ob (vmul (srndvec) 2)))))))

(kick primitiveid-number kick-vector)

Returns void

Applies translation force to the object

Example

 (clear)
 (collisions 1)
 (set-max-physical 20)
 (gravity (vector 0 0 0))

 (every-frame
     (when (> (rndf) 0.92)
         (with-state
             (scale (rndvec))
             (colour (rndvec))
             (let ((ob (build-cube)))
                 (active-box ob)
                 (kick ob (vmul (srndvec) 3))
                 (twist ob (vmul (srndvec) 2))))))

(twist primitiveid-number spin-vector)

Returns void

Applies rotational force to the object

Example

 (clear)
 (collisions 1)
 (set-max-physical 20)
 (gravity (vector 0 0 0))

 (every-frame
     (when (> (rndf) 0.92)
         (with-state
             (scale (rndvec))
             (colour (rndvec))
             (let ((ob (build-cube)))
                 (active-box ob)
                 (kick ob (vmul (srndvec) 3))
                 (twist ob (vmul (srndvec) 2))))))

(add-force primitiveid-number force-vector)

Returns void

Add force to body.

Example

 (clear)
 (collisions 1)
 (for ([i (in-range 15)])
    (let* ([p (vmul (srndvec) 15)]
           [c (with-state
                    (translate p)
                    (build-cube))])
        (active-box c)
        (set-gravity-mode c #f)
        (add-force c (vmul p -10))))

(add-torque primitiveid-number torque-vector)

Returns void

Add torque to body.

Example

 (clear)
 (define c (build-cube))
 (active-box c)
 (add-torque c #(10 0 0))

(set-gravity-mode primitiveid-number mode-boolean)

Returns void

Set whether the body is influenced by the world's gravity or not.

Example

 (clear)
 (collisions 1)
 (define a (with-state
               (translate (vector -.95 5 0))
               (build-cube)))
 (define b (build-cube))
 (active-box a)
 (active-box b)
 (set-gravity-mode b #f)

(has-collided primitiveid-number)

Returns void

Returns true if the grabbed object collided in the last frame

Example

 (clear)
 (ground-plane (vector 0 1 0) 0)
 (collisions 1)
 (set-max-physical 20)
 
 (define ob (with-state
     (translate (vector 0 5 0))
     (build-cube)))
 
 (active-box ob)
 
 (every-frame
     (when (has-collided ob)
         (with-primitive ob
             (colour (rndvec)))))