- Joined
- Jan 11, 2009
- Messages
- 3,414
Hi!
I need some help with my physics engine.
In my engine, i have two types of entities that can collide, "objects" and "boundaries". Objects are anything that moves (think bullets or players), boundaries are anything that's static (think walls). As far as collision goes, objects are represented by a radius, a position, and a height (origin is at the bottom of the "cylinder"). Boundaries are represented by a rect, maxZ, and minZ. These do not transform in any way depending on the orientation of the unit.
I have NO problem detecting wether two objects intersect. That is a piece of cake in any scenario. My problem is about what to do about it.
This is easy enough if you're dealing with spherical collision. The separating vector is ofcourse the vector between the two objects, and the minimum distance they can be from eachother is the sum of the two objects radii. The way i thought of making sure objects don't penetrate eachother, is to add a penalty force in the opposite direction strong enough to push the objects out of eachother. When an object hits a boundary, the whole force will be directed to the object. In object-object collisions, the penalty force will be divided between them based on the ratio of their mass. Pushing objects, instead of just setting their position to a certain coordinate, is good since it is more likely to solve situations where one object might push whatever is colliding with it straight into another object. A con is that it can make collision look jagged. Moving or pushing makes no difference for me.
However, when you are dealing with the kind of shapes i am, it is hard to figure out in which direction you want to push the object, and how hard. Imagine if you have an object dropping down on a roof (boundary). Once the object instersects, it will be colliding on all axes. How do i know which direction it came form? If i would be to start pushing it out on the X axis, then maybe landing in the middle of the roof would cause you to snap next to the house instead. But if i were to start correcting the Z axis, then units would be able to climb walls.
My VERY first sulotion was to not update the coordinates on a specific axis if i knew it would cause the object to collide. This produced nice sliding along walls and such, with no jaggedness, but a) it didn't work as well on the Z axis for some reason (units would get stuck inside eachother) and b) in the event that a unit does get within the bounds of another (like if dropping down on eachother) they would get stuck and start to jitter. I know this is not the solution most real physics engines use though (they use the penalty force method).
One way i thought of solving this would be to chech which axis would be closest to correct. That is, if i can get the character out of the way by either substracting the Y axis by 2, adding the X axis by 4, or adding the Z axis by 10, then the game would clearly chose to move it along the Y axis. and hence have the collision solved. When i tried this in practice though i ended up with units teleporting around and it was just generally not very satisfactory (might be me doing it wrong).
So my conclusion is, what is the best way to keep my objects from intersecting?
I need some help with my physics engine.
In my engine, i have two types of entities that can collide, "objects" and "boundaries". Objects are anything that moves (think bullets or players), boundaries are anything that's static (think walls). As far as collision goes, objects are represented by a radius, a position, and a height (origin is at the bottom of the "cylinder"). Boundaries are represented by a rect, maxZ, and minZ. These do not transform in any way depending on the orientation of the unit.
I have NO problem detecting wether two objects intersect. That is a piece of cake in any scenario. My problem is about what to do about it.
This is easy enough if you're dealing with spherical collision. The separating vector is ofcourse the vector between the two objects, and the minimum distance they can be from eachother is the sum of the two objects radii. The way i thought of making sure objects don't penetrate eachother, is to add a penalty force in the opposite direction strong enough to push the objects out of eachother. When an object hits a boundary, the whole force will be directed to the object. In object-object collisions, the penalty force will be divided between them based on the ratio of their mass. Pushing objects, instead of just setting their position to a certain coordinate, is good since it is more likely to solve situations where one object might push whatever is colliding with it straight into another object. A con is that it can make collision look jagged. Moving or pushing makes no difference for me.
However, when you are dealing with the kind of shapes i am, it is hard to figure out in which direction you want to push the object, and how hard. Imagine if you have an object dropping down on a roof (boundary). Once the object instersects, it will be colliding on all axes. How do i know which direction it came form? If i would be to start pushing it out on the X axis, then maybe landing in the middle of the roof would cause you to snap next to the house instead. But if i were to start correcting the Z axis, then units would be able to climb walls.
My VERY first sulotion was to not update the coordinates on a specific axis if i knew it would cause the object to collide. This produced nice sliding along walls and such, with no jaggedness, but a) it didn't work as well on the Z axis for some reason (units would get stuck inside eachother) and b) in the event that a unit does get within the bounds of another (like if dropping down on eachother) they would get stuck and start to jitter. I know this is not the solution most real physics engines use though (they use the penalty force method).
One way i thought of solving this would be to chech which axis would be closest to correct. That is, if i can get the character out of the way by either substracting the Y axis by 2, adding the X axis by 4, or adding the Z axis by 10, then the game would clearly chose to move it along the Y axis. and hence have the collision solved. When i tried this in practice though i ended up with units teleporting around and it was just generally not very satisfactory (might be me doing it wrong).
So my conclusion is, what is the best way to keep my objects from intersecting?