Friday, March 30, 2018

unity - 2D Spaceship Thruster Movement - Turning


I have a spacecraft (shown in the image) built out of ‘blocks’ / 3D planes. It is drawn within a 3D space, but I’m ignoring the Y axis for now and keeping things on a “2D” XZ axis.


Top-down view of spaceship


The ship has a number of thruster blocks (represented in blue); each of which should fire depending on user input (does the user want to go forward? or turn left? etc).


Although the image doesn’t show it, each thruster is oriented in a logical direction; i.e the thrusters labelled A & E point left, B & F point right, thrusters G & H point down, thrusters C & D point up.


Thrusters are placed arbitrarily around the ship. There is no actual 'Thruster A' or 'Thruster B'; I'm just using the spacecraft in the above image as an example.


I already have the following which works for forward/backward thrust and strafing (in pseudo code):


foreach(thruster in ship.thrusters) {

// Forward thrust

if(keyPressed W) && thruster.direction == ShipLocalSOUTH) {

thruster.fire();
}

// Backward thrust
if(keyPressed S) && thruster.direction == ShipLocalNORTH) {

thruster.fire();
}


// Strafe Left
if(keyPressed Q) && thruster.direction == ShipLocalEAST) {

thruster.fire();
}

// Strafe right
if(keyPressed E) && thruster.direction == ShipLocalWEST) {


thruster.fire();
}

// Turn left
if(keyPressed A && ??) {

// ??
}

// Turn right

if(keyPressed D && ??) {

//??
}
}

class Thruster {

private thrusterPower = 10;


public void fire() {

shipRigidbody.AddForceAtPosition (thruster.transform.forward * thrusterPower, thruster.transform.position);
}
}

My problem lies in determining which thrusters to fire to turn the ship.


I’ve done a lot of googling on this and have found quite a lot of info already. I.e:


Using Torque and Thrusters to Move and Rotate a Player-Designed Spaceship


However my maths/trig skills are - to be blunt - crap and so I’m hoping to get a somewhat simpler explanation or even some code to help me understand and solve this problem. I'm not aiming for superbly accurate physics, just something that is fun to play.




Answer



If you know the center, you have to find out if the thruster is above or below and left or right of your center.


public class Thruster : MonoBehaviour {

public Transform center;
public ThrusterType type;

public bool IsBelowCenterAxis {
get {
return transform.localPosition.z
}
}

public bool IsLeftOfCenterAxis {
get {
return transform.localPosition.x }
}

public bool IsAboveCenterAxis {

get {
return transform.localPosition.z>center.localPosition.z;
}
}

public bool IsRightOfCenterAxis {
get {
return transform.localPosition.x>center.localPosition.x;
}
}


public void fire() {

}

}

and then :


if(Input.GetKey(KeyCode.A) && 
((thruster.type == ThrusterType.LEFT && thruster.IsBelowCenterAxis) ||

(thruster.type == ThrusterType.RIGHT && thruster.IsAboveCenterAxis) ||
(thruster.type == ThrusterType.UP && thruster.IsLeftOfCenterAxis) ||
(thruster.type == ThrusterType.DOWN && thruster.IsRightOfCenterAxis))) {

thruster.fire();
}

if(Input.GetKey(KeyCode.D) &&
((thruster.type == ThrusterType.RIGHT && thruster.IsBelowCenterAxis) ||
(thruster.type == ThrusterType.LEFT && thruster.IsAboveCenterAxis) ||

(thruster.type == ThrusterType.DOWN && thruster.IsLeftOfCenterAxis) ||
(thruster.type == ThrusterType.UP && thruster.IsRightOfCenterAxis))) {

thruster.fire();
}

Now you just need to know the force with which to fire, but that's a different question.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...