javascript - Movement for out-of-bound objects, canvas -
i have space ship in canvas. has velocities, ship.vx
, ship.vy
. when it's 30px away canvas borders set ship.vx
& ship.vy
0
, move background objects in ship's opposite direction. @ moment ship stuck @ point. that's good. if try move left-right(stuck @ top/bottom
) or top-down(stuck @ left/right
) doesn't since stuck in point vx
& vy
set 0
.
if accelerate in it's opposite direction takes 5 seconds pick it's velocity (around 2), it's @ same point 5 seconds.
i tried not set vy
0
when out of x-axis , vice-versa ship keeps moving in other axis.
so i'm trying achieve ship'll stuck when it's 30 px border, if try move or accelerate in other 3 directions it'll pretend if it's not stuck.
any of know mechanisms?
thanks.
function stuckship(){ if( (ship.x - ship.width < 0) || (ship.x + ship.width > w) || (ship.y + ship.height > h) || (ship.y - ship.height < 0)) { ship.vx = 0; ship.vy = 0; } } function againandagain(){ var angle = ship.rotation; var x = math.cos(angle); var y = math.sin(angle); var ax = x*thrust, ay = y*thrust; ship.vx += ax; ship.vy += ay; stuckship(); ship.draw(context); } document.addeventlistener('keydown', function(e){ switch(e.keycode){ case 38: thrust = 0.35; break; case 37: ship.rotation -= 3; break; case 39: ship.rotation += 3; break; } }
manipulating velocity of object based on it's position on screen display-purposes never best idea. use parent-based systems, have 1 main-container , objects (including ship) child container , move relatively main-container. can update container's position, if ship's global position in 30px-band, make "lock" on edge of screen.
Comments
Post a Comment