User Tools

Site Tools


tutorials:creating_an_insect_object

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.0, 1000.0, 0.0)   

Create the insect with:

[InsectObject]
Graphic         = InsectGraphic
AnimationSet    = InsectAnimationSetAll
Position        = (400, 300, -0.1)
Body            = InsectBody
ChildList       = InsectWeightObject
ChildJointList  = InsectBodyRevoluteJoint
FXList          = InsectMovementFX
Scale			= 0.5
 
[InsectGraphic]
Texture         = insect-drone.png
Pivot           = center
TextureSize     = (157, 122, 0)

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

[InsectFly1@InsectGraphic]
TextureOrigin   = (0, 0, 0)
TextureSize     = (157, 122, 0)
 
[InsectFly2@InsectGraphic]
TextureOrigin   = (157, 0, 0)
TextureSize     = (157, 122, 0)
 
[InsectFly3@InsectGraphic]
TextureOrigin   = (314, 0, 0)
TextureSize     = (157, 122, 0)
 
[InsectFly4@InsectGraphic]
TextureOrigin   = (471, 0, 0)
TextureSize     = (157, 122, 0)
 
[InsectFly5@InsectGraphic]
TextureOrigin   = (628, 0, 0)
TextureSize     = (157, 122, 0)
 
[InsectFly6@InsectGraphic]
TextureOrigin   = (0, 122, 0)
TextureSize     = (157, 122, 0)
 
[InsectFly7@InsectGraphic]
TextureOrigin   = (157, 122, 0)
TextureSize     = (157, 122, 0)
 
[InsectFly8@InsectGraphic]
TextureOrigin   = (314, 122, 0)
TextureSize     = (157, 122, 0)
 
[InsectFly9@InsectGraphic]
TextureOrigin   = (471, 122, 0)
TextureSize     = (157, 122, 0)
 
[InsectFly10@InsectGraphic]
TextureOrigin   = (628, 122, 0)
TextureSize     = (157, 122, 0)
 
 
[InsectFlyAnim]
DefaultKeyDuration = 0.01
KeyData1        = InsectFly1
KeyData2        = InsectFly2
KeyData3        = InsectFly3
KeyData4        = InsectFly4
KeyData5        = InsectFly5
KeyData6        = InsectFly6
KeyData7        = InsectFly7
KeyData8        = InsectFly8
KeyData9        = InsectFly9
KeyData10       = InsectFly10
 
 
[InsectAnimationSetAll]
AnimationList   = InsectFlyAnim
LinkList        = InsectFlyLoop
 
[InsectFlyLoop]
Source          = InsectFlyAnim
Destination     = InsectFlyAnim

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
StartValue      = (0, 0, 0)
EndValue        = (-100, -1550, 0) ~ (100, -1550, 0)
Period          = 4
Absolute        = false

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
Position        = (-20, 80, 0)
UseParentSpace  = true
UseRelativeSpeed= true
 
[InsectWeightBody]
PartList        = InsectWeightBodyPart
Dynamic         = true
FixedRotation   = false
HighSpeed       = true
AngularDamping  = 0.0
Inertia         = 0.0
 
[InsectWeightBodyPart]
Type            = box
TopLeft         = (0, 0,0)
BottomRight     = (50, 50, 0)
Restitution     = 0.75
Friction        = 0.9
Solid           = false
Density         = 1.0
 
[InsectBodyRevoluteJoint]
Type            = revolute
ParentAnchor    = (0,55,0)
ChildAnchor     = (0,-10,0)
Collide         = false

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.

With the joint, it has been set so that the weight object can pass thought the insect using Collide = false if they collide.

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 = (0, 0, 0)
 
[BorderBody]
PartList        = BorderBodyPartTop # BorderBodyPartRight # BorderBodyPartBottom # BorderBodyPartLeft
Dynamic         = false
FixedRotation   = true
 
[BorderBodyDefaults]
Type            = box
SelfFlags       = border
CheckMask       = insect
Solid           = true
 
[BorderBodyPartTop@BorderBodyDefaults]
TopLeft         = (0, 0, 0)
BottomRight     = (800, 20, 0)
 
[BorderBodyPartLeft@BorderBodyDefaults]
TopLeft         = (0, 0, 0)
BottomRight     = (20, 600, 0)
 
[BorderBodyPartRight@BorderBodyDefaults]
TopLeft         = (780, 0, 0)
BottomRight     = (800, 600, 0)
 
[BorderBodyPartBottom@BorderBodyDefaults]
TopLeft         = (0, 580, 0)
BottomRight     = (800, 600, 0) 

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
FixedRotation   = false
HighSpeed       = true
AngularDamping  = 0.0
Inertia         = 0.0
CustomGravity   = (0, 0, 0)
 
[InsectBodyPart]
Type            = box
TopLeft         = (-78.5,-31,0)
BottomRight     = (78.5, 61, 0)
SelfFlags       = insect
CheckMask       = border
Restitution     = 0.75
Friction        = 0.9
Solid           = true
Density         = 1.0

And run that up. The insect should be fluttering nicely around the place.

tutorials/creating_an_insect_object.txt · Last modified: 2016/12/09 12:00 (6 months ago) by sausage