View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

# Pushing objects

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1P0jahn  Members

Posted 25 January 2013 - 03:16 PM

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.

### #2Dan Mayor  Members

Posted 25 January 2013 - 04:08 PM

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.

Dan Mayor

Professional Programmer & Hobbyist Game Developer

Seeking team for indie development opportunities, see my classifieds post

### #3FLeBlanc  Members

Posted 25 January 2013 - 04:36 PM

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.

### #4P0jahn  Members

Posted 25 January 2013 - 04:53 PM

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.

### #5CC Ricers  Members

Posted 25 January 2013 - 05:14 PM

What does your current implementation do and what makes it not as smooth?

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

### #6FLeBlanc  Members

Posted 25 January 2013 - 05:14 PM

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.

### #7Dan Mayor  Members

Posted 25 January 2013 - 05:28 PM

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.

Dan Mayor

Professional Programmer & Hobbyist Game Developer

Seeking team for indie development opportunities, see my classifieds post

### #8alnite  Members

Posted 25 January 2013 - 07:22 PM

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!

### #9Barzai  GDNet+

Posted 27 January 2013 - 10:20 PM

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.

### #10BeerNutts  Members

Posted 28 January 2013 - 01:52 PM

...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

Edited by BeerNutts, 28 January 2013 - 01:52 PM.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #11P0jahn  Members

Posted 28 January 2013 - 04:22 PM

Thanks all. I will return to this later when I am done with some other code.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.