Jump to content

  • Log In with Google      Sign In   
  • Create Account

Moving objects based on friction or not

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.

  • You cannot reply to this topic
2 replies to this topic

#1 boogyman19946   Members   -  Reputation: 1449


Posted 09 July 2012 - 06:31 PM

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.

My personal links
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs
- C++ Standard Library Reference - For some of your C++ needs ^.^

#2 aoxomoxoa   Members   -  Reputation: 105


Posted 17 July 2012 - 01:58 PM

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;

Edited by aoxomoxoa, 17 July 2012 - 02:01 PM.

#3 diegzumillo   Members   -  Reputation: 236


Posted 19 July 2012 - 01:42 AM

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 :)

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.