Shoot em up smooth player movement code?
Blitz3D Forums/Blitz3D Programming/Shoot em up smooth player movement code?
| ||
Hi has anyone got some code that moves a player left right in a kinda 'ramped' / smooth way with a little inertia? |
| ||
This should get you going the right direction. Typed straight into replybox, so, untested.Const KEY_LEFT = 203 Const KEY_RIGHT = 205 Repeat cls If KeyDown(KEY_LEFT) xSpeed = xSpeed - 0.1 EndIf If KeyDown(KEY_RIGHT) xSpeed = xSpeed + 0.1 EndIf If not KeyDown(KEY_LEFT) and xSpeed < 0 xSpeed = xSpeed + 0.1 EndIf If not KeyDown(KEY_RIGHT) and xSpeed > 0 xSpeed = xSpeed - 0.1 EndIf if Abs(xSpeed) < 0.1 xSpeed = 0 endif If xSpeed < -5 then xSpeed = -5 If xSpeed > 5 then xSpeed = 5 x = x + xSpeed DrawImage ship,x,y flip Forever |
| ||
You could look into using sin/cos to produce that deired effect. Build up the angle to 90 to full movement, then when no direction key/button is pressed slow decrease the angle parameter of sin to 0. [code] if keydown(LEFT) then if angle < 90 then angle = angle + 1 end if else if keydown(RIGHT) then If angle > -90 then angle = angle - 1 end if else if angle > 0 then angle = angle - 1 if angle < 0 then angle = 0 end if elseif angle < 0 then angle = angle + 1 if angle > 0 then angle = 0 end if end if end if move ship sin(angle),0 [\code] stick that in your loop. Some of that code isn't fully blitz, but you get the jist of it :o). The sin(angle) part is the movement part, with the effect you mentioned. Sorry i'm in a rush, need to run! |
| ||
Maybe this?Graphics 640,480,16,1 Const KEY_LEFT = 203 Const KEY_RIGHT = 205 Global Velocity#, x#=320, y#=450 Const Acceleration# = .25 Const Drag# = .95 ;max speed = Acceleration / ( 1- Drag ) Global Ship = CreateImage( 32, 32 ) MidHandle Ship SetBuffer ImageBuffer( Ship ) Color 255,255,255 Rect 8,0,16,32,1 Rect 0,16,32,16,1 SetBuffer BackBuffer() Repeat Cls Thrust# = Acceleration * ( KeyDown( KEY_RIGHT ) - KeyDown( KEY_LEFT ) ) Velocity = Velocity * Drag + Thrust x = x + Velocity DrawImage ship,x,y Flip Until KeyDown(1) |
| ||
Thanks people! Stevie G, your one is perfect, ta. Can this go into the code archives? |
| ||
Nice code stevie! |