Sign in to follow this  
P0jahn

Pushing objects

Recommended Posts

P0jahn    307

In a 2D game, I want to being able to push other objects. 

Take into account that objects that you push can be a movable object.


I have already created a method that works but its not pretty and results ingame are not smooth.

Share this post


Link to post
Share on other sites
Digivance    1724

You want to provide more details and information.  In short all we can do right now is say "When the two objects are touching have them move together".  You might also want to take weight into account and slow down the movements to simulate the first object struggling to move the second.

Share this post


Link to post
Share on other sites
FLeBlanc    3141
This quickly becomes pretty complex if you have multiple objects, where you can push an object which pushes another object and so forth. You're verging into full physics library territory here, so I would recommend you look into using an existing 2D physics library such as Box2D or Squirrel. Hooking up to these libraries is very easy, and they provide decently robust physics under a free license.

Share this post


Link to post
Share on other sites
P0jahn    307

You want to provide more details and information.  In short all we can do right now is say "When the two objects are touching have them move together".  You might also want to take weight into account and slow down the movements to simulate the first object struggling to move the second.

So say A is pushing B. If A is coming from the left, upon collision it will push B to the right.

If A is coming from above, it will push B downwards.

 

This quickly becomes pretty complex if you have multiple objects, where you can push an object which pushes another object and so forth. You're verging into full physics library territory here, so I would recommend you look into using an existing 2D physics library such as Box2D or Squirrel. Hooking up to these libraries is very easy, and they provide decently robust physics under a free license.

My game is not going to support multiple objects pushing each other. Integrating my game with a 2D physics library is just to much work for such small game. It would be like putting a 500 horse power engine in a piece of junk from the seventies.

Share this post


Link to post
Share on other sites
FLeBlanc    3141

My game is not going to support multiple objects pushing each other. Integrating my game with a 2D physics library is just to much work for such small game. It would be like putting a 500 horse power engine in a piece of junk from the seventies.

 

I think you overestimate the difficulty and complexity of integrating a 2D physics lib. Seriously, take a look at the Box2D tutorials. It really isn't all that difficult.

Share this post


Link to post
Share on other sites
Digivance    1724

You want to provide more details and information.  In short all we can do right now is say "When the two objects are touching have them move together".  You might also want to take weight into account and slow down the movements to simulate the first object struggling to move the second.

So say A is pushing B. If A is coming from the left, upon collision it will push B to the right.

If A is coming from above, it will push B downwards.

 

Without counting physics into the mix yes it's that simple.  I was trying to point out that your post is to vague to hold a real discussion about.  We don't know what the problem is, we don't know what you did.  With lack of any relevant information all we can do is guess that you don't know how to detect the collision?  You can't figure out how to translate two objects at once?  What's not smooth, have you somehow poorly calculated the movements resulting in frame loss?  Are you unsure on how to apply the same movement vectors to the objects?

 

In short what I was getting at is don't be so vague.  Tell us what language you are using, what engine you are using what is happening and what the expected result was supposed to be.

Share this post


Link to post
Share on other sites
alnite    3436

You have collision detection, right? From there on, follow these steps:

 

  1. Find out where player is in relation to the object. If player.y < object.y (assuming origin is on the top-left corner of the screen), then player is above the object. If player.x < object.x, then player is to the left of the object, and so on.
  2. Then, you need to determine when the 'push' happen. You need to figure this out yourself as each game is different.  You need some sort of trigger, when the push happens.  Does keep pressing the object will trigger the push?  What many seconds of pressing the d-pad till it triggers the push?  Or, do you need to press the button while close to the object to trigger the push? Does the player need a special 'push' animation?
  3. Once player is inside 'push' state, then you need to figure out how far is the push.  Different games do it differently.  Does each push pushes the object one tile away? Half a tile? Pixel-perfect push?

So yes, something as small as pushing objects still needs extensive thoughts and planning.

 

Happy coding!
 

Share this post


Link to post
Share on other sites
Barzai    668

If your physics is really basic (like the physics in the games I'm writing now is) this may help.  By really basic I mean no terrain resistance or anything to affect the interaction of the 2 things outside of the motion of the first thing.  From your description of what you're looking for that may be the case.

 

I give each object a collisionRank parameter.  When my collision detector notices that 2 objects have overlapped, it passes them to a collision resolution function.  The collision resolution function looks at the collisionRank parameters of the 2 objects and resolves the overlap by moving the one with the lowest collisionRank.  If the object that got hit had the lower rank then it moves, effectively being pushed.

 

For what its worth, though, this method gives the same smoothness to the push as I get out of how I move the first object in the first place.  Any lack of smoothness comes from a bad timer function or browser portability (I'm coding in javascript) rather than the collision resolution.

 

Of course, this doesn't take into account any of the more complex issues that could arise, like the object having a resistance to being pushed, or what happens when one object pushes a second object into a 3rd object that has a higher collisionRank than the 1st object (or the 2nd object, for that matter).  Or, as noted above, whether you want there to be a trigger needed to cause the push to be possible in the first place.

 

Outside of something like that, as these other folks (who are way more experienced than I am) have pointed out, they'd need to know some sort of information about how you're handling your collision detection and resolution. 

Share this post


Link to post
Share on other sites
BeerNutts    4400

...so I would recommend you look into using an existing 2D physics library such as Box2D or Squirrel.

 

Just to clarify, it's chipmunk physics engine, not squirrel smile.png

Edited by BeerNutts

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this