collapse collapse

* Who's Online

  • Dot Guests: 145
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11164
  • stats Total Topics: 1700
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 1470

Author Topic: Physics coding?  (Read 3659 times)

0 Members and 1 Guest are viewing this topic.

Offline SandstormEntertainment

  • Newbie
  • *
  • Posts: 13
  • Reputation 19
  • Gender: Male
    • View Profile
Physics coding?
« on: August 05, 2009, 11:02:54 am »
Hey, how would i go about programming physics into my game/ game engine? Like to define gravity and friction and such. Would i make a class with those properties or what? Thanks
Sandstorm Entertainment!

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Physics coding?
« Reply #1 on: August 05, 2009, 11:17:30 am »
Well... If I was going to make a physics class.
I would have a variable for each setting.

Example.

Code: [Select]
class cPhysics
{
     float GravityStrength;
     float Friction;
};

After that, I would have a constructor to initialize it obviously.
Code: [Select]
cPhysics::cPhysics(float G, float F)
{
     GravityStrength = G;
     Friction = F;
}

Then you would need a way to apply the friction to movement. In my eyes, movement is a set of numbers. So by passing in the numbers such as the speed they move at, we can decrease its value with friction. If they are jumping, we can decrease the z value by gravity. So lets make a function for it.

Code: [Select]
float cPhysics::ApplyPhysics(int Movement, bool ApplyGravity)
{
     if(ApplyGravity)
          return (Movement-GravityStrength);
     else
          return (Movement-Friction);
}


Thats for BASIC physics. For more realistic stuff, you have to use the formulas from a math book. But the basic idea is the same, you build a physics class, then pass in the maximum speed possible, that is then reduced by the friction or gravity, thus acting as if it was slowed down. To keep this effect up for a slowing motion. Keep a temporary variable for a units current speed as well as its normal speed. Use the current speed to move the character. Total speed will be the original speed that you can always reset back to or add up to.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline SandstormEntertainment

  • Newbie
  • *
  • Posts: 13
  • Reputation 19
  • Gender: Male
    • View Profile
Re: Physics coding?
« Reply #2 on: August 05, 2009, 01:32:18 pm »
That makes sense. And do you know how i would fit the formulas into my variables and funtion then?
Sandstorm Entertainment!

Offline GodLord

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 477
  • Reputation 800
  • Gender: Male
  • C++/Java Developer
    • View Profile
Re: Physics coding?
« Reply #3 on: August 05, 2009, 01:35:45 pm »
That makes sense. And do you know how i would fit the formulas into my variables and funtion then?
Wouldn't you just replace the formulas with what you used as variables then so instead of
Code: [Select]
Movement-GravityStrengthYou would use your formula.

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Physics coding?
« Reply #4 on: August 05, 2009, 01:37:50 pm »
Say you have a variable to move your model or sprite. So as a example, lets say my guy moves 12 pixels every cycle of the engine.

so
Code: [Select]
cPhysics phys(1.3, 2.9);

float iMove = 12;
float iCurrentMove =iMove;

 (phys.ApplyPhysics(iCurrentMove, false); // We are moving, not jumping.

int main()
{
     iCurrentMove = (phys.ApplyPhysics(iCurrentMove, false)); // We are moving, not jumping.
     DoMoveCharacter(iCurrentMove);
     return 0;
}
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: Physics coding?
« Reply #5 on: August 05, 2009, 03:09:56 pm »
If you want real Physics you will either need:
A Physics library like Newton Game Dynamics
or
Write your own

If you write your own here are a few starting points:
Weight = Mass x Gravity
Force = Mass x Acceleration
Fx = Fy (The forces in the X direction = the forces in the y direction)
Also Fl = Fr (Forces in the left direction = the forces in the right direction)

All I can say is that my college Physics class was hard and fun at the same time :D.

Those are all I can remember off the top of my head but you get the idea.
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

 

Donate


* Search


* Recent Posts

I miss the chatbox by Shishka
[August 27, 2019, 11:52:39 pm]


Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]