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.
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.
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.
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.
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.
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.
You have collision detection, right? From there on, follow these steps:
So yes, something as small as pushing objects still needs extensive thoughts and planning.
Happy coding!
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.
...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