Animation help plz!!!

Blitz3D Forums/Blitz3D Beginners Area/Animation help plz!!!

GIMPY73(Posted 2003) [#1]
Hi

Im doing a cannon fodder clone in Blitz3d tho the game is 2d

ive got the mouse routine working fine the main character can move in 8 directions and is controled by the mouse
left mouse button moves the character to where you clicked right mouse button fire's a bullet

The problem im having is this

My character has 24 frames of animation (0-23) and there is 3 frames per direction so if he moves up screen his back is towards you etc etc

What i need is some animation code so when i click my character walks towards where i clicked instead of sliding

I just cant get my head around it any help plz

i can post the code if needed but you will need to make some of your own graphics cos i ripped the graphics out of the amiga version of cannon fodder as test graphics


Thanks
GIMPY


Gauge(Posted 2003) [#2]
Assum CharX,CharY as the characters position.
Then your using mousex,mousey to pick the spot. If your not using tiles just the x/y pos figure use mousex/mousey and use these next ifs to figure the direction. If your using tiles...youll have to write a slightly more advanced code, cause if you click 3 tiles right)(east) and 1 tile(north) your anim string would be loaded as ne, and you would move 1ne, and then 2e all as anim ne direction.

If charX=ClickX Andif CharY<ClickY then "anim string south"
If charX=clickX andif chary>clicky then "anim string north"
If charX>clickx andif chary=Clicky then "anim string east"
if charX<clickx andif chary=clicky then "anim string east"
;Note if your not using a tile system, you'll probably want
;to add a 20 or 30 degree pixel varriance on clickx/clicky
;otherwise it'll only pick e/n/s/w on line of pixels
If CharX<ClickX Andif CharY<ClickY then "anim string nw"
if charX>ClickX andif CharY<ClickY then "anim string ne"
if CharX>ClickX andif CharY>ClickY then "anim string se"
if charx<ClickX andif CharY>Clicky then "anim string sw"

I don't know if this helps, but post up your code and i'll try to get it to work for ya


GIMPY73(Posted 2003) [#3]
K thanks m8 here is my code

;*****************
;* Mouse Routine *
;* Cannon Fodder *
;* Style *
;*****************

Graphics 640,480,16
SetBuffer BackBuffer()

;-Types-----------------------------------------------------

Type bullet
Field x#,y#
Field ang#,speed#
Field img
Field life#
End Type

Type player
Field x#,y#
Field ang#,speed#
Field frm ;animation frame
End Type

Type particle
Field x#,y#
Field dx#,dy#
Field size#
Field r,g,b
End Type

;-Globals-------------------------------------------------------

Global me.player=New player
Global clickedx#,clickedy#
Global imgplayer
Global imgshot
Global sndshot
Global sndgun
Global fps = CreateTimer(60)

;-Graphics+Sounds-----------------------------------------------

imgplayer=LoadAnimImage("fodman3.png",15,14,0,23)
imgshot=LoadImage("shot.bmp")
sndshot=LoadSound("shot.wav")
sndgun=LoadSound("gun.wav")
MaskImage imgplayer,255,0,255
MidHandle imgplayer
MidHandle imgshot
me\x=160
me\y=100

;-Main loop-------------------------------------------------

While Not KeyHit(1)

Cls

Color 50,100,150
Rect 0,0,640,480,1

angle=ATan2(MouseX()-me\x,MouseY()-me\y)

If Angle<0 Angle=Angle+360


If MouseHit(2)

clickedx=MouseX()
clickedy=MouseY()
me\ang=ATan2(clickedx-me\x,clickedy-me\y)
me\speed=2

EndIf

If Sqr(me\x-clickedx)^2+(me\y-clickedy)^2<9
me\speed=0
EndIf

If KeyDown(42) And MouseDown(2)
me\speed=6
End If

If MouseHit(1)

b.bullet = New bullet ;Create bullet
b\x = me\x : b\y = me\y
b\ang= ATan2(MouseX()-b\x,MouseY()-b\y)+Rnd(-3,6)
b\speed=6
b\life=30
b\img=imgshot
PlaySound(sndgun)

End If

For b.bullet = Each bullet
movebullet(b)
Next
If angle<0 angle=angle+360

If angle>160 And angle<200
me\frm=4;up
EndIf

If angle>110 And angle<160
me\frm=22;up right
EndIf

If angle>70 And angle<110
me\frm=10;right
EndIf

If angle>20 And angle<70
me\frm=16;down right
EndIf

If angle>345 Or angle<15 Or angle=0
me\frm=1;down
EndIf

If angle<335 And angle>290
me\frm=13 ;down left
EndIf

If angle<290 And angle>245
me\frm=7 ;left
EndIf

If angle<245 And angle>200
me\frm=19 ;up left
EndIf

;-Move particles----------------------------------------------
For pt.particle = Each particle

pt\x = pt\x + pt\dx
pt\y = pt\y + pt\dy
pt\size = pt\size - .6

If pt\size=<0 Then Delete pt

Next

me\y=me\y+(Cos(me\ang)*me\speed)
me\x=me\x+(Sin(me\ang)*me\speed)

For pt.particle = Each particle

Color pt\r,pt\g,pt\b
Oval pt\x-pt\size/2,pt\y-pt\size/2,pt\size,pt\size

Next

Color 255,255,255
DrawImage imgplayer,me\x,me\y,me\frm;player gfx

Color 255,25,25
Rect MouseX(),MouseY(),2,2; mouse gfx

Flip
Wend

End

;-Functions-------------------------------------------------


Function MoveBullet(b.bullet)



b\y=b\y+(Cos(b\ang)*b\speed)
b\x=b\x+(Sin(b\ang)*b\speed)


DrawImage b\img,b\x,b\y,0


b\life=b\life-1


If b\life<0 ;Then Delete b.bullet
For i=1 To 50

pt.particle = New particle
pt\x = b\x
pt\y = b\y
pt\dx = Rnd(-2,2):pt\dy=Rnd(-2,2)
pt\size = Rnd(2,4)
col# = Rnd(48,128)
pt\r = col: pt\g = col: pt\b = col

If i>30 Then

pt\r=255: pt\g=300-pt\size*40: pt\b=0 ;colour it red/yellow
pt\dx=pt\dx/2 ;slow it down
pt\dy=pt\dy/2

End If
Next

Delete b.bullet
chnwave1=PlaySound(sndshot)
ChannelVolume chnwave1,.2


End If

End Function


Gauge(Posted 2003) [#4]
Alright i got the code this morning its about 7am central time here. I'll work on it tonight when i get outta work(6p or so). I noticed a few little things.


GIMPY73(Posted 2003) [#5]
Thanks Gauge for atleast looking at my code :)






Thanks
GIMPY


Gauge(Posted 2003) [#6]
I'm back, sorry it tooke me so long...heres a few things.
1. Instead of Rect 0,0,,640,480 each frame, use Clscolor 50,100,150..it'll auto clear the screen to that color...
2. Each time it loops right now it reads the angle every time and changes the animation loop back to the first frame of whichever direction the mouse is from the char. To toggle the the three frames, use me\frm=me\frm+1 after flip and before the wend. Then run a few ifs if frame is 4,7,10 the end of that loop then restart ie If me\frm=4 then me\frm=1,etc. You should be able to check this without half a dozen ifs with left$ and divided it by 3 though but i haven't tried it.,If you want the character to always look in the direction of the mouse, you'll have to keep two angles, the angle its currently moving, vs the angle its facing. Or use If me\speed=0(for the char not moving) me\frm=(1,4 etc from the angle).
3. I would function this out so your main loop looks like the following.
Main Loop()
Cls
ReadAngle Me
If Mousehit(2) MovePlayer me
If Mousehit(1) Shoot
UpdateBullets
Color 255,255,255 
DrawImage imgplayer,me\x,me\y,me\frm;player gfx 
Color 255,25,25 
Rect MouseX(),MouseY(),2,2; mouse gfx
Flip
Wend


Of course i tend to over function things too....I didn't have an aniimage so i couldn't test it totally though, and for some reason my old testfile of this got deleted *growl*
I used text 10,10,angle and test 10,50,me\frm add those in before flip and you'll see what its doing. I always add text, such and such a spot so i can track everything.

You may also want to use the field bullet and use it as a type in player also. Well good luck, hope that helps


GIMPY73(Posted 2003) [#7]
Thanks M8 ill give it a try 2night and c wot i can come up with




GIMPY :)


Dr Derek Doctors(Posted 2003) [#8]
Spooky! I'm writing Cannon Fodder in B2D! That's kinda' scary...