Controlling a Spaceship with Rocket Engines

Hey,
I have a problem with controlling a spaceship. It want to controll it by adding a list of rocket engines, having a rotation and a power. The player will be able to power them and the spaceship moves. I know the basics of jbullet and jme. But I don’t know how to implement this, if you could give me a tip, it would be very nice.
Thank you
Turakar

With applyImpulse you can set the position of the rocket force as well as its direction. It takes two vectors for this.

You should set gravity to 0 to simulate space. Controlling the rockets can be done by adding analogListeners and register key’s to the rockets.

No sure how far you are already with your code, so these are just some very general hints.

[EDIT] a lot of information can be found here as well. Good to start:
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physics

Yes, the basics are already set up, but my problem is how to calculate the relative position of the impulses and the impulse itself. I don’t know how to do this, if the ship is already rotated. Perhaps I should have explained my problem more precise. But thank you for your help.

Yeah, I hear you. That is a tricky part indeed. I do something similar in my naval ship simulator. What I did is the following:

  1. Create control point relative to the ship (in your case, this will be the local position of the rockets).
  2. At every prePhysicsTick, calculate the world position of these contol points.
  3. Apply impuls at these newly calculated positions (substract the ships position from the vectors calculated in point 2).

Some code how I did it:
1)
[java]vecControlPoint[i].set(xCoor, 0f, zCoor); // Set the coordinates for the given control point on starboard side.
vecControlPointCalc[i] = new Vector3f(xCoor, 0f, zCoor); // Set the coordinates for the given calculated control point on starboard side.
[/java]
2 & 3)
[java]shipPos.set(myShipsPhysics.getPhysicsLocation()); // Store the physic body’s location
// Loop through all the control points and apply a force at every control point:
waterImpulseForce.set(0, localDisplacement[i]waveForce(1+(waveHeight[i]-cpHeight[i]))*tpf, 0); // Set the force.
waterImpulseLocation.set(vecControlPointCalc[i].subtract(shipPos)); // Set the location the force has to be applied to.
myShipsPhysics.applyImpulse(waterImpulseForce, waterImpulseLocation); // Apply the force to the hull.[/java]

This force is only pointing upwards because in my case it is used to push the ship upwards when it sails into a wave. If you want to calculate a certain direction of the force, you can do it as follows:

[java]// Rudder:
rudderForce.set((float)((propForce*0.25f)*Math.cos(-getShipRealHeading()-rudderAngle))tpf, 0f, (float)((propForce0.25f)*Math.sin(-getShipRealHeading()-rudderAngle))*tpf);
rudderLocation.set(myShip.rudder.getWorldTranslation().x-myShipsPhysics.getPhysicsLocation().x, myShip.rudder.getWorldTranslation().y-myShip.getLocalTranslation().y+(float)(Math.abs(Math.asin(rudderAngle))*0.2f), myShip.rudder.getWorldTranslation().z-myShipsPhysics.getPhysicsLocation().z);
myShipsPhysics.applyImpulse(rudderForce, rudderLocation);[/java]

getShipRealHeading() is a function which does nothing else then returning the rotation of the ship arounds its y-axis:
[java]public float getShipRealHeading()
{
myShipsPhysics.getPhysicsRotation(shipsRot); // Get the actual rotation of the ship.
shipsRot.toAngles(angles); // Convert the rotation to 3 angles.
shipRealHeading = angles[1]; // Get the real heading of the ship.
return shipRealHeading;
}[/java]

Of course, this is just my solution for my application, but maybe it will give you some ideas to continue?

1 Like

Another solution is to compute the force relative to the ship, and then apply the ship rotation to the force. This allows you to avoid cos and sin, which are very annoying when moving in the 3 dimensions…

2 Likes

@yang71
Good tip! Especially for a space ship which moves in 3 dimensions. Maybe I can use it as well :smiley:

Thank you for your help! But a problem occured: I don’t know how to
rotate a 3d vector ;).

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

1 Like

Wow, big thanks! The basics are working! My two rocket Engines are working!