I have a little bit of a dilemma regarding the "correctness" of my physics simulation.
The game I'm working on is a 2D sidescroller and my question pertains to the method of simulation self-propelled motion (walking).
Currently, I'm basically faking the motion of the objects in the game, but I'd very much like to move beyond that and introduce a reasonably correct way to move objects about. My problem stems from the fact that in order to move, we apply a force against the ground and, after application of suitable laws of motion (I believe the third law is most notable in this case), we end up accelerating while the ground seems to stay put.
The way I see it is that I would have to check what other objects I'm in contact with, add up all the forces, check what objects I'm pushing against, and workout the acceleration. I looked around articles to see if any of them mention doing things this way, but I couldn't find anything on it. My question is as follows:
Is it at all feasible to simulate game physics that way or should I just fake the initial forces and pushing action?
I can't seem to find the right way to convey what I'm trying to say, but if you guys can't make out what I'm saying, let me know and I'll try explaining again.
Moving objects based on friction or not
What you want to do is have an object with an x and y coordinates, an x velocity and a y velocity, and a function that updates the velocities and coordinates.
[source lang="cpp"]class Obj{
int vx,vy;
public:
int x,y;
/*the rest*/
void update()
{
if(obj_jump){
vy = 9;
obj_in_air = true;
}
x += vx;
y += vy;
if(obj_in_air) vy += -3;
if(vy < -9) vy = -9;
}
};[/source]
So the forces you mentioned will be acting on the velocity.
The line "if(obj_in_air) vy += -3" is an example of how the force of gravity can work on the object's upward velocity. Every frame the velocity (initially 9 if they jumped) will be 9, 6, 3, 0. When it hits 0, that is the peak of the jump. They start heading down at 3, 6, 9 pixels per frame, with 9 as terminal velocity.
The same can be implemented any other force, however you have to know when the force can and should be ignored.
For example, you should make sure to turn obj_in_air to false when the object hits the ground, or else it will forever be attempting to go down farther, which is a bit ridiculous.
EDIT: This way, if the object is trying to go right, you only need to change vx to the speed it will go right. You should never really be manually changing the x and y; that is accomplished by x += vx; y += vy;
[source lang="cpp"]class Obj{
int vx,vy;
public:
int x,y;
/*the rest*/
void update()
{
if(obj_jump){
vy = 9;
obj_in_air = true;
}
x += vx;
y += vy;
if(obj_in_air) vy += -3;
if(vy < -9) vy = -9;
}
};[/source]
So the forces you mentioned will be acting on the velocity.
The line "if(obj_in_air) vy += -3" is an example of how the force of gravity can work on the object's upward velocity. Every frame the velocity (initially 9 if they jumped) will be 9, 6, 3, 0. When it hits 0, that is the peak of the jump. They start heading down at 3, 6, 9 pixels per frame, with 9 as terminal velocity.
The same can be implemented any other force, however you have to know when the force can and should be ignored.
For example, you should make sure to turn obj_in_air to false when the object hits the ground, or else it will forever be attempting to go down farther, which is a bit ridiculous.
EDIT: This way, if the object is trying to go right, you only need to change vx to the speed it will go right. You should never really be manually changing the x and y; that is accomplished by x += vx; y += vy;
You're making your own physics simulator? I never thought too much about it, as there are so many engines ready to go, but I expect it to be pretty much how you're describing, calculating the acceleration based on all forces acting on all physical objects. Things like ground are usually not meant to move, so it's a logical assumption that several simplification schemes take place. Sorry, I'm not of much help

This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement