Question

I have a Player Entity that is governed by gravity and I have another entity at the bottom of the screen that moves around.

When my falling player entity hits the entity at the bottom I want it to bounce off of it.

Ideally I'd like to use the .bounciness property of the player entity.

Était-ce utile?

La solution

You want your bottom entity to contain the following properties:

checkAgainst: ig.Entity.TYPE.A, // player entity type
collides: ig.Entity.COLLIDES.ACTIVE

Then you want your check() method of your bottomEntity to reverse the velocity of the player entity when it collides with it.

check: function(other) {
  other.vel.y -= 100; // you could also use other.accel.y
}

Also if you want you can handle deflection angles with collisions as well (similar to the Arkanoid games):

If the player hits in the center, you want it to go straight up. If it hits on the right half, you want it to go right; if it hits on the left, you want it to go left.

So find where the player hits the bottom entity, and find its angle relative to the end of the entity.

var playerPos = player.pos.x - bottomEntity.pos.x;
var relativePos = ( bottomEntity.size.x - playerPos);
var angle = relativePos * ( Math.PI / bottomEntity.size.x ); // translate to radians - this finds the number of radians per bottom entity pixel

Once you got the angle, take the cos of it to grab the direction. Multiply the direction times the bottom entities velocity, and you've got the bottom entities new velocity.

var newVel = Math.cos( angle ) * bottomEntity.vel.x;

Then your check() method would look like this:

check: function(other) {
  var playerPos = other.pos.x - bottomEntity.pos.x;
  var relativePos = ( bottomEntity.size.x - playerPos);
  var angle = relativePos * ( Math.PI / bottomEntity.size.x ); // translate to radians - this finds the number of radians per bottom entity pixel
  other.vel.y -= newVel;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top