Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Technocrat on Tue 22/12/2009 13:04:37

Title: Lander - gravity and thrust...
Post by: Technocrat on Tue 22/12/2009 13:04:37
If anyone's played one of those reeeaaaallly old lunar-lander games, they'll know what it is I'm trying to implement as a mini-game in AGS. I think I might have gotten myself in over my head, but it also makes an interesting thought-experiment. There are a couple of problems I've noticed having been experimenting with it. I've probably gotten it organised in a really ineffective way, and any pointers would be helpful!

I start off in the level, having the player as the "lander". The three main variables I'm using are "Thrust", "Gravity" and "Speed". In Repeatedly_Execute I have the following:


int Gravity = 1;
Speed=Speed-Gravity; 
 
player.y=player.y + Speed;


This alters the speed of the "lander" every cycle, based on the force that gravity exerts on it. In effect, making it move downwards more quickly as time progresses, as "Gravity" is subtracted from "Speed" every cycle. The problem I find here therefore is that it becomes absurdly fast, and zooms downwards off the screen, even when gravity is only 1.

My second issue is thus - controlling the lander.


  if (Thrust>0) Thrust-=1;  // So that thrust doesn't stick at full every time it's pressed
 
     if ((IsKeyPressed(eKeyUpArrow))&&(Thrust<5)) {
      Thrust+=2;
   }
   
Speed = Speed + Thrust;



In order to make the thrust propel the craft upwards, the addition of "Thrust" is designed to counteract the subtraction of "Gravity" from "Thrust", resulting in net upwards movement. once again, it moves far too quickly.

As I say, my first problem is the absurd rate of movement, but for the second, I notice that as it gets faster, it will move by several pixels per cycle - this will actually cause trouble by, for example, leaping straight past the "ground" at the bottom if falling too quickly. What I need it to do instead is always move at a *distance* of one pixel, but the speed of this one pixel movement to change depending on the speed of the lander.

If anyone could offer any suggestions, I'd be most appreciative, or if they could point me towards a helpful thread - I've not been able to find something quite appropriate yet though.

Many thanks!
Title: Re: Lander - gravity and thrust...
Post by: Gilbert on Tue 22/12/2009 13:32:16
I haven't really read the content of your post, but I think one way to do this is to use a virtual coordinate system, say, the coordinate values are 10 times (for finer movement it could be 100x, 500x, etc. depending on your need) the real scale on screen. Just do whatever physics calculation and store it in an integer variable say, ScaledY and then set the position of the player via something like player.y=ScaledY/10;.

Title: Re: Lander - gravity and thrust...
Post by: Calin Leafshade on Tue 22/12/2009 13:42:19
use floats for position, velocity and acceleration.

Obviously cordinates need to be ints but do the conversion at the last second just before moving the object.

As for moving past the platform i used an algorithm to check one 'frame' ahead in increments of 1.

so if your speed is 4 pxs per frame: check 1 pixel infront, then 2 pixels, then 3, then 4.

If you dont find a platform then you can move the frame as normal.
Title: Re: Lander - gravity and thrust...
Post by: Khris on Tue 22/12/2009 13:45:15
In theory, the thrust should be either x or zero, x being a constant value here. At least that's how the lunar lander games worked, afaik.

The rest seems fine and exactly, scaling should solve both problems at once.

If you need constant distance and varying frame rate, keep in mind that the smallest unit is 1/40 second (at the default game speed at least), thus (40*distance/second) is the top speed.
Another way is to go up pixel by pixel if the next position was inside the ground.
Title: Re: Lander - gravity and thrust...
Post by: Technocrat on Wed 23/12/2009 12:09:19
Ah, many thanks! By using absirdly high scaled ints, then using a scaled Y value, it seems to be allowing me to create a more "natural" feel to the flight. Now I just need to find the perfect thrust/gravity ratio.