
-----------------------------------
LaZ3R
Fri Dec 07, 2007 1:36 pm

Question about making a gravity game
-----------------------------------
I want to create a simple game where you choose the angle to launch the ball, along with the speed of it, and it must use the gravitational field of a planet in order to push itself around the planet and into the target.

It's basically like that flash game that was made a while ago :)

I'm just having a bit of trouble thinking about how to start this. I was thinking of implementing Fg = GMm / r^2, but don't know if it would really help me in this case :P. Any suggestions would be awesome :D

-----------------------------------
Ultrahex
Fri Dec 07, 2007 1:54 pm

Re: Question about making a gravity game
-----------------------------------
This is actually one of the examples that i think is in the Turing Example Programs.

But yes you are correct you could use that formula and measure the distance, just remember you are working with pixels, so your distances of measurement shouldn't be as ridiculous as in the constants given in that formula, but the ratios (as in formula structure) should be roughly the same if not the same.

-----------------------------------
LaZ3R
Fri Dec 07, 2007 2:12 pm

RE:Question about making a gravity game
-----------------------------------
The example program for this gravity idea is terrible. It's so unorganized and hard to follow. Partly why I never use those example programs. Better to learn for myself, easier to understand an individuals own code :)

Thanks though. I got it working in 20 lines or so :P Need to improve it now :)

-----------------------------------
LaZ3R
Fri Dec 07, 2007 2:21 pm

RE:Question about making a gravity game
-----------------------------------
Only problem I'm having right now is making the ball move at a certain angle instead of going straight into the other ball at an increasing acceleration. 

    Fg := (G * m1 * m2) / (Math.Distance (x1, y1, x2, y2)) ** 2

Basically my formula for now and 
    x1 += (x2 - x1) * Fg
    y1 += (y2 - y1) * Fg

is what I used to determine the new position of the ball each time through the loop. How can I manipulate the formula so I can choose an initial angle so it can kind of have an orbit effect around the other ball?

-----------------------------------
LaZ3R
Fri Dec 07, 2007 2:36 pm

RE:Question about making a gravity game
-----------------------------------
I'm kind of figuring out how to get it working half of how I want it :P

I'll figure it out, just need to clear my mind :D

Thanks.

-----------------------------------
Saad
Fri Dec 07, 2007 2:39 pm

RE:Question about making a gravity game
-----------------------------------
Ok, so you know that Fg = GMm / r^2 gives a force, now F = ma so then acceleration is  F/m. This gives us the magnitude of acceleration, so now we just have to split it into its vector components. 

Ax = cos(theta) * |A|
Ax = sin(theta) * |A|

Where theta is the angle between the two balls.

-----------------------------------
Ultrahex
Fri Dec 07, 2007 2:40 pm

Re: Question about making a gravity game
-----------------------------------
It appears (from how i understand what you just posted), that your physics is incorrectly done.

An initial angle and such would be done by setting an initial velocity... BTW the Force Of Gravity should not act on the location, it should act on the velocity!

Acceleration Changes Velocity
Velocity Changes Position.

For Example For Free Fall Gravity its Fg = mg , where Fg is the force of gravity, m is the mass, and g is the acceleration due to gravity.

lets assume our mass is 1kg, and gravity is 9.8m/s^2 like usual on earth.
also assume our initial position is x=0, y= 10, and velocity = 0 (for simplicity).

After 1 Second, velocity = 9.8m/sec down, x=0 still, y=0.5*9.8m/sec
After 2 Second, velocity = 2*9.8m/sec down, x=0 still, y=0.5*9.8*2^2=2*9.8
After 3 Second, velocity = 3*9.8m/sec down, x=0 still, y=0.5*9.8*3^2=(9/2)*9.8
....

thus... after each moment in time, our velocity increase by Fg since Fg is constant its just going up by 9.8m/sec each time. However since you are using Fg = GMm / r^2, Fg is not constant and needs to be calculated at each point in time before adding to velocity.

The position is affected by its velocity, which you can do simple ways (as in just adding your velocity to its position, or actually calculate it correctly :).

-----------------------------------
LaZ3R
Fri Dec 07, 2007 2:52 pm

RE:Question about making a gravity game
-----------------------------------
LOL, I didn't realize I put Fg in there. Kind of trying to skip a few steps I see :D

I'm half asleep right now, that would explain a lot :D

Saad's method should work just perfectly actually, Think the fact that we are currently doing some weird stuff in Electrostatics in Physics is screwing me over :P

-----------------------------------
LaZ3R
Fri Dec 07, 2007 2:52 pm

RE:Question about making a gravity game
-----------------------------------
Thanks guys :P

-----------------------------------
Sean
Fri Dec 07, 2007 5:31 pm

Re: Question about making a gravity game
-----------------------------------
I'm actually looking for a gravity script, been working on one for a Mario game that me and Gooie were working on. 

Any idea how to do it properly, and get the character to jump and come down?

-----------------------------------
Nick
Fri Dec 07, 2007 5:40 pm

RE:Question about making a gravity game
-----------------------------------
try if ground (or chary = y of ground or whatdotcolp(charx,chary - 1) = ground color) then jump proc

your jump procedure will be like your regular loop except it dissallows the chracter to jump again and y velocity increases until max height is reached then it dereases until your chra hits the ground

hope that helps

-----------------------------------
Ultrahex
Fri Dec 07, 2007 6:27 pm

Re: Question about making a gravity game
-----------------------------------
Not to insult you momop or anything, but in order for correct jumping what you would do is something similar what you said for collision detection with ground. However for proper gravity jumping with jump procedure you would have an initial velocity speed upwards on the jump and then gravity would pull the character down.

I will implement something like this just to show the difference.

-----------------------------------
Nick
Fri Dec 07, 2007 6:46 pm

RE:Question about making a gravity game
-----------------------------------
i think i know what you mean 

what I said:

    the character raises at a constant speed then falls immediatly at the same speed

what you said:

    the character raises at a speed relitive to gravity slows at the height then falls at the speed relitive once again to gravity

-----------------------------------
Ultrahex
Fri Dec 07, 2007 7:20 pm

Re: Question about making a gravity game
-----------------------------------
Precisely, yours assumes constant velocity, mine is assuming constant acceleration which looks more realistic, for code example:

If you do not see the difference,
In assuming constant velocity, the character moves up and down at same speed
In assuming constant gravity, the gravity of the things around it are constant (such as each pulling down), thus the character moves up and down with different speeds and reaches 0 velocity at the top of the jump and then becomes negative to mean downwards direction.

keys:
r is change realistic
a,d are move left, move right
w is jump


% Type Declarations

type point :
    record
        x : real
        y : real
    end record

type obj :
    record
        v : point % Velocity
        s : point % Position
        isJump : boolean
    end record

% Variables

var realistic : boolean := true
const g := 9.8

var player : obj
player.isJump := false
player.v.x := 0
player.v.y := 0
player.s.x := 50
player.s.y := 0

var keys : array char of boolean

% Procedures

fcn applyGravity (o : obj) : obj
    var a : obj := o
    if (realistic) then
        a.v.y -= g
        a.s.y += a.v.y
    else
        if (a.isJump) then
            if (a.s.y 