Jump to content

Collision between a sprite and a tweened sprite


BobDylan
 Share

Recommended Posts

I have a game where a player-sprite can jump on top of blocks.

These blocks are moving vertically up and down (with a tween).

Arcade physics and gravity is enabled on the player-sprite only.

 

But sometimes the player-sprite falls 'through' a block, as if the physics engine of the player can not calculate correctly if there's a block underneath the player.

 

Can anybody tell me why this is happening, and how to prevent this ?

Link to comment
Share on other sites

tweening is not the best solution for this..    if you tween something you probably tween the Y coordinate.. setting the y coordinate manually (that's what happens if you tween it) instead of letting the physicsengine do it with velocity disables the collision..

 

the reason it sometimes works is because on every step you will suddenly fall through the block because its tweened - then you'll be inside the block and the physics engine corrects this.. but if the block is not thick enough and you are already below the middle it will push you out on the wrong side..

 

use a custom function that changes the direction of the velocity   *= -1   after a certain distance and you will not have this problem anymore

Link to comment
Share on other sites

Collisions are highly dependent on the velocity calculations that the physics engine is working with. When you start manually adjusting the position of a physics-enabled object, the velocity becomes almost impossible to correctly calculate, and the separation routines begin failing in odd ways. You can try doing block.body.moves = false (if you've not already) which will stop the physics system imposing its movement on the object, but at the end of the day you should really be using velocity to control platform movement, rather than directly changing the x and/or y values.

Link to comment
Share on other sites

Thank you lewster32 , the following does work :

blocksprite.body.moves = false;

The player now stays on top of the blocks, whatever movement is done to the block underneath. However I get the feeling from you guys that I'm doing something that gets me in trouble some day.

The reason I want to use tweens for the block movements, is because I need easing, which I find very hard to do with changing the velocity of the blocks only.

Any way to get a nice easing (bounce) effect on those blocks without using a tween?

Link to comment
Share on other sites

You can literally apply bounce to the block's body:

block.body.bounce.setTo(0, 0.5); // will make the block apply half its velocity in the opposite direction when it collides with an object on the y axis - if using gravity, it will result in a realistic bounce

If you want a smoothed ease like Quadratic, you can roughly replicate that by reducing the velocity over time as it approaches its intended destination.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...