1 decade ago by boomshanka
Hi there!
I have a strange error that seems straight-forward but I can't tell if I'm doing something wrong or not. It has to do with changing entity velocities.
The game is very similar to asteroids - enemies move in from the top, left and right side of the canvas and the player has to dodge the enemies. I have written a simple "power-up" that the player can pick up which makes the enemies bounce back on impact (no pun intended). This seems to work fine for entities coming from the left or top side, but for entities coming from the right the velocity is not reversed.
Basically I reverse the velocity in the entity update-method by calling
For entities coming from the left the call is translated into
I have a strange error that seems straight-forward but I can't tell if I'm doing something wrong or not. It has to do with changing entity velocities.
The game is very similar to asteroids - enemies move in from the top, left and right side of the canvas and the player has to dodge the enemies. I have written a simple "power-up" that the player can pick up which makes the enemies bounce back on impact (no pun intended). This seems to work fine for entities coming from the left or top side, but for entities coming from the right the velocity is not reversed.
Basically I reverse the velocity in the entity update-method by calling
this.vel.x = -1 * this.maxVel.x
and this.vel.y = -1 * this.maxVel.y
but then I noticed that the value is reset in lib/impact/entity.js:123 where this call is made:return vel.limit( -max, max );
For entities coming from the left the call is translated into
-100.limit( -100, 100 );
but for enemies coming from the right the line becomes 100.limit( -(-100), -100 );
which is basically the same as 100.limit( 100, -100 );
. So I know why the value is set as it is, I just don't know the best way to solve it. Maybe I shouldn't mess with the velocity at all and use large values for accel.x and accel.y but I really think there's a better way. Does anyone have a suggestion on how to solve this?