Creating Object Animation in Code?

Blitz3D Forums/Blitz3D Beginners Area/Creating Object Animation in Code?

Captain Wicker (crazy hillbilly)(Posted 2011) [#1]
Hi,

Is it possible to model an object or mesh and create animation on the object with bb3d? Like rotating a limb and somehow record it onto the model?


Thanks!


Yasha(Posted 2011) [#2]
SetAnimKey is the command you want, and comes with a decent example.

Be aware though that there's no built-in way to save the result of this, unless you write your own save function. In 99% of cases I would strongly recommend not bothering with animating in code, as it's usually a lot more work than the alternative.


Captain Wicker (crazy hillbilly)(Posted 2011) [#3]
thanks Yasha! your advice is always appreciated. :D


RemiD(Posted 2012) [#4]
Yes it is possible, but not really practical to do.

See an example i have done in the past (old ugly code...) :

Graphics3D 800,600,32,2 
SetBuffer BackBuffer()  
AmbientLight 125,125,125 
 
Light = CreateLight(2)  
LightRange Light,20 
PositionEntity Light,0,10,0  
 
Origine = CreateCube() 
ScaleMesh Origine,0.01,0.01,0.01 
EntityColor Origine,255,0,0 
PositionMesh Origine,0,0,0 
  
Camera = CreateCamera()   
CameraRange Camera,0.1,1000   
PositionEntity Camera,3,1.6,2 
PointEntity Camera,Origine 
 
Robot_Feet = CreatePivot() 
PositionEntity Robot_Feet,0,0,0 
 
Robot_Mesh = CreateCube() 
ScaleEntity Robot_Mesh,0.5/2,0.4/2,0.8/2 
EntityColor Robot_Mesh,060,060,060 
EntityParent Robot_Mesh,Robot_Feet 
PositionEntity Robot_Mesh,0,0,0 
 
Bone0_Pivot = CreatePivot()  
ScaleEntity Bone0_Pivot,1,1,1 
EntityParent Bone0_Pivot,Robot_Feet 
PositionEntity Bone0_Pivot,0,0.4/2,0.8/2 
 
Bone0_Mesh = CreateSphere(6) 
ScaleEntity Bone0_Mesh,0.05,0.05,0.05 
EntityColor Bone0_Mesh,000,000,255 
EntityParent Bone0_Mesh,Bone0_Pivot 
PositionEntity Bone0_Mesh,0,0,0 
 
BrasD_Mesh = CreateCube() 
ScaleEntity BrasD_Mesh,0.1/2,0.31/2+0.05/2,0.1/2 
EntityColor BrasD_Mesh,060,060,060 
EntityAlpha BrasD_Mesh,0.5 
EntityParent BrasD_Mesh,Bone0_Pivot 
PositionEntity BrasD_Mesh,0,+0.31/2-0.05/2,0 
  
Bone1_Pivot = CreatePivot()  
ScaleEntity Bone1_Pivot,1,1,1 
EntityParent Bone1_Pivot,Bone0_Pivot 
PositionEntity Bone1_Pivot,0,+0.31,0 
 
Bone1_Mesh = CreateSphere(6) 
ScaleEntity Bone1_Mesh,0.05,0.05,0.05 
EntityColor Bone1_Mesh,000,000,255 
EntityParent Bone1_Mesh,Bone1_Pivot 
PositionEntity Bone1_Mesh,0,0,0 
 
AvantBrasD_Mesh = CreateCube() 
ScaleEntity AvantBrasD_Mesh,0.1/2,0.31/2,0.1/2 
EntityColor AvantBrasD_Mesh,060,060,060 
EntityAlpha AvantBrasD_Mesh,0.5 
EntityParent AvantBrasD_Mesh,Bone1_Pivot 
PositionEntity AvantBrasD_Mesh,0,+0.31/2,0 

Bone2_Pivot = CreatePivot()  
ScaleEntity Bone2_Pivot,1,1,1 
EntityParent Bone2_Pivot,Bone1_Pivot 
PositionEntity Bone2_Pivot,0,+0.31,0 
 
Bone2_Mesh = CreateSphere(6) 
ScaleEntity Bone2_Mesh,0.05,0.05,0.05 
EntityColor Bone2_Mesh,000,000,255 
EntityParent Bone2_Mesh,Bone2_Pivot 
PositionEntity Bone2_Mesh,0,0,0 
 
Pince1_Mesh = CreateCube() 
ScaleEntity Pince1_Mesh,0.1/2,0.2/2,0.05/2 
EntityColor Pince1_Mesh,060,060,060 
EntityAlpha Pince1_Mesh,0.5 
EntityParent Pince1_Mesh,Bone2_Pivot 
PositionEntity Pince1_Mesh,0,+0.2/2,0.05/2 
  
Bone3_Pivot = CreatePivot()  
ScaleEntity Bone3_Pivot,1,1,1 
EntityParent Bone3_Pivot,Bone1_Pivot 
PositionEntity Bone3_Pivot,0,+0.31,0 
 
Bone3_Mesh = CreateSphere(6) 
ScaleEntity Bone3_Mesh,0.05,0.05,0.05 
EntityColor Bone3_Mesh,000,000,255 
EntityParent Bone3_Mesh,Bone3_Pivot 
PositionEntity Bone3_Mesh,0,0,0 
 
Pince2_Mesh = CreateCube() 
ScaleEntity Pince2_Mesh,0.1/2,0.2/2,0.05/2 
EntityColor Pince2_Mesh,060,060,060 
EntityAlpha Pince2_Mesh,0.5 
EntityParent Pince2_Mesh,Bone3_Pivot 
PositionEntity Pince2_Mesh,0,+0.2/2,-0.05/2 
 
 
  
;/////////////////////////////////////// 
;////////// Animated sequence \\\\\\\\\\ 
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 
;Specifies the Frame : 
Frame% = 1 
 
;Moves and Rotates the Bones : 
Bone0_Pitch# = 0 
Bone0_Yaw# = 0 
Bone0_Roll# = 0 
RotateEntity Bone0_Pivot,Bone0_Pitch#,Bone0_Yaw#,Bone0_Roll# 
 
Bone1_Pitch# = +160 
Bone1_Yaw# = 0 
Bone1_Roll# = 0 
RotateEntity Bone1_Pivot,Bone1_Pitch#,Bone1_Yaw#,Bone1_Roll# 
 
Bone2_Pitch# = 0 
Bone2_Yaw# = 0 
Bone2_Roll# = 0 
RotateEntity Bone2_Pivot,Bone2_Pitch#,Bone2_Yaw#,Bone2_Roll# 
 
Bone3_Pitch# = 0 
Bone3_Yaw# = 0 
Bone3_Roll# = 0 
RotateEntity Bone3_Pivot,Bone3_Pitch#,Bone3_Yaw#,Bone3_Roll# 
 
;Records the Frame :  
SetAnimKey Bone0_Pivot,Frame% 
SetAnimKey Bone1_Pivot,Frame% 
SetAnimKey Bone2_Pivot,Frame% 
SetAnimKey Bone3_Pivot,Frame% 
 
 
 
;Specifies the Frame : 
Frame% = 5 
 
;Moves and Rotates the Bones : 
Bone0_Pitch# = +90 
Bone0_Yaw# = 0 
Bone0_Roll# = 0 
RotateEntity Bone0_Pivot,Bone0_Pitch#,Bone0_Yaw#,Bone0_Roll# 
 
Bone1_Pitch# = +90 
Bone1_Yaw# = 0 
Bone1_Roll# = 0 
RotateEntity Bone1_Pivot,Bone1_Pitch#,Bone1_Yaw#,Bone1_Roll# 
 
Bone2_Pitch# = +20 
Bone2_Yaw# = 0 
Bone2_Roll# = 0 
RotateEntity Bone2_Pivot,Bone2_Pitch#,Bone2_Yaw#,Bone2_Roll# 
 
Bone3_Pitch# = -20 
Bone3_Yaw# = 0 
Bone3_Roll# = 0 
RotateEntity Bone3_Pivot,Bone3_Pitch#,Bone3_Yaw#,Bone3_Roll# 
 
;Records the Frame :  
SetAnimKey Bone0_Pivot,Frame% 
SetAnimKey Bone1_Pivot,Frame% 
SetAnimKey Bone2_Pivot,Frame% 
SetAnimKey Bone3_Pivot,Frame% 
 
 
  
;Specifies the Frame : 
Frame% = 10 
 
;Moves and Rotates the Bones : 
Bone0_Pitch# = +90 
Bone0_Yaw# = 0 
Bone0_Roll# = 0 
RotateEntity Bone0_Pivot,Bone0_Pitch#,Bone0_Yaw#,Bone0_Roll# 
 
Bone1_Pitch# = +90 
Bone1_Yaw# = 0 
Bone1_Roll# = 0 
RotateEntity Bone1_Pivot,Bone1_Pitch#,Bone1_Yaw#,Bone1_Roll# 
 
Bone2_Pitch# = 0 
Bone2_Yaw# = 0 
Bone2_Roll# = 0 
RotateEntity Bone2_Pivot,Bone2_Pitch#,Bone2_Yaw#,Bone2_Roll# 
 
Bone3_Pitch# = 0 
Bone3_Yaw# = 0 
Bone3_Roll# = 0 
RotateEntity Bone3_Pivot,Bone3_Pitch#,Bone3_Yaw#,Bone3_Roll# 
 
;Records the Frame :  
SetAnimKey Bone0_Pivot,Frame% 
SetAnimKey Bone1_Pivot,Frame% 
SetAnimKey Bone2_Pivot,Frame% 
SetAnimKey Bone3_Pivot,Frame% 
 
 
  
;Specifies the Frame : 
Frame% = 15 
 
;Moves and Rotates the Bones : 
Bone0_Pitch# = 0 
Bone0_Yaw# = 0 
Bone0_Roll# = 0 
RotateEntity Bone0_Pivot,Bone0_Pitch#,Bone0_Yaw#,Bone0_Roll# 
 
Bone1_Pitch# = +160 
Bone1_Yaw# = 0 
Bone1_Roll# = 0 
RotateEntity Bone1_Pivot,Bone1_Pitch#,Bone1_Yaw#,Bone1_Roll# 
 
Bone2_Pitch# = 0 
Bone2_Yaw# = 0 
Bone2_Roll# = 0 
RotateEntity Bone2_Pivot,Bone2_Pitch#,Bone2_Yaw#,Bone2_Roll# 
 
Bone3_Pitch# = 0 
Bone3_Yaw# = 0 
Bone3_Roll# = 0 
RotateEntity Bone3_Pivot,Bone3_Pitch#,Bone3_Yaw#,Bone3_Roll# 
 
;Records the Frame :  
SetAnimKey Bone0_Pivot,Frame% 
SetAnimKey Bone1_Pivot,Frame% 
SetAnimKey Bone2_Pivot,Frame% 
SetAnimKey Bone3_Pivot,Frame% 
 
 
  
;Adds the recorded frames to the sequence : 
Sequence0 = AddAnimSeq(Bone0_Pivot,Frame%) 
 
;Loads the Sequence previously created 
Sequence0Test = ExtractAnimSeq(Bone0_Pivot,1,15) 
 
 
  
Repeat  
 
	Cls 
	 	
	If KeyDown(17) = True   	  
		MoveEntity Camera,0,0,0.1  	  
	EndIf  
   
	If KeyDown(31)=True   	  
		MoveEntity Camera,0,0,-0.1  	  
	EndIf    
	
	If KeyDown(30)=True   	  
		MoveEntity Camera,-0.1,0,0 	  
	EndIf
	
	If KeyDown(32)=True   	  
		MoveEntity Camera,0.1,0,0 	  
	EndIf
	
	MXSpeed# = Float(MouseXSpeed())*0.25   
	MYSpeed# = Float(MouseYSpeed())*0.25   
	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2   
	CameraPitch# = CameraPitch# + MYSpeed#   
	If (CameraPitch# < -89)
	 CameraPitch# = -89
	EndIf   
	If (CameraPitch# > 89)
		CameraPitch# = 89  
	EndIf 
	RotateEntity Camera,CameraPitch#,EntityYaw(Camera)-MXSpeed#,0   
 
	If KeyDown(44) 	 
		If Not Animating(Bone0_Pivot) 			 
			;Runs the recorded sequence 
			Animate(Bone0_Pivot,3,1,Sequence0Test) 									 
		EndIf	 
	EndIf 

	If KeyDown(2) 	  
		WireFrame True    
	Else  	  
		WireFrame False 		  
	EndIf 
	 
	UpdateWorld  
	 
	RenderWorld  
	 
	Text 20,20,"AnimSeq(Bone0_Pivot) : " + AnimSeq(Bone0_Pivot) 
	Text 20,40,"AnimLength(Bone0_Pivot) : " + AnimLength(Bone0_Pivot) 
	Text 20,60,"AnimTime(Bone0_Pivot) : " + AnimTime(Bone0_Pivot) 
	Text 20,80,"W to run the animated sequence." 
	 
	Flip  
	 
Until KeyDown(1)  
	 
End


Fragmotion is an accessible software if you want to animate your b3d meshes.