realbasic-games
[Top] [All Lists]

Re: Gravity, Physics etc.

To: REALbasic Games <realbasic-games at lists dot realsoftware dot com>
Subject: Re: Gravity, Physics etc.
From: "Joseph J. Strout" <joe at realsoftware dot com>
Date: Mon, 13 Dec 2004 17:18:07 -0600
Delivered-to: realbasic-games at lists dot realsoftware dot com
References: <1D4C1578-4D56-11D9-9E4C-000D9334D728 at mac dot com>
At 9:27 AM +1100 12/14/04, Josh Farquhar wrote:

For my major project in software, I am programming a collection of mini-games. However, I have already hit a snag. I am not too good with advanced maths or physics or anything, so I am not sure how to program in gravity. The mini-game is one where you click on a ball to bounce it back up, before it hits the ground.

A simple gravity simulation is quite, er, simple. The ball has, in addition to its position (x,y), a velocity (vx,vy). Gravity applies a constant acceleration, which is to say that it increases the velocity at a constant rate. So, if the time since the last frame is dt, you simply update the velocity via gravity:

   vy = vy + G * dt   // where G is acceleration due to gravity
   // (...which doesn't affect vx at all)

Then update the position from the velocity:

   x = x + vx * dt
   y = y + vy * dt

That's all there is to it.

I have managed some simple gravity code, where I add a certain number to a variable called VertVelocity, which I then add to the sprites Y position. This looks OK, but I know there must be something that will look a lot better.

No, that sounds exactly right to me.

 I also need the ball to bounce off walls etc. too.

Ah, well that's a completely different kettle of fish, involving collision detection and response. There's some simple code for that in a demo called "pinball" (or something like that) on the CD; there was also a recent article about it in RB Developer magazine, by Lars Jensen. I'd recommend you dig up that code, and also order that issue of RBD if you don't have it already.

Best,
- Joe

--
REAL World 2005 - The REALbasic User Conference
March 23-25, 2005, Austin, Texas
<http://www.realsoftware.com/realworld>
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>