Tutorial 04 - Joints
This tutorial shows how to use joints. At first the source code:
SuperStrict
Import pyroplay.ppisaac
Graphics 800 , 600
' this is only because of the ppIsaac logo that uses black color and I don't wat it to be transparent
SetMaskColor(255 , 255 , 0)
' initialise ppIsaac
ppIsaac.Start()
ppIsaac.gravity.y = 50000
ppIsaac.SetWorldSize(-800,-600,800,600)
ppIsaac.SetSolverModel(0)
ppIsaac.SetGlobalCollisionDetection(False)
' create box objects
Local ground:ppBox = ppBox.Create(394 , 415 , 541 , 46 , 0)
ground.LoadTexture("media/brick.png")
ground.SetAngle(15)
Local box01:ppBox = ppBox .Create(300,300,50,50,0)
box01.LoadTexture("media/box_texture.png")
Local box02:ppBox = ppBox .Create(400,50,40,40,5)
box02.LoadTexture("media/box_texture.png")
'create oval objects
Local oval01:ppOval = ppOval.Create(500,50, 20,20, 0)
oval01.LoadTexture("media/circle_texture.png")
Local oval02:ppOval = ppOval.Create(500,150, 200,20, 5)
oval02.LoadTexture("media/circle_texture.png")
Local oval03:ppOval = ppOval.Create(500,200, 200,20, 5)
oval03.LoadTexture("media/circle_texture.png")
'create hinge joints
Local joint01:ppHingeJoint = box02.CreateHingeJoint(box01, box01.posX, box01.posY)
joint01.SetStiffness(1.0)
Local joint02:ppHingeJoint = oval01.CreateHingeJoint(oval02, oval01.posX, oval01.posY)
Local joint03:ppHingeJoint = oval02.CreateHingeJoint(oval03, oval02.posX, oval02.posY)
joint02.SetStiffness(0.1)
joint03.SetStiffness(1.0)
joint01.SetVisible(True)
joint02.SetVisible(True)
joint03.SetVisible(True)
joint03.SetCollisionState(True)
Local img_background:TImage = LoadImage("media/background01.png")
Local rot#
While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
Cls
ppIsaac.Update()
If MouseHit(2) Then
ppIsaac.AddExplosion(MouseX() , MouseY() , 1000000000)
EndIf
SetColor 255,255,255
DrawImage img_background, 0,0
ppIsaac.Draw()
DrawText "fps:" + ppIsaac.GetFPS() , 10 , 10
DrawText "Click right mouse button near the jointed box!" , 10 , 30
Flip
Wend
ppIsaac.Stop()
End
Now lets get through the code. This initialises ppIsaac:
Now this initialises ppIsaac:
' initialise ppIsaac
ppIsaac.Start()
ppIsaac.gravity.y = 50000
ppIsaac.SetWorldSize(-800,-600,800,600)
The following line tells ppIsaac to use the most precise calculation solver model. The higher the int parameter you set, the less precise and the faster are the calculation. For gaming purposes a SolverModel of 2 should be enough. But today I feel like to be very precise...
ppIsaac.SetSolverModel(0)
Lets set collision detection off, we don't need here:
ppIsaac.SetGlobalCollisionDetection(False)
Create some objects to play with:
' create box objects
Local ground:ppBox = ppBox.Create(394 , 415 , 541 , 46 , 0)
ground.LoadTexture("media/brick.png")
ground.SetAngle(15)
Local box01:ppBox = ppBox .Create(300,300,50,50,0)
box01.LoadTexture("media/box_texture.png")
Local box02:ppBox = ppBox .Create(400,50,40,40,5)
box02.LoadTexture("media/box_texture.png")
'create oval objects
Local oval01:ppOval = ppOval.Create(500,50, 20,20, 0)
oval01.LoadTexture("media/circle_texture.png")
Local oval02:ppOval = ppOval.Create(500,150, 200,20, 5)
oval02.LoadTexture("media/circle_texture.png")
Local oval03:ppOval = ppOval.Create(500,200, 200,20, 5)
oval03.LoadTexture("media/circle_texture.png")
The above part should be clear to you and brings nothing new. Now we create some joints:
'create hinge joint
Local joint02:ppHingeJoint = oval01.CreateHingeJoint(oval02, oval01.posX, oval01.posY)
Local joint03:ppHingeJoint = oval02.CreateHingeJoint(oval03, oval02.posX, oval02.posY)
joint02.SetStiffness(0.1)
joint03.SetStiffness(1.0)
joint01.SetVisible(True)
joint02.SetVisible(True)
joint03.SetVisible(True)
As you can see, every object has got a CreateHingeJoint() method to create one. The first parameter is the object to join with. As second and third parameter you set the x and y position of the hinge. The method returns a ppHingeJoint object.
I think the SetStiffnes() method of the joint is self-explanatory. The float parameter should be between 0.0 and 1.0.
Per default joints are not visible. Use SetVisible(True) to let the joints be drawned.
Now look at the following line:
joint03.SetCollisionState(True)
Per default bodies that are connected with a joint do not collide. SetCollisionState(True) sets the collision between jointed bodies on.
Now you should have an understanding of joints in ppIssac. Right now there is only one type of joints. It's planned to support more joint types in future versions of ppIsaac.
Links:
ppIsaac Product Page
Tutorial 01 - Hello Isaac!
Tutorial 02 - Static and Dynamic Objects
Tutorial 03 - Texture and Materials
Tutorial 05 - Collision Detection and Material Collision