Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Move 3d object in direction it faces

    Beginners
    3
    9
    682
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • poof92
      poof92 last edited by Dave

      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 with c.ly then it goes in a fixed direction I've even used the 3d tutorials but to no avail

      1 Reply Last reply Reply Quote 0
      • PickleCatStars
        PickleCatStars F last edited by Dave

        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...

        1 Reply Last reply Reply Quote 2
        • Dave
          Dave Fuze Team last edited by Dave

          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!

          1 Reply Last reply Reply Quote 1
          • PickleCatStars
            PickleCatStars F last edited by

            You could use that side vector to shoot missiles out of the side of your car.

            Dave 1 Reply Last reply Reply Quote 2
            • Dave
              Dave Fuze Team @PickleCatStars last edited by

              @toxibunny I second, third and fourth this idea! What a perfect application!

              1 Reply Last reply Reply Quote 0
              • poof92
                poof92 last edited by

                Awesome thanks so much I'll give it a try

                1 Reply Last reply Reply Quote 0
                • poof92
                  poof92 last edited by

                  It works for the most part but the object doesn't move towards its face.

                  1 Reply Last reply Reply Quote 0
                  • PickleCatStars
                    PickleCatStars F last edited by

                    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.

                    1 Reply Last reply Reply Quote 0
                    • poof92
                      poof92 last edited by

                      Awesome it works perfectly now thanks

                      1 Reply Last reply Reply Quote 2
                      • First post
                        Last post