Move 3d object in direction it faces
-
So I've set up a little game where you have a basic player and I can rotate it with
c.lx
but when I push up withc.ly
then it goes in a fixed direction I've even used the 3d tutorials but to no avail -
sin() and cos() are probably what you want to use here. Fuze even has a sincos() function that’ll do them both at once.
Assuming a fixed camera, and if I’m remembering correctly, If you do something like
Player.x += sin(player.angle)*c.ly Player.z += cos(player.angle)*c.ly
That should at least give you an idea to get you started...
-
The above is correct - but we can make something useful here which is referred to as a forward vector. This is a calculated direction vector which takes the sin() and cos() of the player angle into account - and it can be used to get other useful vectors as well, such as the 'side' vector - which describes the direction to the left and right of the forward (think holding your arms out in a T shape).
For this I'll assume you have a vector to describe the player position as opposed to separate x, y and z variables.
// You might also want something to adjust the angle the player is facing, for example the right stick X axis: playerAngle += c.rx playerForward = {cos(playerAngle), 0, sin(playerAngle)} // 0 on the Y axis assuming you don't want to fly around but move on the ground. playerPos += playerFoward * c.ly // Add the player forward vector to player position, using the controller Y axis value as a modifier.
You will likely need to modify the controller variables (.ly/.rx etc) to make the numbers move nicely in your project, depending on the scale of things and such. Usually multiplying them by zero point something is a good place to start.
To get the side vector, we can take the cross product of the player forward vector and the pure Y axis. This would give us left and right movement, mapped to something like the left stick X axis this creates complete movement!
playerSide = cross(playerForward, {0, 1, 0}) playerPos += playerSide * c.lx
Apologies if it's more info than needed, but just wanted to make sure you had this to refer to if you require it!
-
You could use that side vector to shoot missiles out of the side of your car.
-
@toxibunny I second, third and fourth this idea! What a perfect application!
-
Awesome thanks so much I'll give it a try
-
-
If it’s off by 90 degrees or whatever, you can always just bodge it.
playerForward = {cos(playerAngle +90 ), 0, sin(playerAngle +90 )}
For example.
-
Awesome it works perfectly now thanks