Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

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 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?

1 decade ago by Joncom

If you're just trying to reverse velocity, why not do this?
this.vel.x *= -1;
this.vel.y *= -1;

This multiplies each velocity by -1.

1 decade ago by boomshanka

Yeah that would make sense but I'm increasing the velocity after a while to increase the difficulty (done by modifying maxVel.x and maxVel.y) that's why I'm using maxVel.x and maxVel.y

1 decade ago by mglnunez

maxVel.x and maxVel.y are positive numbers.
You are making them all move at negative max velocity.

It's why it only works on enemies from the top and left, because their velocities are positive.
Enemies coming from the right already have negative velocity.

I recommend something like this.
var sign = (this.vel.x < 0 ) ? 1 : -1;
this.vel.x = sign * this.maxVel.x;

1 decade ago by mimik

http://impactjs.com/documentation/class-reference/entity#accel-x-accel-y
Page 1 of 1
« first « previous next › last »