CodeRecipe( );

one stop for all game coders..

Follow us on Twitter
Home
Spring constraint Print E-mail
User Rating: / 2
PoorBest 
Written by CR Team   
Saturday, 18 April 2009 00:00

What is a spring ?

A spring constraint in Director is a constraint similar to an Angular joint , the only difference being that , in a spring constraint the constraint between 2 rigidbodies can be interchanged dynamically.

As in , during program execution one can change the constraint between rbA<->rbB to say rbA<->rbC .

Apart from this and the change in method to create a spring , no significant difference was found. Please refer Angular Joint for the diagram.

spring_constraint

Syntax and usage:

createSpring()

1
createSpring(ConstraintDesc desc,symbol forceExertionMode ,float restLength) 

ConstraintDesc - Required. Reference to the constraint descriptor object.

forceExertionMode - Takes the following symbol values.

#kDuringCompression - Restoring force will be applied only when the actual length is less than the rest length between objectA and objectB.

#kDuringExpansion - Restoring force will be applied only when the actual length is more than the rest length between objectA and objectB.

#kBoth - Restoring force will be applied in both the above cases.

restLength - Required. Float value that specifies the rest length of the spring. The distance that need to be maintained between 2 rigid bodies.

Example code :

createSpring()
1
2
3
4
5
6
-- Spring constraint Descriptor between two rigid bodies' b1 and b2.
pocA = vector(6.0,0.0,0.0)
pocB = vector(8.0,0.0,0.0)
SpringDesc = ConstraintDesc("SpringDesc",b1,b2,pocA,pocB,500,1)
--Create a spring which has expansion and compression with a rest length of 5.
member("PhysicsWorld").createSpring(SpringDesc,#kboth,5.0)

Other helper functions/methods available in spring are:

getSpring() - Return the spring handler specified by the descriptor name.

getSpring()
1
2
--Lingo Syntax 
spring_handler = member("PhysicsWorld").getSpring("spring_desc_name")

getSprings() - This returns a list of all the springs in the physics world.

getSprings()
1
2
3
--Lingo Syntax 
lSprings = []
lSprings = member("PhysicsWorld").getSprings()

deleteSpring() - This deletes the specified string using the name of the spring.

deleteSpring()
1
2
3
4
5
6
7
--Lingo Syntax 
-- Spring constraint descriptor between two rigid bodies' b1 and b2.
SpringDesc = ConstraintDesc("SpringDesc",b1,b2,vector(6.0,0.0,0.0),vector(8.0,0.0,0.0),500,1)
--Create a spring which has expansion and compression with a rest length of 5.
member("PhysicsWorld").createSpring(SpringDesc,#kboth,5.0)
--Deletes the spring
member("PhysicsWorld").deleteSpring("SpringDesc")