#How do I make physics impulses relative to the camera?

1 messages · Page 1 of 1 (latest)

bleak flicker
#

I was following Godot Academy's tutorial for a ball rolling game to get my game working, but I realised at the last moment that the game has a fixed camera. How would I get these controls to adjust for the direction you're looking?

bleak flicker
#

Sorry for bumping this, but I'm working on a tight deadline

hollow lion
#

You can get the forward facing vector of your camera with camera.get_global_transform().basis.z

bleak flicker
#

How would I apply this to the physics impulse?

#

I remembered somewhere else in my code that I was already getting these for regular movement, hence the different syntax

bleak flicker
#

Bump

bleak flicker
#

Sorry to be pushy, but this is the last hurdle before I can tinker on my own

solid shale
#
# Look up the parameters it needs! But anyway, get the sum of keys the user pressed.
var user_pressed := Input.get_vector("move_left", ..., "move_down") 
# Stick it into 3space so we can correctly rotate it. AFAICT this matches your preferred movement angles, where forward goes to a negative x rotation, left goes to a positive z rotation etc.
var absolute_pressed := Vector3(user_pressed.y, 0, -user_pressed.x)
# Transform the direction they pressed into a camera-relative value.
# This is _actually_ relative; some amount of their press might go into the y dimension. So if you're picky about that, you might need to normalize this value, and apply the roll_speed * delta transform after projecting it back into the plane.
# Or: apply only the yaw aspect of the camera's transform, and ignore its pitch and roll (fetch the euler angles and cancel out the x and z components retaining only y, then make that into a basis and apply only that transform).
var relative_pressed := _camera.global_basis * absolute_pressed
# Scale to desired length. See prev note about reprojection into the plane if that's an issue for you.
var scaled_pressed := roll_speed * delta * relative_pressed
# Apply the rotations as you had them before. Maybe this could just be `angular_velocity += scaled_pressed`, I haven't thought it through :-D
angular_velocity.x += scaled_pressed.x
angular_velocity.z += scaled_pressed.z
#

a lot of your question is about geometry & vectors more than programming; read through https://docs.godotengine.org/en/stable/tutorials/math/vector_math.html (and its friends like the Advanced Vector Math section, Matrices & Transforms, etc)