Applying an impulse to a RigidBody3D based on rotation
Asked Answered
P

3

0

Hi, I'm trying to make a 3D football game, and to kick the ball I'm trying to use apply_impulse() (obviously if this is not the best way, that can be changed)
The only issue is that I don't understand physics all too well, and I don't know what parameters I need to pass in, in order to apply an impulse that takes into account the player's rotation degrees, and therefore ends up 'kicking' the ball in the direction the player is facing. There is also literally no tutorial videos that end up including this or forum questions from what I can see, so I'm a little bit in the dark and alone despite knowing that other people have definitely been through this as well lol.
If anyone can put some code that would end up kicking the ball off in the direction the player is facing, that would be amazing, but any help at all is greatly appreciated! 🙂

Polyzoan answered 25/2, 2023 at 8:19 Comment(0)
S
0

Looks like you need a forward vector.

Document: https://docs.godotengine.org/en/stable/tutorials/3d/using_transforms.html#obtaining-information

var football : RigidBody3D
var player : Node3D
var impulse_power : float = 100.0

func _kick(football : RigidBody3D):
	var forward : Vector3 = player.get_global_transform().basis.z
	football.apply_impulse(forward * impulse_power)
Sectary answered 25/2, 2023 at 12:46 Comment(0)
P
0

Sectary I've put this in but it's saying the apply_impulse function now has too few arguments?

I ended up changing it to:

as the second argument also needed to be a vector3, and then flipped the forwards vector, which seems to be working.

Thank you so much for the help though- a push in the right direction was all I needed 🙂

Polyzoan answered 26/2, 2023 at 12:20 Comment(0)
S
0

Sectary In Godot the forward vector is -basis.z and not basis.z.

Scandic answered 26/2, 2023 at 20:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.