Adobe Director Online
 
Poonam Yadav

Sridevi Aishwariya .G
Director Developer

Physics for Beginners - Part 2

 
Download the source here
 

Once a Rigid object is created in the Physics world, we need to add functions to move the rigidbody in the scene, the 3D world uses the transform functions to move the models, while the physics world uses the following physics methods to imitate the movement of the model in the realworld.

  • applyForce()
  • applyLinearImpulse()
  • applyAngularImpulse()
  • applyTorque()
  • attemptMoveTo()

:) well, its time to pick up those long forgotten physics books yet another time. Well In short Force is force, impulse is the force that is applied on a object for a given duration of time, Torque is the rotational equivalent of force, the direction of rotation is given by the right hand thumb rule. I personally prefer the trial and error method, rather then look up equations.

The steps to create the movie are.

  1. Initialize the physics world
  2. Position the camera
  3. Create the models and create a rigid body for the same in the physics world
  4. Do the stage Lighting
  5. Move the RigidBody

Since we have already covered how to initialize the physics world and create the rigid body in Part 1, we will start with how to position the camera.

The camera is used to specify the point (vector) from which the 3D world is viewed.

tCamera = tMember.camera[1]

tCamera.transform.position = vector (-3, 35, 250)

The first statement sets the camera of the sprite ‘tMember’ to camera 1 and hence overrides the default camera. The second statement places the camera in the vector position (-3, 35, 200) to view the scene.

tMember.deleteLight(1)

tMember.newLight("Light01",#point)

tMember.light("Light01").transform.position = m_ground.transform.position

The above statements create a new Light source for the scene and places it on the ground. There are various types of lights sources.

#ambient is a generalized light in the 3D world.
#directional is a light from a specific direction.
#point is a light source like a light bulb.
#spot is a spotlight effect.

We will cover Lights and Cameras in depth some other time.

The applyForce() method is used to move the model

rb = gDirPhyz.getrigidbody("Sphere")

if _key.keyPressed(123) then -- left button      

  rb.applyForce(vector(-10,0,0))

end if

When the left button is clicked a slight force is applied in the negative x direction, so that the object appears to move in the left direction. Similarly, we can apply torque and impulse to the object.

To create the movie, import an Image and label it “MyImage” to create texture for the 3D models. Insert a physics cast member (Insert-> Media Element-> Physics) and name is “physX”. Insert a 3D scenes (Insert-> Media Element-> Shockwave 3D), name the cast member as “3D world”.

Drag and Drop the “3D world” cast member on the stage, and then drag and drop the physics cast member on the 3D scene. Left click on the 3D scene and add the following code

property pTimeStep

property pSubSteps

global gDirPhyz

 

on beginSprite me

 

  tSprite = sprite(me.spriteNum)

  tMember  = tSprite.member

  tMember.resetWorld()

 

  tCamera = tMember.camera[1]

  tCamera.transform.position = vector (-3, 35, 250)

 

  pTimeStepMode = #equal

  pTimeStep = 0.33 

  pSubSteps = 5

  gDirPhyz = member("physX")

  gDirPhyz.Init(member(tMember), vector(1,1,1),#equal, pTimeStep, pSubSteps )    

  gDirPhyz.gravity = vector(0,-10,0)

 

  -- create the sphere model

 

  mr_sphere          = tMember.newModelResource("Sphere", #sphere)

  mr_sphere.radius   = 10

  m_sphere           = tMember.newModel("Sphere", mr_sphere)

  m_sphere.transform.position = vector(0,15,0)

 

  -- add new shader and texture to the ball model

 

  tMember.newTexture("Texture01",#fromImageObject,member("MyImage").image)

  newShader01 = tMember.newShader("newPainter01",#standard)

  newShader01.texture = tMember.texture("Texture01")

  tMember.shader("newPainter01").texturemode = #wrapSpherical

  m_sphere.shader = newShader01

 

  -- create a rigid body for the sphereModel

 

  m_sphere.addmodifier(#meshdeform)

  rb_sphere = gDirPhyz.createRigidBody(m_sphere.name,m_sphere.name,#sphere,#dynamic)

  rb_sphere.restitution = 0

  rb_sphere.mass = 50

 

  -- create the ground

 

  mr_ground          = tMember.newModelResource("Ground", #box)

  mr_ground.width    = 300

  mr_ground.height   = 3

  mr_ground.length   = 300

  m_ground        = tMember.newModel("Ground", mr_ground)

 

  m_ground.transform.position = vector(0,0,0)

 

 

  -- add new shader and texture to the ball model

  tMember.newTexture("Texture02",#fromImageObject,member("MyImage").image)

  tMember.model("Ground").shader.texture= tMember.texture("Texture02")

 

  m_ground.addmodifier(#meshdeform)

  rb_ground = gDirPhyz.createRigidBody(m_ground.name,m_ground.name,#box,#static)

  rb_ground.restitution = 0

  rb_ground.mass = 100

 

  -- light the mesh 

  tMember.deleteLight(1)

  tMember.newLight("Light01",#point)

  tMember.light("Light01").transform.position = m_ground.transform.position

 

end

 

 

on endSprite me

  if gDirPhyz.isInitialized then

    gDirPhyz.destroy()

  end if   

end 

 

on exitFrame me  

 

  rb = gDirPhyz.getrigidbody("Sphere")

  if _key.keyPressed(123) then -- left button      

    rb.applyForce(vector(-10,0,0))

  end if

  if _key.keyPressed(124) then -- right button

    rb.applyForce(vector(10,0,0))

  end if

  if _key.keyPressed(126) then -- up button

    rb.applyForce(vector(0,0,-10))

  end if

  if _key.keyPressed(125) then -- down button

    rb.applyForce(vector(0,0,10))

  end if

 

  gDirPhyz.simulate(pTimeStep, pSubSteps) 

 

end

References:
[1] 3D samples from coderecipe http://coderecipe.com/physics-samples/

Feedback:
If you have any questions or comments concerning this article, please send me a message at srideviaishwariya@gmail.com

 
spacer image
This is not an official site from Adobe. If you need any clarifications, please mail me at info@adobedirectoronline.com