Model animation and control

Blitz3D Forums/Blitz3D Beginners Area/Model animation and control

collimic(Posted 2016) [#1]
I am looking for some sample code to help me load an animated .b3d file that several sequences in it. I am trying to control a player model, make it move across the screen while playing a sequence. I am using the ninja.b3d file I found with pacemaker 1.0. I can get the keys to play the animation but the model will not move at the same time.

Sample code using WASD to move the player while playing sequence 1 would be very helpful.

Thank you all in advance
v/r


Dan(Posted 2016) [#2]
Check the blitz3d folder Samples\mak\Castle there is a demo of mario3d like model running arround a Castle.

Or click, on the native blitz3d ide, the samples link (on the Help Tab), then choose mak\Castle


collimic(Posted 2016) [#3]
I have looked at the code included with the Castle but I do not truly understand how to get the model to move and animate at the same time.

Most of the code has been written by a programming student. So his code is very ruff.
Any info to help the student clean up his code and make the controls and movement work would be great.
v/r



Bitterman.bb


Ninja.bb



Cocopino(Posted 2016) [#4]
ExtractAnimSeq does not do anything unless you store the information into a variable to use it later.
E.g.

Graphics3D 1600,900

Local camera = CreateCamera()
PositionEntity camera,0,5,0

Local ninja = LoadAnimMesh("media/ninja.b3d")
ScaleEntity ninja,.5,.5,.5
PositionEntity ninja,0,0,10

PointEntity(camera,ninja)
EntityParent(camera,ninja)
MoveEntity	(camera,0,3,0)

Local i
For i=0 To 20
	Local cube = CreateCube()
	PositionEntity(cube,-3,0,i*10)
Next

Local anim_idle = ExtractAnimSeq( ninja,206,250 ) ;Idle 2
Local anim_walk = ExtractAnimSeq( ninja,1,14 ) ;anim sequence 1:	Walk (normal)
Local anim = 0

While Not KeyHit(1)
	
	If KeyDown(17) Then 
		MoveEntity (ninja,0,0,0.5)
		If anim<>anim_walk Then
			anim=anim_walk
			Animate(ninja,1,0.3,anim)
		EndIf
	ElseIf anim<>anim_idle Then 
		anim=anim_idle
		Animate(ninja,1,0.1,anim)
	EndIf
	
	UpdateWorld()
	RenderWorld()
	Flip()
Wend


W key moves and animates ninja.


Guy Fawkes(Posted 2016) [#5]
This is AWESOME code guys! Although it would be nice if the Ninja could smoothly transition between multiple animation such as walking and attacking with the sword at the same time.

ninja_test.bb:



Thanks! =)

~GF


Guy Fawkes(Posted 2016) [#6]
Can someone please help make this Ninja Walk & attack with the Sword at the same time?

Thank You!

~GF


RemiD(Posted 2016) [#7]

make this Ninja Walk & attack with the Sword at the same time


If it is not an existing animation, you would have to create one yourself by mixing the two.
You can use Fragmotion to do this (the positions/rotations of the lower body while walking + the positions/rotations of the upper body while attacking) However if the two animations don't use the same number of frames, you may have to tweak it a little...


Guy Fawkes(Posted 2016) [#8]
Thank You, Remi. But that's not my point. I want to be able to do this in code.

Regards,

~GF


Cocopino(Posted 2016) [#9]
What probably would work is having two invisible ninjas as well as the "real" (visible) ninja.

Do not animate the visible ninja. Instead:
Animate invisible ninja 1 (walk animation), copy bone position + rotation to the legs/feet of the visible ninja.
Animate invisible ninja 2 (sword animation), copy bone position + rotation to the arms/hands of the visible ninja.
(Parenting the bones may also work)

The spine rotation/position may need an average between the two animations for rotation and position to make the new animation look natural.

You may then be able to store these new bone information using SetAnimKey but I've never actually used that command.


RemiD(Posted 2016) [#10]
What Cocopino suggests may work, try it !


Guy Fawkes(Posted 2016) [#11]
That's a good idea, Cocopino! I WILL try it! =)

~GF


Rick Nasher(Posted 2016) [#12]
If it works please share, I'm kinda curious to see this working too.


RemiD(Posted 2016) [#13]
Additionally you may want to try this : http://www.blitzbasic.com/Community/posts.php?topic=105608 (post#116)


Cocopino(Posted 2016) [#14]
Proof of concept below.
You'll need to figure out which bones are legs/feet and which ones the arms/legs instead of having a counter of course.

Graphics3D 1920,1080

Local camera = CreateCamera()
PositionEntity camera,0,5,-15

Local ninja = LoadAnimMesh("media/ninja.b3d")
Local ninja_inv1 = LoadAnimMesh("media/ninja.b3d")
Local ninja_inv2 = LoadAnimMesh("media/ninja.b3d")

EntityAlpha(ninja_inv1,0.01)
EntityAlpha(ninja_inv2,0.01)

PointEntity(camera,ninja)
EntityParent(camera,ninja)
MoveEntity(camera,0,6,0)

Local anim_walk = ExtractAnimSeq( ninja_inv1,1,14 ) ;anim sequence 1:	Walk (normal)
Local anim_sword = ExtractAnimSeq( ninja_inv2,32,44 ) ;anim sequence 3:	Punch and swipe sword
Local anim = 0

Animate(ninja_inv1,1,0.3,anim_walk)
Animate(ninja_inv2,1,0.3,anim_sword)

While Not KeyHit(1)
	
	Local bone = NextChild(ninja)
	Local bone1 = NextChild(ninja_inv1)
	Local bone2 = NextChild(ninja_inv2)
	Local counter = 0
	
	While bone
		
		If bone1 And bone2 Then
			
			counter=counter+1
			
			If counter>14
				PositionEntity(bone,EntityX(bone2),EntityY(bone2),EntityZ(bone2))
				RotateEntity(bone,EntityPitch(bone2),EntityYaw(bone2),EntityRoll(bone2))
			Else
				PositionEntity(bone,EntityX(bone1),EntityY(bone1),EntityZ(bone1))
				RotateEntity(bone,EntityPitch(bone1),EntityYaw(bone1),EntityRoll(bone1))
			EndIf
			
			bone=NextChild(bone)
			bone1=NextChild(bone1)
			bone2=NextChild(bone2)
		Else
			bone=0
		EndIf
		
	Wend
	
	UpdateWorld()
	RenderWorld()
	
	Text 10,10,"total children: " + counter
	Flip()
	
Wend

Function NextChild(ent)
	
	Local siblingcnt
	If CountChildren(ent)>0
		Return GetChild(ent,1)
	EndIf
	
	Local foundunused=False
	Local foundent = 0, parent,sibling
	While foundunused=False And ent<>0
		parent = GetParent(ent)
		If parent<>0
			If CountChildren(parent)>1
				If GetChild(parent,CountChildren(parent))<>ent
					For siblingcnt = 1 To CountChildren(parent)
						sibling = GetChild(parent,siblingcnt)
						If sibling=ent
							foundunused = True
							foundent = GetChild(parent,siblingcnt+1)
						EndIf
					Next
				EndIf
			EndIf
		EndIf
		ent = parent
	Wend
	Return foundent
	
End Function



Guy Fawkes(Posted 2016) [#15]
AHAHAHAHA! OMFG! Cocopino, that's HYSTERICAL! Thank you! That kicks ASS! =D

~GF


Rick Nasher(Posted 2016) [#16]
Always cool to see some nice animations. lol


RemiD(Posted 2016) [#17]
A better approach imo, would be to create a new animation on your character joints/bones (with positionentity, rotateentity, setanimkey, addanimSeq) by analyzing the rotations/translations of the 2 animations that you want to mix.
This way it will be premade and easier to use when you need it.


Guy Fawkes(Posted 2016) [#18]
Imho, that would actually be a worse way because at least in code you have infinite combinations, whereas in animations you would have to make the animations one by one.

~GF


RemiD(Posted 2016) [#19]
@GF>>I agree, but reread my post, i just suggest to create a new animation (in code) by analyzing the positions/rotations of the joints of each frame of the 2 others existing animations.

An old ugly code to demonstrate how to create an animation in code with Blitz3d :



Guy Fawkes(Posted 2016) [#20]
That's not ugly Code, Remi. That's actually beautiful code. =)

~GF


gpete(Posted 2016) [#21]
I found it useful to see coded animation! So the character mesh needs to be separate entities attached to the joints? (Yes or No??). In the samples there was a program by Birdie I think that used a one entity mesh that was attached to the joints- I believe attached to vertexes? I find it hard to wrap my head around that- I guess I consider characters to be like "puppet" models.. To me, the difficulty of animation is the main thing that holds me from creating human character-based programs.
I know a lot of things I post are redundant- but I just retired a year ago and now I have time to code again... (wish I had the sharper mind I had 15 years ago ;-)

Any comments?


RemiD(Posted 2016) [#22]

So the character mesh needs to be separate entities attached to the joints?


not necessarily

you need a skeleton with joints/bones

then you can either create separate body parts and set each body part as a child of the appropriate joint/bone (so that it will turn/move with it) or you can create a whole body and then set influences (weights) of some joints/bones over some vertices (then called "skinned vertices") and then the whole body will be deformed depending on how the joints/bones turn/move (when animating)

you can create a mesh, and then a skeleton with joints/bones, and then influences (weights) of some joints/bones over some vertices by using a modeling/rigging/skinning/animating tool like Fragmotion


RemiD(Posted 2016) [#23]
here is a simple example on how to animate a rigged skinned mesh (with a b3d rigged skinned animated mesh provided) :
http://rd-stuff.fr/rigged-skinned-mesh-and-animation-201611042221.7z


gpete(Posted 2016) [#24]
Thanks for your reply Remi- you certainly write very interesting programming threads.
What I am going to try to do is create a animation list for several basic animations using a simple list of 3d coordinates moves for the joint or body limb/objects. I have looked at Milkshape and and Blender animations where you manually move the joints in a animation sequence- but I do not want to use an external program for animation.
I want the functions to be in Blitz 3D code:
WALK function: right and left leg anim for one step- trigger is a KeyDown-
Raise Arm function and use gun: and another to swing sword
Turn head function:
Falldown Function:
etc, etc....
If I can do it I will share the code.. ( ;-) )


RemiD(Posted 2016) [#25]
Of course you can create an animation in code (see my example in post #19) but it will be less intuitive and less realistic.
You certainly will have difficulties to create complex animations...

(i suggest to try Fragmotion, it is easy enough to use, you have all you need to create meshes, skeletons (with joints/bones), influences (weights) of some joints/bones over some vertices, animations with poses , and the b3d export is clean)