Creating an Insect with FX, physics and joints

In this tutorial I'll show you how to create an insect with movement something like a mosquito. I'll be using a looping FX to create the movement. In order to keep the insect right side up, there will be a weighted object connected under the insect with a revolute joint (see joint config).

Firstly, the animation frames of a robotic insect with beating wings:

The physics need to be set in the config to a rough standard:

[Physics]
Gravity         = (0, 1000)   

Create the insect with:

[InsectObject]
AnimationSet    = InsectAnimationSet
Body            = InsectBody
ChildList       = InsectWeightObject
ChildJointList  = InsectBodyRevoluteJoint
FXList          = InsectMovementFX
Scale           = 0.5

Next the animation so that the insect beats its wings over and over:

[InsectAnimationSet]
Texture         = insect-drone.png
FrameSize       = (157, 122)
KeyDuration     = 0.01
StartAnim       = Fly

At this point, we can create an instance of the insect in code so you can see it animating:

orxObject_CreateFromConfig("InsectObject");

Next step is to give the insect some movement. We'll use a speed FX for this to set random directions:

[InsectMovementFX]
SlotList        = InsectMovementFXSlot
Loop            = true
 
[InsectMovementFXSlot]
Type            = speed
Curve           = linear
StartTime       = 1
EndTime         = 2
EndValue        = (-100, -1550) ~ (100, -1550)
Period          = 4

Notice in the FX, it is looped so that a movement change occurs every 2 seconds. The left and right movement direction will be random each time, but the up speed will be -1500 every 2 seconds. This creates a little “tug of war” effect against gravity which helps give an insect motion effect.

Next we'll attach an child object as a weight. The weight will be connected to the parent insect object using a revolute joint (see joint config):

[InsectWeightObject]
Body            = InsectWeightBody
 
[InsectWeightBody]
PartList        = InsectWeightBodyPart
Dynamic         = true
HighSpeed       = true
AngularDamping  = 0.0
Inertia         = 0.0
 
[InsectWeightBodyPart]
Type            = box
TopLeft         = (0, 0)
BottomRight     = (50, 50)
Solid           = false
Density         = 1.0
 
[InsectBodyRevoluteJoint]
Type            = revolute
ParentAnchor    = (78.5, 200) ;Keep the anchor a good distance below the insect object
ChildAnchor     = (0, 0)

Notice the weight body is dynamic so it will be affected by gravity. The physics will be pushing down on the weight, but the speed movements of the insect will compensate and create more randomness to the movement. The revolute joint will keep the weighted object at the bottom, eventually straightening the insect if it is upside down.

If the insect flies off the screen, we may never see it again. So perhaps a border around the screen is in order. That will keep the insect on the screen:

[Border]
Body            = BorderBody
Position        = (-400, -300) ;top-left of the border area
 
[BorderBody]
PartList        = BorderBodyPartTop # BorderBodyPartRight # BorderBodyPartBottom # BorderBodyPartLeft
FixedRotation   = true
 
[BorderBodyDefaults]
Type            = box
SelfFlags       = border
CheckMask       = insect
Solid           = true
 
[BorderBodyPartTop@BorderBodyDefaults]
TopLeft         = (0, 0)
BottomRight     = (800, 20)
 
[BorderBodyPartLeft@BorderBodyDefaults]
TopLeft         = (0, 0)
BottomRight     = (20, 600)
 
[BorderBodyPartRight@BorderBodyDefaults]
TopLeft         = (780, 0)
BottomRight     = (800, 600)
 
[BorderBodyPartBottom@BorderBodyDefaults]
TopLeft         = (0, 580)
BottomRight     = (800, 600) 

Create an instance of the border object with:

orxObject_CreateFromConfig("Border");

And the insect will need a body too in order to collide with the border:

[InsectBody]
PartList        = InsectBodyPart
Dynamic         = true
HighSpeed       = true
AngularDamping  = 0.0
Inertia         = 0.0
CustomGravity   = (0, 0)
 
[InsectBodyPart]
Type            = box
TopLeft         = (0, 31)
BottomRight     = (157, 122)
SelfFlags       = insect
CheckMask       = border
Restitution     = 0.75
Friction        = 0.9
Density         = 1.0
Solid           = true

And run that up. The insect should be fluttering nicely around the place. You can run with ShowDebug = true in the [Physics] section, and can check how the weight object is interacting with the insect object.