Applying force with apply_force releative to rotation

4.3
Hey so is there anyway I can use the apply_force function in relative to a objects rotation. My game is 3D and third-person. Like how in a first person game WASD works in relative to the way you are looking at. And also, im using a rigidbody3D, a character body is not a option for my game.

func _input(event: InputEvent) -> void:
	
	if event is InputEventKey:
		if Input.is_action_pressed("rotate_left"):
			apply_force(Vector3(-1.0,0.0,0.0), Vector3(0.0,2.5,0.0))
		elif Input.is_action_pressed("rotate_right"):
			apply_force(Vector3(1.0,0.0,0.0), Vector3(0.0,2.5,0.0))
		elif Input.is_action_pressed("rotate_foward"):
			apply_force(Vector3(0.0,0.0,-1.0), Vector3(0.0,2.5,0.0))
		elif Input.is_action_pressed("rotate_backward"):
			apply_force(Vector3(0.0,0.0,1.0), Vector3(0.0,2.5,0.0))

also if you guys have any ideas on how to make this code more efficient that would be helpful thanks

What behavior does this code exhibit, and what behavior do you want it to make?

To make it slightly shorter, you could make a temporary constant that is Vector3(0.0, 2.5, 0.0) with a short name and replace the position argument with it. That way, you can change them by changing only one number, and the lines are shorter.

To fix your problem, you could create a dictionary of the force vectors, and on key press, grab the vector and rotate it by the rigid body rotation vector.

I don’t know if this helps too.

could you possibly provide a example of the using vector3.rotate in my situation. ill provide the full script

extends RigidBody3D

@onready var center = $center
@onready var camera: Camera3D = $center/SpringArm3D/Camera3D
@onready var spring_arm: SpringArm3D = $center/SpringArm3D

var rotation_vector3: Vector3

const rocket_height = Vector3(0.0,2.5,0.0)

func _ready() -> void:
	pass


func _physics_process(delta: float) -> void:
		
	if Input.is_action_pressed("thrust_active"):
		apply_force(Vector3(0,15,0))
	
	
func _input(event: InputEvent) -> void:
	
	if event is InputEventKey:
		if Input.is_action_pressed("rotate_left"):
			apply_force(Vector3(-1.0,0.0,0.0), rocket_height)
		elif Input.is_action_pressed("rotate_right"):
			apply_force(Vector3(1.0,0.0,0.0), rocket_height)
		elif Input.is_action_pressed("rotate_foward"):
			apply_force(Vector3(0.0,0.0,-1.0), rocket_height)
		elif Input.is_action_pressed("rotate_backward"):
			apply_force(Vector3(0.0,0.0,1.0), rocket_height)
	
	
	if event is InputEventMouseButton:
		Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
		
		if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
			camera.position.z += 0.5
			spring_arm.spring_length += 0.5
		elif event.button_index == MOUSE_BUTTON_WHEEL_UP:
			camera.position.z -= 0.5
			spring_arm.spring_length -= 0.5
	elif event.is_action_pressed("ui_cancel"):
		Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
	if Input.MOUSE_MODE_CAPTURED:
		if event is InputEventMouseMotion:
			center.ROTATION_EDIT_MODE_EULER
			center.rotation.y -= deg_to_rad(-event.relative.x*0.1)
			center.rotation.x -= deg_to_rad(-event.relative.y*0.1)
			center.rotation.x = clamp(center.rotation.x	, deg_to_rad(-90), deg_to_rad(90))
			
			

im trying to get apply_force to change with the rotation of $center

What type of node is $center?

Node3D its just a pivot point for the camera

Maybe something to this effect?

func _input(event: InputEvent) -> void:
	
	if event is InputEventKey:
		if Input.is_action_pressed("rotate_left"):
			apply_force(Vector3(-1.0,0.0,0.0).rotated($center.transform.basis.z, $center.rotation.z), rocket_height) #if the $center node is PI / 4 rad left, rotate the force vector by that rotation.
		elif Input.is_action_pressed("rotate_right"):
			apply_force(Vector3(1.0,0.0,0.0).rotated($center.transform.basis.z, $center.rotation.z), rocket_height)
		elif Input.is_action_pressed("rotate_foward"):
			apply_force(Vector3(0.0,0.0,-1.0).rotated($center.transform.basis.x, $center.rotation.x), rocket_height)
		elif Input.is_action_pressed("rotate_backward"):
			apply_force(Vector3(0.0,0.0,1.0).rotated($center.transform.basis.x, $center.rotation.x), rocket_height)

Just rotate the force vectors by the rotation of your $center node. I don’t do much 3d, so 3d transforms are confusing to me. This code is untested and will probably not work first try. Let me know if you can figure it out. Link to transforms documentation here.

sadly no effect. i might go to a different project and see what i can get working with vector3.rotate. thanks for help ill keep trying

I was just thinking this morning that you might have to also rotate the rocket_height variable if it’s in global coordinates. If they are all in local coordinates, this shouldn’t be an issue.