CodeRecipe( );

one stop for all game coders..

Follow us on Twitter
Home
Setting up physics - Director Print E-mail
User Rating: / 4
PoorBest 
Written by CR Team   
Thursday, 18 June 2009 00:00

Creating a physics scene is Director is simple. Let us follow the 5 step process:

  1. Create a 3d world ( name it ‘3d_scene’ ) using menu Insert-> Media Element -> Shockwave 3d
  2. Create a physics world ( name it ‘physX’ )using menu Insert -> Media Element -> Physics
  3. Create a behavior script ( click on menu Window-> Script , set the type to behavior ) with the following code say physics_init ( just a random name i gave )
Init physics
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
property pDirPhyz
property pTimeStep,pSubSteps
 
on endSprite me
--if the physics world is initialized, then stop it on endsprite
if (pDirPhyz.isInitialized = 1) then
pDirPhyz.destroy()
end if
end
 
--step physics on every exit frame
 
on exitFrame me
-- Step the physics
pDirPhyz.simulate()
end exitFrame
 
on beginSprite me
--get a handler for physX
pDirPhyz = member("physX")
pTimeStep = 0.025
pSubSteps = 5
--hook the physics world to the 3d world
pDirPhyz.Init(member("3d_scene"), vector(1,1,1),#equal, pTimeStep, pSubSteps )
 
pDirPhyz.gravity = vector(0,-9.8,0)
pDirPhyz.contacttolerance = 0.02
pDirPhyz.friction = 0.8
pDirPhyz.lineardamping = 0.4
pDirPhyz.angulardamping = 0.4
pDirPhyz.restitution = 0.5
-- put "Initialized Physics"
end

 

  1. From the cast member window , drag and drop the behavior on to the 3d world placed on the stage ( place the 3d world on the stage if you have not already done so :) )
  2. To create a simple sphere in 3d and make it a rigidbody , use the following code, make a movie script:
startmovie
1
2
3
4
5
6
7
8
9
10
11
12
13
on startmovie
g3d_scene = member("3d_scene")
BallRes = g3d_scene.newModelResource("sphere", #sphere)
BallRes.radius = 10
BallRes.resolution = 6 --create a model out of the resource
Ball = g3d_scene.newModel("Ball" ,BallRes)
Ball.addmodifier(#meshdeform)
--create rigidbodies out of the spheres created above
physX = member("physX")
--read the doc/help in Director :)
rb_ball = physX.createRigidBody(Ball.name,Ball.name,#sphere,#dynamic)
rb_ball.mass = 100
end

 

Note:One cannot reset the 3d world before you stop the physics world/engine.
To do this you have to stop the physics world first.

Following code snippet should be the sequence to be followed in case.That should do it, run the movie and see the ball fall under gravity :).

 

Re-Initialize Physics
1
2
3
4
5
6
7
physX = member(”physX”)
physX .destroy()
g3d_scene = member(”3d_scene”)
g3d_scene.resetWorld()
-–Re-initialize the Physics world
physX.Init(member(”3d_scene”),vector(1,1,1),#equal,0.03,5)
physX.gravity = vector(0,0,-9.8)

 

Let us know if this helped…


 

CR Login

CodeRecipe Store

Twitter Feed

CR :) >>: RT @indiegamescom: Student game dev competition Dare to be Digital now accepting applicants http://t.co/Hf1phWJG