Jump to content

Move sprites to the nearest grid value with a physics engine


BobDylan
 Share

Recommended Posts

I have a game with a player sprite that moves on top of platforms (like Mario). Right now it works with Arcade physics for the y-axis and tweens for the x values, so I can control the horizontal movements very precisely. All moves of the playersprite end on a 64x64 grid. So when a tween ends, the x value is always a multiple of 64.

 

Now I want to implment the physics engine for the x-axis as well, but I'm having trouble to achieve the same grid-value thing as above.  When the playersprite bounces into walls, i want it to move to the nearest 64x64 position (but looking natural, of course).

 

I already tried using calculating the nearest 64x64 coordinate, maintaining the direction of the current movement and keeping the velocity at speed until the exact point is reached, but this ended up very messy, because sometimes the sprite moves over this point because of a high speed, and I have to set the sprite back, which looks really weird.

 

Hope the above is clear. Basically I want movements to fixed coordinates, but WITH a physics engine.

Any ideas how to achieve this?

Link to comment
Share on other sites

Very difficult with a physics engine I'm afraid. Physics simulations in are not typically suited to grid-based games as all movement is subject to division and multiplication with float values, and any attempt to manually re-position physics-enabled objects results in unpredictable movement as the physics engine tries to step in to make sense of the sudden change in velocity. For any game which uses a grid or has ultra-specific positioning requirements, you will almost certainly need to roll your own movement routine and 'fake' the physical effects such as bouncing off walls etc with tweens, animations or carefully controlled movement calculations.

Link to comment
Share on other sites

This thread might answer a question for me as well.  I am working on a space shooter that starts with a group of enemies.  I want them to individually break off from a formation and attack (i.e. like classic Galaga game).  I can identify them with getAt(index) but I have been struggling to figure out to make them move in a specific pattern that uses precise coordinates.  The movements with a tween are very choppy / jerky.  Are you saying that the physics engine prevents this (which would explain why my tweens look terrible)?  If so, can I detect collisions without the physics engine?

Link to comment
Share on other sites

Tweens should not be choppy - tweens are far less demanding to calculate than physics, and obviously way more controllable too. What will be happening though is that if your enemies have physics enabled, and body.moves = true (the default) then the physics engine and the tweens will continually fight every frame over the positioning of the enemies, causing them to vibrate and jerk. By setting body.moves = false, you can prevent the physics system influencing the position of the enemies, while keeping the ability to detect collisions/overlaps. You could also re-enable it at the end of the tween if you want velocity to take hold again.

Link to comment
Share on other sites

Ok thanks again,

 

I was hoping there was an easy solution. I think I can manage to do it with the method I mentioned,but it's very hard to get it right.

 

It would help if there was something like a 'bounced' or 'blocked' property (or event), to check which sprite is blocking the other sprite.

 

At this moment I'm using a collision-handler to find the block that the playersprite bumped into, but it's a bit tricky because the block that the player stands on is sometimes also colliding (especially when it's moving up and down). And sometimes there's no collision at all, because the velocity was already down to 0.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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