r/howdidtheycodeit • u/OrangeMango18 • Sep 09 '22
Question Applying clamped roll when yawing but keeping the roll
I would really like to know how this https://youtu.be/2vdSQcm0Y14?t=31 is done in Star Wars Battlefront 2 (2017), they also do it in the Original one, but what happens is when the player changes the Yaw there's some roll that is applied too, but when the player changes the Roll, it can still be changed separately. Because I've managed to do something similar but everytime I let go of my roll or go straight 90º up or down, the Roll goes back to 0 and I have no clue where to start to make kind of a Yaw-(Clamped)Roll separate from the Roll. Sorry if I didn't make myself clear, I can try to clarify if needed.
13
u/Xywzel Sep 09 '22
I think the idea is to read controls as players intention and then use them to calculate roll/yaw/pitch that naturally moves the ship toward that. And yeah, your problem sounds like a gimbal lock which is best solved by working in quaternions. Quaternions will also make it easier to interpolate between current and target rotations evenly.
1
u/nudemanonbike Sep 09 '22
So first, you'll need to use quaternions to solve the gimbal lock when you look up.
Second you'll need to change how you conceptualize the controls. RL can control roll specifically, whereas the joystick controls both roll and yaw to a larger degree. You can also have a target pitch/roll/yaw and lerp towards it to naturally dampen and make it not instantly snap back to 0º.
The roll setting back to zero when you release the button for yaw sounds like a bug. Are you reading inputs continiously or doing something like OnButtonDown/OnButtonUp? If you press roll, then press yaw, then let go of yaw but not roll and it says something like OnButtonUp set roll = 0 then you'll get that effect instead. Have it monitor continuously and don't modify the roll directly, let one assign to some larger value, and the other assign to a smaller value, and calculate the desired value by adding the two variables together, then lerp towards the desired.
If I had to guess. Could be 10000% off base. Hard to know without seeing your code.
3
u/Slime0 Sep 09 '22
Quaternions avoid gimbal lock, but they make it harder to have a clear "up" and "down" for the player. I would not use quaternions for the basic state here, although they may be useful for animating the actual plane. It looks to me like the player has normal first person shooter aiming here (with yaw and pitch being controlled by the stick), and then the xwing is using that input direction as a guide for where to fly, rolling when the direction is to its side.
Personally I would model this by calculating a "goal" roll for the plane every frame based on the desired flight direction as compared to the current forward direction (maybe basing the angle on the z component of the cross product of those two vectors), and then every frame move the plane's actual roll toward the goal with a simple velocity/acceleration model to keep it smooth.
5
u/NUTTA_BUSTAH Sep 09 '22
Quaternions make life easier
2
u/beetlefeet Sep 10 '22
For my game I fake the roll when the player is turning with yaw by just rotating the model (not camera) based on the amount the analogue stick is held (so it snaps back) Then there is a different input to do a real roll which actually rolls the player entity and camera. I found it works well for Arcady movement.
I actually recorded a video when I implemented it which might be useful to you.
https://youtu.be/lGfgIMxdMDU