QJ.NET | Videos | Forums | iPhone | MMORPG | Nintendo DS | Wii | PlayStation 3 | PSP | Xbox 360 | PC | Downloads | Contact Us
Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact

QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides

Go Back   QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides > Developers Corner > PSP Development, Hacks, and Homebrew > PSP Development Forum
The above video goes away if you are a member and logged in, so log in now!

Gravity in C

This is a discussion on Gravity in C within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; Hi guys. Does anyone here have a small example of gravity in C? Basically I'm playing around with some stuff ...

Reply
 
LinkBack Thread Tools
Old 11-05-2007, 01:37 PM   #1

Developer
 
placo23's Avatar
 
Join Date: Aug 2007
Posts: 726
Trader Feedback: 0
Question Gravity in C

Hi guys. Does anyone here have a small example of gravity in C? Basically I'm playing around with some stuff here and wanted to do some tests with gravity. I just wanted to have a ball for example that falls, but uses some gravity so the ball bounces a bit when it hits the floor. Just like a normall ball would do. I have no idea where to start with it.

Any ideas/samples for helping me?

Cheers
__________________
Placo23

Spoiler for Wanna see my Apps?:
Why not donate and become part of this selected list?
Spoiler for Donators:
DarkSeveN - 5.100.000
--DylanDangles--2.000.000
Zuiver - 100.000
AlwaysAmiYumi - 10.338.63
placo23 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 11-05-2007, 02:13 PM   #2


Developer
 
bhunter's Avatar
 
Join Date: Dec 2006
Location: Sydney, Australia
Posts: 163
Trader Feedback: 0
Default

Well I don't know the code but the calculation would be an acceleration downwards of 9.8M/S^2 (until reaching terminal velocity obviously) and then the bounce back would be a variable depending on the compound your ball is made out of. I would probably start it on a loss of 25% momentum (ie bounce back at 75% the velocity it hit the ground at) with a deceleration of 9.8M/S^2 until Speed = 0 and then start the routine again.

Continue until the impact velocity < 0.1 (Should be low enough).

Also note, your impact point could be made out of multiple compounds also so make sure you factor that into your code ie Grass has more absorption of energy than concrete .
__________________
PSP History
Bought Dec 15th 2006 @ 2.6 -> 2.71 HEND -> 1.5 -> 3.02 OE-A -> 3.02 OE-B -> 3.03 OE-A -> 3.03OE-A' -> 3.03 OE-B -> 3.10 OE-A -> 3.10 OE-A' -> 3.30 OE-A.
bhunter is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 11-05-2007, 03:10 PM   #3

sceKernelExitGame();
 
Bronx's Avatar
 
Join Date: Jan 2006
Location: New York
Posts: 3,125
Trader Feedback: 0
Default

