Game Development Community

Spaceship Game Acceleration Help

by Sebastian Young · in Torque 2D Beginner · 12/12/2013 (12:17 pm) · 1 replies

I recently finished the Getting Started tutorial where you make a spaceship and asteroid game, and I am now working on putting my own spin on it for practice. In the tutorial it had me set %spaceship.setFixedAngle(false); so the ship doesn't rotate in response to physics forces.

I would like to change it so that it does rotate due to collisions since it will make the collisions more realistic. The problem is when I set %spaceship.setFixedAngle(false) to true, as soon as I push 'w' to accelerate the spaceship starts spinning out of control. What can I do to prevent this from happening while still letting the ship rotate in response to a collision?

Here is the code for the accelerate function as I suspect that is where the problem is but I have no idea as to what.

function PlayerShip::accelerate(%this)
{
//Get the angle of our spaceship. When the ship is pointing upwards, its Angle is 0.
%adjustedAngle = %this.Angle;

//Make sure that the angle is always between 0 and 360 degrees
if(%adjustedAngle < 0) %adjustedAngle *= -1;
else if(%adjustedAngle > 0) %adjustedAngle = 360-%adjustedAngle;

//When used as a math operand, % refers to modulo (or modulus) operator
//This function can be read as %adjusted angle = %adjustedAngle % 360;
%adjustedAngle %= 360;

//If we are thrusting, shorten our vector
if(%this.isThrusting)
{
//Calculate a direction from an Angle and Magnitude
%ThrustVector= Vector2Direction(%adjustedAngle,35);
}
else
{
%ThrustVector = Vector2Direction(%adjustedAngle,95);

//We temporarily remove the Damping of Linear Velocity to allow full power!
%this.setLinearDamping(0.0);

//We temporarily increase the Damping of Angular velocity so that the ship turns slower when at full thrust
%this.setAngularDamping(2.0);
}

//Adding our position to the ThrustVector determines the strength of our thrust
%MywordX = %this.Position.x + %ThrustVector.x;
%MywordY = %this.Position.y + %ThrustVector.y;

//applyLinearImpulse pushes on our spaceship, using %ThrustVector as the impulse vector.
//The second parameter is the point in the ship's collision shape used to apply the thrust
%this.applyLinearImpulse(%ThrustVector, "0 0");

//We are now thrusting, we will set this to false when we release the 'w' key
%this.isThrusting = true;

//We create a schedule to repeat this thrust every 100 milliseconds
%this.thrustschedule = %this.schedule(100,accelerate);
}

#1
12/16/2013 (2:54 pm)
The applyLinearImpulse is happening at 0,0 in local space. Is that the center of mass for the spaceship? Applying a force at a location that is not the center of mass for the object will also generate an angular velocity.