Game Development Community

Stuttering Spaceships - should I be using a 'Tick'?

by Quest Johnny · in Torque Game Builder · 12/03/2007 (7:47 pm) · 2 replies

I'm a noob with TGB, so please excuse if this is blatantly obvious to everybody but me:

I'm trying to make a 2D game where you are piloting a spaceship around in space, similar a bit to "asteroid" where you can turn and thrust. The problem I'm getting is that the spaceship (and everything) "stutters" madly when the ship gets up to speed. I'll explain a bit more:

I don't know what I'm doing, so I started by bounding keys to WAS, turning on turns left, right and thrust. Pretty standard so far. Turn only gets called once for the keypress, so I needed a way for the ship to keep on turning while the key was down, and this is I think where I go wrong.

I use onTimer() and set a continual 25ms timer on the ship (approx. equal to a tick).

When Left is down, it changes the ship's Course and then calls an update function updateCourse() which ultimately calls rotateTo() to rotate my sprite. That worked a bit funny, but with tweaking how much it turned and the rotateTo() speed, it seems quite fluid.

When I press 'W' I say thrust is on and I call an updateMove() which uses setLinearVelocityPolar using the ship's course and speed.

and the ship turns very well, but 'stutters' if I move forward. I strongly suspect that this is because of my trying to force essentially a game-loop which is competing with the REAL game-loop and that I am going about this entirely the wrong way. However, reading this function, I don't see why turn works and thrust does not. Another interesting thing is that while the ship turns when moving, it doesn't actually change direction..

It's probably simplest at this point to just show you my whole code:

function pShipFwd() {
   $shipThrust = true;
   $pShip.updateMove(); //on change you see
}//F

function pShipFwdStop() {
   $shipThrust = false;
   $pShip.updateMove(); //on change you see
}//F

function pShipLeft() {
   $shipLeft = true;
   $pShip.updateCourse();
}

function pShipRight() {
   $shipRight = true;
   $pShip.updateCourse();
}

function pShipTurnStop() {   
   $shipLeft = false;
   $shipRight = false;
}

function playerShip::updateCourse(%this) {
   %turnSpeed = 1;
   %rotationSpeed = 60;
   
   if($shipLeft)
      $shipCourse = $shipCourse - %turnSpeed;
   else
   if($shipRight)
      $shipCourse = $shipCourse + %turnSpeed;
   %this.rotateTo( $shipCourse, %rotationSpeed );   //autostop is true
}//F

function playerShip::updateMove(%this) {
   %this.setLinearVelocityPolar($shipCourse,$shipSpeed);
}//F

function playerShip::onTimer(%this) {
   if($shipLeft || $shipRight) 
      %this.updateCourse();
      
   if($shipThrust && $shipSpeed<30) {
      $shipSpeed++;
      %this.updateMove();
   }//if
   else
   if(!$shipThrust && $shipSpeed>0) {
      $shipSpeed = $shipSpeed - 1;
      %this.updateMove();
   }//if 
}//F

Am I doing this wrong for T2D? I don't quite see how to get an object to "rotate left while a key is pressed" without a loop like this

#1
12/03/2007 (8:11 pm)
There is a bug with TGB when the camera is mounted to an object. If you have your camera mounted to your ship, then you'll just have to put up with the jutters until 1.5.2 is released (which will fix the problem).

There is a lengthy post about it in the bug report forum.
#2
12/03/2007 (8:54 pm)
Ha! I was just about to post that even when I used the asteroid code given, it stuttered.

Well, that's great to know =) thanks. I'll eagerly await 1.5.2