![]() |
| Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact |
| ||||||
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 ...
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 |
![]() ![]() Developer
|
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?:
Spoiler for Donators:
|
|
|
|
|
|
#2 |
![]() ![]() ![]() Developer
|
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. |
|
|
|
|
|
#3 |
![]() ![]() sceKernelExitGame();
|
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 );
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
__________________
|
|
|
|
|
|
#4 |
![]() ![]() ![]() Developer
|
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;
}
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) |
|
|
|
|
|
#5 |
![]() ![]() Developer in Making...
|
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
}
__________________
NEWMy New BLOG!NEW The Wentire Worls in two Sectors.... When did I get dev statz?Spoiler for my PSP homebrewReleases:
Spoiler for Great Quotes:
|
|
|
|
|
|
#6 | |
![]() ![]() It's good to be free...
|
Quote:
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 əʞɒʇ
|
|
|
|
|
|
|
#7 |
![]() ![]() Developer
|
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?:
Spoiler for Donators:
|
|
|
|
![]() |
| Tags |
| gravity |
| Thread Tools | |
|
|