This came up in another thread and I had such a good time playing with it, I had to share
// Project: Extended Pivot Demo
// Created: 2016-01-02
// Set window properties.
SetWindowTitle( "Extended Pivot Demo" )
SetWindowSize( 1024, 768, 0 )
// Set display properties.
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetPrintSize(20)
// Initialize some variables.
BlueCol = MakeColor(0,0,128)
LightBlueCol = MakeColor(150,150,255)
GreenCol = MakeColor(0,100,0)
LightGreenCol = MakeColor(0,200,0)
HeadNod$ = "DOWN"
ArmOne$ = "FORWARD"
ArmTwo$ = "BACK"
LegOne$ = "BACK"
LegTwo$ = "FORWARD"
ChosenObject = 1
Part$ as string[6]
Part$[1] = "Torso"
Part$[2] = "Head"
Part$[3] = "Right Arm"
Part$[4] = "Left Arm"
Part$[5] = "Right Leg"
Part$[6] = "Left Leg"
// Construct a background.
ClearScreen()
DrawBox(0,0,1024,400,BlueCol,BlueCol,LightBlueCol,LightBlueCol,1)
DrawBox(0,401,1025,768,GreenCol,GreenCol,LightGreenCol,LightGreenCol,1)
BackdropImg = GetImage(0,0,1025,768)
BackdropSpr = CreateSprite(BackdropImg)
SetGlobal3DDepth(1)
// Create the main object, the torso.
// The torso pivots on center y and z
// but pivots at the hips on the x axis,
// so move it up and fix its pivot point.
TorsoObj = CreateObjectBox(4,6,2)
SetObjectPosition(TorsoObj,0,3,0)
FixObjectPivot(TorsoObj)
// Create and attach an object for the head.
// The head will pivot at its center to nod
// or turn left and right. It should pivot
// at a "neck" object when tilting side-to-side,
// but that can be left for later.
HeadObj = CreateObjectBox(3,3,3)
SetObjectPosition(HeadObj,0,8,0)
FixObjectToObject(HeadObj,TorsoObj)
// Add an arm, move it down so that
// the pivot point is at the top of
// the arm, where it attaches to the
// shoulder. Fix the pivot, then fix
// the object to the torso, and move
// it into place at the shoulder.
ArmOneObj = CreateObjectBox(1,6,1)
SetObjectPosition(ArmOneObj,0,-3,0)
FixObjectPivot(ArmOneObj)
FixObjectToObject(ArmOneObj,TorsoObj)
SetObjectPosition(ArmOneObj,-2.8,6,0)
// Same for the second arm, only put
// it on the other shoulder.
ArmTwoObj = CreateObjectBox(1,6,1)
SetObjectPosition(ArmTwoObj,0,-3,0)
FixObjectPivot(ArmTwoObj)
FixObjectToObject(ArmTwoObj,TorsoObj)
SetObjectPosition(ArmTwoObj,2.8,6,0)
// And add a pair of legs!
LegOneObj = CreateObjectBox(1.8,6,2)
SetObjectPosition(LegOneObj,0,-3,0)
FixObjectPivot(LegOneObj)
FixObjectToObject(LegOneObj,TorsoObj)
SetObjectPosition(LegOneObj,-1.1,-0.2,0)
LegTwoObj = CreateObjectBox(1.8,6,2)
SetObjectPosition(LegTwoObj,0,-3,0)
FixObjectPivot(LegTwoObj)
FixObjectToObject(LegTwoObj,TorsoObj)
SetObjectPosition(LegTwoObj,1.1,-0.2,0)
// Add the pivot point marker.
XPivotObj = CreateObjectBox(100,.05,.05)
SetObjectColor(XPivotObj,255,0,0,0)
YPivotObj = CreateObjectBox(.05,100,.05)
SetObjectColor(YPivotObj,0,255,0,255)
ZPivotObj = CreateObjectBox(.05,.05,100)
SetObjectColor(ZPivotObj,0,0,255,255)
FixObjectToObject(YPivotObj,XPivotObj)
FixObjectToObject(ZPivotObj,XPivotObj)
// Attach it to the torso
FixObjectToObject(XPivotObj, ChosenObject + 100000)
// Move the torso down a bit on the screen.
SetObjectPosition(TorsoObj,0,-3,0)
do
if GetRawKeyReleased(27) then end
if GetRawKeyReleased(32)
ChosenObject = ChosenObject + 1
if ChosenObject = 7 then ChosenObject = 1
FixObjectToObject(XPivotObj, 0)
FixObjectToObject(XPivotObj, ChosenObject + 100000)
endif
// Spin the torso around.
RotateOBjectLocalY(TorsoObj,.2)
// Nod the head up and down.
if GetObjectAngleX(HeadObj) => 10
HeadNod$ = "DOWN"
endif
if GetObjectAngleX(HeadObj) <= -10
HeadNod$ = "UP"
endif
if HeadNod$ = "DOWN"
RotateObjectLocalX(HeadObj,-0.25)
else
RotateObjectLocalX(HeadObj,0.25)
endif
// Wave the first arm back and forth.
if GetObjectAngleX(ArmOneObj) >= 45
ArmOne$ = "BACK"
endif
if GetObjectAngleX(ArmOneObj) <= -45
ArmOne$ = "FORWARD"
endif
if ArmOne$ = "FORWARD"
RotateObjectLocalX(ArmOneObj,1.5)
else
RotateObjectLocalX(ArmOneObj,-1.5)
endif
// Wave the second arm back and forth.
if GetObjectAngleX(ArmTwoObj) >= 45
ArmTwo$ = "BACK"
endif
if GetObjectAngleX(ArmTwoObj) <= -45
ArmTwo$ = "FORWARD"
endif
if ArmTwo$ = "FORWARD"
RotateObjectLocalX(ArmTwoObj,1.5)
else
RotateObjectLocalX(ArmTwoObj,-1.5)
endif
// Make the first leg move.
if GetObjectAngleX(LegOneObj) >= 45
LegOne$ = "BACK"
endif
if GetObjectAngleX(LegOneObj) <= -45
LegOne$ = "FORWARD"
endif
if LegOne$ = "FORWARD"
RotateObjectLocalX(LegOneObj,1.5)
else
RotateObjectLocalX(LegOneObj,-1.5)
endif
// Make the second leg move.
if GetObjectAngleX(LegTwoObj) >= 45
LegTwo$ = "BACK"
endif
if GetObjectAngleX(LegTwoObj) <= -45
LegTwo$ = "FORWARD"
endif
if LegTwo$ = "FORWARD"
RotateObjectLocalX(LegTwoObj,1.5)
else
RotateObjectLocalX(LegTwoObj,-1.5)
endif
print("")
print(" Hit the spacebar to cycle through the object's pivot points")
print("")
print(" Showing pivot point for " + Part$[ChosenObject])
Sync()
loop