Question about making a gravity game
Author |
Message |
LaZ3R
|
Posted: Fri Dec 07, 2007 1:36 pm Post subject: 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 . Any suggestions would be awesome ![Very Happy Very Happy](images/smiles/icon_biggrin.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Ultrahex
|
Posted: Fri Dec 07, 2007 1:54 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
LaZ3R
|
Posted: Fri Dec 07, 2007 2:12 pm Post subject: 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 Need to improve it now ![Smile Smile](images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
LaZ3R
|
Posted: Fri Dec 07, 2007 2:21 pm Post subject: 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? |
|
|
|
|
![](images/spacer.gif) |
LaZ3R
|
Posted: Fri Dec 07, 2007 2:36 pm Post subject: RE:Question about making a gravity game |
|
|
I'm kind of figuring out how to get it working half of how I want it
I'll figure it out, just need to clear my mind
Thanks. |
|
|
|
|
![](images/spacer.gif) |
Saad
![](http://compsci.ca/v3/uploads/user_avatars/182387547249e7ebb91fb8a.png)
|
Posted: Fri Dec 07, 2007 2:39 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
Ultrahex
|
Posted: Fri Dec 07, 2007 2:40 pm Post subject: 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 . |
|
|
|
|
![](images/spacer.gif) |
LaZ3R
|
Posted: Fri Dec 07, 2007 2:52 pm Post subject: 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
I'm half asleep right now, that would explain a lot
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 ![Razz Razz](images/smiles/icon_razz.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
LaZ3R
|
Posted: Fri Dec 07, 2007 2:52 pm Post subject: RE:Question about making a gravity game |
|
|
Thanks guys ![Razz Razz](http://compsci.ca/v3/images/smiles/icon_razz.gif) |
|
|
|
|
![](images/spacer.gif) |
Sean
![](http://compsci.ca/v3/uploads/user_avatars/47413941748406f441f83e.png)
|
Posted: Fri Dec 07, 2007 5:31 pm Post subject: 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? |
|
|
|
|
![](images/spacer.gif) |
Nick
![](http://compsci.ca/v3/uploads/user_avatars/19644337547e837b41d67a.png)
|
Posted: Fri Dec 07, 2007 5:40 pm Post subject: 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 |
|
|
|
|
![](images/spacer.gif) |
Ultrahex
|
Posted: Fri Dec 07, 2007 6:27 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
Nick
![](http://compsci.ca/v3/uploads/user_avatars/19644337547e837b41d67a.png)
|
Posted: Fri Dec 07, 2007 6:46 pm Post subject: 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 |
|
|
|
|
![](images/spacer.gif) |
Ultrahex
|
Posted: Fri Dec 07, 2007 7:20 pm Post subject: 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
Turing: |
% 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 <= 145) then
a.v.y + = g
a.s.y + = a.v.y
else
a.v.y - = g
a.s.y + = a.v.y
end if
end if
end if
result a
end applyGravity
proc gameLoop ()
loop
Draw.FillBox (round (player.s.x ), round (player.s.y ), round (player.s.x + 10), round (player.s.y + 10), brightred)
put "Realistic: ", realistic
put "Jumping: ", player.isJump
put "Vel (x,y): (", player.v.x, ",", player.v.y, ")"
put "Pos (x,y): (", player.s.x, ",", player.s.y, ")"
Input.KeyDown (keys )
if keys ('r') then
realistic := (not realistic )
end if
if keys ('a') then % Move Left
player.s.x - = 15
end if
if keys ('d') then % Move Right
player.s.x + = 15
end if
if keys ('w') then % Jump
if (not player.isJump ) then
player.isJump := true
if (realistic ) then
player.v.y + = 74
end if
end if
end if
player := applyGravity (player )
if (player.s.y <= 0) then
player.s.y := 0
player.v.y := 0
player.isJump := false
end if
View.Update ()
delay (100)
cls
exit when keys ('x')
end loop
end gameLoop
% Start Program
View.Set ("offscreenonly")
gameLoop ()
|
|
|
|
|
|
![](images/spacer.gif) |
|
|