something like this (assumes you're using 3D, if not, get rid of the z axis):
Code:
struct RigidBody
{
     Vector3 position; 
     Vector3 velocity;
     Vector3 force;
     float mass;
};
typedef struct RigidBody RigidBody;

void Integrate( RigidBody* body, float dt )
{
     body->velocity += force/mass * dt * dt;
     body->position +=velocity;
     body->force = 0.0f;
}

void AddForce( RigidBody* body, Vector3* force )
{
     body->force += force;
}

// in main loop assuming a rigidbody has been initialized and we have the delta time
Vector3 gravity; gravity.x = 0.0f; gravity.y = -9.8f; gravity.z = 0.0f; // this shouldn't be declared in the main loop either.
AddForce( myRigidBody,  gravity );
Integrate( myRigidBody, deltaTime );
I hope this helps. I'd suggest you try downloading Chipmunk or Box2D, they're great 2D physics engine. If you're working in 3D, try giving ODE a look.

Btw, for integrating the velocity and position I'm using a John Schultz Symplectic Euler (aka Semi-implicit Euler, or Newton-Stormer-Verlet) variation. It's quite stable compared to a regular euler integration. Other integration schemes you can look at are the verlet (specifically the velocity-less verlet), midpoint, and Runga-Kutta 4 (and 3 and 2).

I hope this helps!

edit - Oh, I'd look at simple springs for the collision reaction (or better yet use Box2D or Chipmunk). Here is a decent page on the very basics of physics simulation. http://freespace.virgin.net/hugo.eli...els/m_main.htm
Bronx is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 11-05-2007, 05:30 PM   #4


Developer
 
JustChris's Avatar
 
Join Date: Oct 2005
Posts: 206
Trader Feedback: 0
Default

Damn, I wish I have heard about ODE before. I'm coding my own collision engine from tutorials I've read. I knew about the Newton SDK, but that didn't seem easy to port to the PSP.

I actually have a program which is just in pre-alpha stage right now, to make a bouncing ball. It was done to debug the controls in my program. You can test the bounce against a certain y-value so that the ball reaches a certain height it bounces off the "floor" representing the y-value.

in pseudocode:

Code:
if (ball.collidesWith(floor)) {
  if (ball.velocity > velocityLimit) {
    ball.velocity = -ball.velocity * elasticity;
  } else {
    ball.velocity = 0;
    ball.airborne = false;
}
The velocityLimit and elasticity variables are set by the user. Just tweak them till they look "right". A ball with 0 elasticity will stop on a dime, and a ball close to 1 bounciness will jump very high.

If you want to check for collision against polygons that gets more involved.

I'm gonna give ODE a shot. Trying to make a physics engine on my own was educational but I need something that I know will work, and quick.
__________________
GameSnooper - Random drawing of gaming news

The 32 Bit Shell - My development blog (also includes gaming oddities)
JustChris is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 11-05-2007, 08:34 PM   #5

Developer in Making...
 
BlackShark's Avatar
 
Join Date: Oct 2006
Location: Pimp'en in the US F#cking A!!!
Posts: 1,254
Trader Feedback: 0
Default

basically for very simple Gravity effect, you would have


Code:
gravity = 0.02;
momentumY = 0;

yPosition = 40;

while(bleh) 
{

yPosition += momentumY;

momentumY += gravity;


//other conditions and what not
}
__________________
The Wentire Worls in two Sectors....
When did I get dev statz?
Spoiler for my PSP homebrewReleases:
Ace of Space V1|PvP Pong Online|PvP Pong v3 | 3.03 BlackShark Custom Firmware
(PvP Pong DL'ed well over 2403 times combined! get yours now!)
Spoiler for Great Quotes:

"No Snowflake in an Avalanche ever feels responsible....." - Fortune Cookie.
BlackShark is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 11-05-2007, 08:40 PM   #6

It's good to be free...
 
Archaemic's Avatar
 
Join Date: Feb 2007
Posts: 2,440
Trader Feedback: 0
Default

Quote:
Originally Posted by BlackShark
basically for very simple Gravity effect, you would have


Code:
gravity = 0.02;
momentumY = 0;

yPosition = 40;

while(bleh) 
{

yPosition += momentumY;

momentumY += gravity;


//other conditions and what not
}
Yeah, if you want the gravity to be cycle-based, and not time-based.

The whole thing will slow down and speed up based on how many times it loops. Not good. You want to base it on time between calculations.
__________________
pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ
Archaemic is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 11-06-2007, 01:28 AM   #7

Developer
 
placo23's Avatar
 
Join Date: Aug 2007
Posts: 726
Trader Feedback: 0
Default

Guys thank you very much for all the explnations and examples. For this one, I'll be only using 2D, as I plan to develop a small game (flash like) using the concepts, just as a proof of concept for myself.

I shall be releasing something in the next days... I've also got another game I'm developing, and shall be releasing it soon.

Thank you very much for all the comments and explanations
__________________
Placo23

Spoiler for Wanna see my Apps?:
Why not donate and become part of this selected list?
Spoiler for Donators:
DarkSeveN - 5.100.000
--DylanDangles--2.000.000
Zuiver - 100.000
AlwaysAmiYumi - 10.338.63
placo23 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
gravity

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -8. The time now is 11:55 AM.



Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © 2009, QJ.NET. All Rights Reserved.
Contact Us