I'm building a Breakout-style game, and I'd like to have different physics for the ball depending on if it's colliding with a block vs. a paddle. Having reviewed impact.js, I see that Impact's bounciness physics are implemented in handleMovementTrace(). How do I find out which Entity the ball is currently colliding with in handleMovementTrace? Is this done with the res.tile properties?
Thx.
I check the tile it collided with using getTile. You will need to adjust the position depending where the collision took place. (ie. my tiles are 8x8, so add 8 pixels to res.pos.y to identify that tile)
if( res.collision.y ) {
console.log('tile: '+ig.game.backgroundMaps[0].getTile(Math.floor(res.pos.x), Math.floor(res.pos.y+8)));
}
Hmm...
So could I do something like this?
handleMovementTrace: function ( res ) {
var collidedEntity = ig.game.backgroundMaps[0].getTile(res.tile.x, res.tile.y);
}
No, getTile accepts pixel coordinates only. I'm not sure what res.tile object is used for...
Having reviewed the code in collision-map.js I'm fairly confident in stating that res.tile.x is the tile the Entity collides with along the x-axis, whereas res.tile.y is the same, but for the y-axis.
spacehunter, you're right that getTile() only takes coordinates, but no need to floor() the values, as the function already does this for you (see map.js).
Thx.
1 decade ago
by dominic
Well,
.handleMovementTrace()
is only called for collisions with the tile map. The
res.tile.x
and
res.tile.y
is the tile that the entity collided with on the respective axis.
For collisions with other entities there&
039;s the <2> method. However, this wont help you much to adjust the #.bounciness
of the ball entity, because
.collideWith()
is only called
after the collision has been resolved.
You could still adjust the ball's velocity here. E.g. to speed the ball up after a collision with the paddle, something like this could work:
collideWith: function( other, axis ) {
if( other instanceof EntityPaddle ) {
this.vel.x *= 1.5;
this.vel.y *= 1.5;
}
}
Page 1 of 1
« first
« previous
next ›
last »