Vizard 8 » Reference » Physics » Forces
8.0

Forces

In addition to the world gravity, forces are applied in a variety of ways. The following are the ways to apply forces to a node.

applyForce

This is the most robust of all the following, and each of the following functions can be accomplished with some math and this function. The applyForce function takes five parameters: duration, dir, mode, pos, posMode.

 

duration

This is the time which the force is applied for.

 

dir and mode

The dir is a vector which specifies what direction the force vector points. The mode argument is used to specify if the force is applied in world coordinates (viz.ABS_GLOBAL) or local coordinates (viz.ABS_LOCAL).

 

pos and posMode

These are used to specify where the force is applied. When not specifying the center of the object this will create a torque on the object. The posMode argument once allows one to differentiate between the world and local coordinates for the position of where the force is applied.

 

To visualize how the force is applied when changing the position, imagine a handle drawn from the objects center of mass to the point specified. The force will be applied to the end of the handle in the direction specified previously.

 

The following demonstrates using applyForce to accelerate a box upward for 3 seconds.

box = viz.add('box.wrl',pos=[0,1.6,15],scale=[1,2,1])
box.collideBox()

box.applyForce(dir=[0,9,0],duration=3)

applyTorque

The applyTorque function works by passing in a vector which represents the magnitude of force to apply to each axis and the duration of which to apply the torque.

ball = viz.add('ball.wrl',pos=[0,.2,6]) # Add a ball
ballPhys = ball.collideSphere() # Enable physics on ball

ball.applyTorque([0,1,0],0) # Apply small torque for 1 second

In the above example the torque is applied along the world's 'y' axis. Remember that Vizard uses a left handed coordinate system so use the left-hand rule to calculate which direction an object will rotate. Like before to apply the torque in the ball's coordinates call applyTorque like so: ball.applyTorque([0,1,0],0,viz.ABS_LOCAL),  This example appears the same as before however this is only because the global 'y' and local 'y' are the same at this point.

addThruster

This function is similar to the applyForce function however it does not take a duration argument and the force is constantly applied while the truster is enabled.

 

In the following pressing space will activate and deactivate the thruster.

person = viz.add('vcc_female.cfg',pos=[0,1.6,10])
person.collideBox()
thrust = person.addThruster(mode=viz.ABS_LOCAL,force=[0,6,0])
# Press space to enable/disable thruster
vizact.onkeydown(' ',vizact.choice([thrust.disable,thrust.enable]))

addVelocity

This function allows one to set the desired velocity of an object and the physics engine will apply the necessary forces to maintain the velocity. In order to maintain a specific velocity this function will counteract other forces so this function typically shouldn't be used when other forces are acting on a node.

ball = viz.add('ball.wrl',pos=[0,1,10])
ball.collideSphere()

vel = ball.addVelocity(velocity=[0,.15,0],maxForce=1)

slider = viz.addSlider(pos=(0.5,0.1,0))
slider.set(0.5)

def UpdateVelocity(pos):
    vel.setVelocity([(pos*2.0 - 1.0)*5.0,.18,0])
vizact.onslider(slider,UpdateVelocity)
 

addSpring

The spring is somewhat different from the rest of these forces. Once a spring is created, the spring object's location can be moved which then causes the node to spring to that location. Forces are automatically applied to create the spring affect.

 

There are two main parameters to the spring which are the linearKd and the linearKs. LinearKd represents the force applied by the spring. The higher the value the more force the spring will exert on the object. LinearKs represents the dampening of the oscillation of the spring. The higher this value the slower the object will spring to the position. These values can be set when adding the spring or later with the setLinearKd and setLinearKs functions.

 

In the following pressing space will cause the ball to spring to the next location specified by pos.

ball = viz.add('white_ball.wrl',pos=[0,1.5,6])
ball.collideSphere()

spring = ball.addSpring(viz.LINK_POS,linearKd=20,linearKs=200)
spring.setLinearKd(5) # Change Kd
spring.setLinearKs(100) # Change Ks
spring.setPosition(ball.getPosition())

pos = ([0,1.5,6],[1,.5,6],[1,3,6],[-1,3,6],[-1,.5,6])
curr = 0
def move():
    global curr
    curr = (curr+1)%len(pos)
    spring.setPosition(pos[curr])   
vizact.onkeydown(' ',move) # Press space to move

Multiple Springs

 

More than one spring can be added to an object, and in this case the object will reach an equilibrium somewhere between the springs.

 

In the following press left and up to apply a force and watch the springs reposition the ball in the center.

ball = viz.add('white_ball.wrl',pos=[0,1.5,6])
ball.collideSphere()

spring1 = ball.addSpring(viz.LINK_POS,linearKd=2,linearKs=200)
spring1.setPosition([-1,1.5,6])
spring2 = ball.addSpring(viz.LINK_POS,linearKd=2,linearKs=200)
spring2.setPosition([1,1.5,6])
spring3 = ball.addSpring(viz.LINK_POS,linearKd=2,linearKs=200)
spring3.setPosition([0,3,6])
spring4 = ball.addSpring(viz.LINK_POS,linearKd=2,linearKs=200)
spring4.setPosition([0,0,6])

vizact.onkeydown(viz.KEY_UP,ball.applyForce,dir=[0,5,0])
vizact.onkeydown(viz.KEY_RIGHT,ball.applyForce,dir=[5,0,0])

addAttractor

This function simulates forces of attraction between two objects. Adding attractors for all objects to all other objects in the world allows for the simulation of planetary gravity.

 

The function takes two parameters, the strength and the exponent. The larger the strength the larger the force will be, and the larger the exponent the quicker the force will diminish as the distance between the objects grow.

 

The following is an example of a binary star system where they are both orbiting around their center of mass.

viz.phys.setGravity(0,0,0)

sat1 = viz.add('white_ball.wrl',pos=[-2,1.5,10],color=viz.YELLOW)
sat1.collideSphere()
sat1.setLinearDamping(0)

sat2 = viz.add('white_ball.wrl',pos=[2,1.5,10])
sat2.collideSphere()
sat2.setLinearDamping(0)

# Give initial forces
sat1.applyForce([0,.2,.3]);sat2.applyForce([0,-.2,-.3])

attractor = sat1.addAttractor(sat2,strength=1e5,exp=2)

Note

Setting the strength to a negative value pushes the objects away from each other which demonstrates repulsion.

See also

In this section:

Physics basics

Physics Shapes - In depth discussion of the collide methods.

Forces - More detail on applyForce and applyTorque.

Joints - In depth overview of the joint types.

Motors - In depth explanation of how each motor works with each joint.

Physics Command Table

Other sections:

Tutorial: Physics

Event Reference

Example scripts:

Basic physics

Tracking an object

Collision plane

Callbacks & complex shapes

Forces & materials