I’m working on my very first game, a spaceship simulator. I started following a tutorial I found on youtube from UnrealTek and ended creating my own custom version of the blueprints that produces the cool semi-realistic sensation of being on the space that I had in my mind. It uses Thruster Physic Components for roll, pitch and yaw.
NOTE: I recorded the explanation of the issue on this youtube video. Just the same that is explained below.
So I was using a Box Collision Component to test but eventually I decided to replace it for a more accurate collision geometry (a simplified version of the ship). I created it on Blender, renamed it according to the documentation and…the nightmare began. The collision works fine except that it changes the behavior of the rotation. When doing a 360º YAW rotation you can see the horizon go up and down (like a sine wave).
I made sure to set the center of mass of the ship and the geometry at the origin in Blender. No offset for center of mass being applied on Unreal. I tried with the Auto Convex Collision and the results were the same. I added a box simple collision and the “sine wave” movement was gone! I also added a box in Blender (as collision) and it also made the wave dissapear, but again, it’s a box and also changed the angular velocity.
I’ve been stuck with this for 2 weeks and can’t find tutorials about it. From the bottom of my heart, HEELP!!!
How did you set center of mass in Blender? Because as far as I know, UE4 calculates center of mass on its own, from the volume of your collision meshes. There is no way you can align center of mass with the pivot point unless object is symmetrical across all axis, besides using center of mass offset.
In blender, you select the object and do CTRL+ALT+Shift+C and select “origin to center of mass”. Once the origin was there I moved the objects (both the mesh and the collision mesh) to the origin and applied location, rotation and scale.
As I show in the video, I tried an Auto Convex Collision instead of using the collision mesh from blender but the results were exactly the same.
Ok I see. Moving anything in Blender won’t change position of the center on mass in UE4. Center of mass is re-calculated in UE4 from the collision mesh, in addition inertia tensor is calculated too. The box will have symmetrical inertia tensor on all axis, which means that you need the same amount of torque to get angular acceleration of 5rad/sec in pitch and in yaw. When inertia tensor is not symmetrical, as its the case with your ship judging from its shape, you need different amount of thrust on different axis to achieve coherent angular velocity. On top of that, as you are not applying torque but use thrusters, position of the thrusters in relation to center of mass will change resulting amount of torque applied to object.
If you want to keep developing it in direction of using physics for simulation, you need to read up on quite a few things:
Basically even if you know which thruster you need to fire from player controls, you have to calculate amount of force each of them should produce, such that resulting torques are similar in magnitude. This can be done by taking into account distance of the thruster from center of mass and inertia tensor in axis in which thruster creates rotation.
The general approach would to be is to calculate how much stronger one thruster should be in comparison to another thruster. For example, if we have two thruster which provide yaw movement of the ship. One is placed at the nose and one is placed at the back of the ship. Front thruster is 5 meters away from the center of mass and back thruster is 7 meters away from the center of mass.
As both thrusters act in the same axis, moment of inertia is the same. All formulas, such as calculation of torque and angular acceleration are linear then only distance to center of mass is considerable variable. Using this observation your front thruster should be 7/5 times stronger than back thruster to produce the same amount of angular velocity.
In case when thrusters act in different axis things get a bit more complicated as now we need to take into account difference in moment of inertia on each axis (inertia tensor).
Let’s say we have w1 and w2 which are angular velocities around Z axis (yaw) and Y axis (pitch).
We want to achieve w1 = w2 when some controls are applied.
Expand angular velocity:
w = w[SUB]0[/SUB] + a * dt
a - angular acceleration
a = T / I
T is torque and I is moment of inertia
T = F * r
F is force applied at some point away from the center of mass and r is distance to that point of force application.
So r is distance at which thruster is placed and F is force that it produces.
Lets pull all this back into first equation and assume that there where no angular velocity when controls where applied so w0 is zero in both axis:
w[SUB]1[/SUB] = w[SUB]2[/SUB]
a[SUB]1[/SUB] * dt = a[SUB]2[/SUB] * dt
T[SUB]1[/SUB] / I[SUB]1[/SUB] = T[SUB]2[/SUB] / I[SUB]2[/SUB]
(F[SUB]1[/SUB] * r[SUB]1[/SUB]) / I[SUB]1[/SUB] = (F[SUB]2[/SUB] * r[SUB]2[/SUB]) / I[SUB]2[/SUB]
I - moment of inertia is know for a specific mesh and can be retrieved from inertia tensor vector. r - distance to thruster from center of mass can be calculated too. So we are left with two unknowns F[SUB]1[/SUB] and F[SUB]2[/SUB]. We can set one of them to value of 1 and get equation to calculate scale for the second force.
F[SUB]2[/SUB] = 1;
(F[SUB]1[/SUB] * r[SUB]1[/SUB]) / I[SUB]1[/SUB] = (1 * r[SUB]2[/SUB]) / I[SUB]2[/SUB];
F[SUB]1[/SUB] * r[SUB]1[/SUB] = (I[SUB]1[/SUB] * r[SUB]2[/SUB]) / I[SUB]2[/SUB];
F[SUB]1[/SUB] = ((I[SUB]1[/SUB] * r[SUB]2[/SUB]) / I[SUB]2[/SUB] ) / r[SUB]1[/SUB];
F[SUB]1[/SUB] then becomes a scale factor of how much stronger/weaker thruster 1 should be in comparison to thruster 2.
(there might be mistake in derivation, it’s best to double check)
Wow thank you for all the detailed response. I while ago read the links you posted but I think I was only considering that it was an issue with the center of mass of my mesh or some wrong configurations in the physics section.
Another guy answered yesterday this same question I posted here but in a youtube video about collisions. He told me about the Torque calculations too that I wasn’t taking in consideration. I guess that as a designer I will have to consider the impact of simplifying the rotation mechanics versus completing the mechanics with all the calculations you accurately mentioned.
Something I’d like to add is that in my blueprint I’m changing the Angular velocity to 1 when receiving input from player (to create the sensation of weight while rotating) and back to 3 when no input to stop the rotation a bit faster. This of course would have to change in case the physics calculations are implemented.
Thanks, you deserve a promotion in your job for your answer lol
Exactly, just making you aware of what you have to tackle if you go with full physics approach. Kinematic behavior - aka rotating and moving mesh manually gives you much more control than physics approach but it’s easier to get stuck in corner cases if you don’t plan all features upfront. Good luck wth it!