Jump to content
  • Advertisement
curator785

Vector3 How does it move?

Recommended Posts

if (!_ReachedDestination)
                {
                    // I'm not sure why you need to subtract the TranslationVector from the Waypoint which is the point you need to go to in world space?
                    var Direction = _Waypoint - Unit.Transform.WorldMatrix.TranslationVector;
                    // I'm not sure why you would divide the length here for the direction?
                    // Get distance towards next point and normalize the direction at the same time
                    var LengthToDestination = Direction.Length();
                    Direction /= LengthToDestination;
                    // Check when to advance to the next waypoint
                    bool WaypointAdvance = false;
                    // Check to see if an intermediate point was passed by projecting the position along the path
                    if (_PathToDestination.Count > 0 && _WaypointIndex > 0 && _WaypointIndex != _PathToDestination.Count - 1)
                    {
                        Vector3 PointNormal = _Waypoint - _PathToDestination[_WaypointIndex - 1];
                        PointNormal.Normalize();
                        // I think the Unit part is where the unit is in world space so we are doing a dot method to find the distance from our waypoint?
                        float Current = Vector3.Dot(Unit.Transform.WorldMatrix.TranslationVector, PointNormal);
                        float Target = Vector3.Dot(_Waypoint, PointNormal);
                        // If we are at our waypoint or passed it advance to the next waypoint?
                        if (Current > Target)
                        {
                            WaypointAdvance = true;
                        }
                    }
                    else
                    { 
                        // Check distance to final point
                        if (LengthToDestination < _DestinationThreshold)
                        {
                            WaypointAdvance = true;
                        }
                    }
                    // Advance waypoint?
                    if (WaypointAdvance)
                    {
                        _WaypointIndex++;
                        if (_ReachedDestination)
                        {
                            // Final waypoint reached
                            Stop(_ListUnit);
                            return;
                        }
                    }
                    // Calculate speed based on distance from final destination
                    // Slows the unit down or speeds it up...? based on how far away from the end point it is?
                    float moveSpeed = (_MoveDestination - Unit.Transform.WorldMatrix.TranslationVector).Length() * _DestinationSlowdown;
                    if (moveSpeed > 1.0f)
                    {
                        moveSpeed = 1.0f;
                    }
                    // Slow down around corners
                    // I know this puts an arc in the path but i dont understand why you would want that for a straight line on my 3d plane it still arcs a bit to its destination
                    // I need to figure otu how to make it go straight if there are no corners
                    float cornerSpeedMultiply = Math.Max(0.0f, Vector3.Dot(Direction, _MoveDirection)) * _CornerSlowdown + (1.0f - _CornerSlowdown);
                    // Allow a very simple inertia to the character to make animation transitions more fluid
                    // Adds everything up to try to provide a direction on the update game loop
                    _MoveDirection = _MoveDirection * 0.85f + Direction * moveSpeed * cornerSpeedMultiply * 0.15f;
                    
                    // Using the default character component to do the moving
                    _CharacterComponent.SetVelocity(_MoveDirection * _Speed);
                    
                    // Make the unit face the direction its traveling but at the end when it gets to its end point rotates back to the -z axis... not sure why it reverts to facing that way instead of just facing the way it was going
                    if (_MoveDirection.Length() > 0.001)
                    {
                        _YawOrientation = MathUtil.RadiansToDegrees((float)Math.Atan2(-_MoveDirection.Z, _MoveDirection.X) + MathUtil.PiOverTwo);
                    }
                    Unit.Transform.Rotation = Quaternion.RotationYawPitchRoll(MathUtil.DegreesToRadians(_YawOrientation), 0, 0);
                }
                else
                {
                    Stop(_ListUnit);
                }
}

Below is some code I pulled from our current project.  I'm trying to figure out how in the world it works.  Can someone take a look at my comments and tell me if its right or shine some light on the question I commented out?

 

Edited by curator785

Share this post


Link to post
Share on other sites
Advertisement

Before anybody wants to take a look at that, do you mind wrapping your code inside code tags please? The Wysiwig Editor you used to create that post, has a little button "< >"where you can format your code properly, it will increate the readability a lot. Thank you :)

Share this post


Link to post
Share on other sites
1 hour ago, curator785 said:

// I'm not sure why you need to subtract the TranslationVector from the Waypoint which is the point you need to go to in world space?

Vector - Vector = move to or away depending on what vector is fist.

Lets say your character is at point (1,1,0) and The target is (5,5,0), you want to do it in 10 steps. (1,1,0) - (5,5,0) = (-4,-4,0) So that is your new target.

(-4,-4,0)/10 = (-0.4f,-0.4f,0) is how much you move each step or (-4,-4,0) * 0.1f = (-0.4f,-0.4f,0).

Reverse: (5,5,0) and The target is (1,1,0) : (5,5,0) - (1,1,0) = (4, 4,0).

VectorExplain.jpg.5833e9f09c7b43fc88ae08ebb011a444.jpg

Vector + Vector is used after the Vector - Vector to move the amount.

(4,4,0) Was the answer for reverse so we know if we add (4,4,0) to (1,1,0) We reach that target. If we want it in 10 steps we do:

Speed = 0.1f

(4,4,0)*0.1f = (0.4f,0.4f,0). So now if we add that to (1,1,0) + (0.4f,0.4f,0) = (1.4f,1.4f,0) it moved one tenth up.

1 hour ago, curator785 said:

// I'm not sure why you would divide the length here for the direction?
                    // Get distance towards next point and normalize the direction at the same time

Vectors are also directions. Normalize only makes them range between 0-1. So (5,10,2) = (0.5f,1f,0.2f). This is done so we can use it easily as a direction by just using multiplication.

VectorDirections.jpg.e5697c2373ced919ef68f7a037f495e3.jpg

Length of a vector is calculated using Pythagoras.

Length.jpg.721af2da4cd6d7d3fbfd95e8d12ef664.jpg

1 hour ago, curator785 said:

// Slows the unit down or speeds it up...? based on how far away from the end point it is?

It's a scaler. So < 1 = slow >1 is faster 1 = same and -value = reverse :

(1,1,1) * 1 = (1,1,1) Same (1,1,1) * 0,5f = (0.5f, 0.5f, 0.5f) Slower  (1,1,1) * 2 = (2,2,2) Faster and (1,1,1) * -1 = (-1,-1,-1).

So that part moves the object exactly to that point. If it's near it slows down, far it speeds up and if it went past it will go back.

 

I hope this helps a bit. Vectors are high school level and there is lots of places to learn them on the net. 2D and 3D vectors work the same.

 

Edited by Scouting Ninja

Share this post


Link to post
Share on other sites

The 15 part video series in this link would definitely benefit you.  As Scouting Ninja pointed out, this is typically taught in high school math classes.

 

Share this post


Link to post
Share on other sites

  • Advertisement
  • Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By FoxerProduction
      Our production is currently looking to work remotely with Blender artists with Blender Game Engine experience.
      Requirements:
      Communicate fluently in English, skill in 3D design, character and environment designs which communicate personality and story, a professional understanding of artistic design principles including light, color, mood, composition, high creative intelligence, and bonus points if you have experience with character animation.
      We’re accepting of talented students and hobbyists with an excellent work ethic.
      Please respond via email to peter@stakoungroup.com with your interest as soon as possible for an interview.
      Thank you,
      The Foxer Production Team
    • By Aryndon
      Project Redemption is an semi-fantasy RPG with a linear story and an elaborate combat system.
      We are building in Unity and are currently looking animators and artists.
      What we are looking for
      -Someone who is okay with split revenue/profits when finished
      -Collaborate with others in the team. Do you have an idea/thought on what should be included? Tell us!
      -Someone who wants to work with people that are passionate about this project
      If you are interested. Please message me and I will get back to you as soon as possible! Or add me on Discord AJ#6664
    • By CocoaColetto
      I am basically brand new to the gaming industry business wise although I have been a gamer for years. I officially started my game publishing company, and being as though I am only 20, I have no connects to the gaming industry. Of course, I'm still going to do more internet research, but I thought why not ask folks who may have business hands in the gaming community? If anyone is questioning, my game prototype is basically done (I designed it myself) and its very detailed and I am going to start searching for a team to help me build it. Thank you. 
    • By recp
      Hello,
      How can I get center of scene or node or model? What is best way to do this? Scene structure:
      Scene   |   o - Node[s]            |            o - Model[s] // Mesh                       |                       o - Primitive[s] // Sub-Mesh                                    |                                    o local AABB and world AABB I'm using AABB's center as center of primitive and I'm combining all AABB boxes to build an AABB for scene. When I visualized the boxes it seems work as expected. 
      But I need to get center of scene, node or model for apply rotation around center. Because I'm using a trackball for rotating attached node or model. Currently I'm using scene's AABB's center as rotation point (pivot), for single object it is working. After rotation is completed center of primitive remains same which it should be, I think. But if I load a scene which contains multiple models or primitives, after rotation is completed center of scene's AABB is moving (I'm using that as center of scene). Because every time rotation is completed, new AABB is calculated for scene by combining all sub AABB boxes. I think this may be a normal because there is no balance between AABB boxes while rotating. For instance if I use two same CUBE without rotations center of new scene's AABB remains same.
      My solution (it seems work for now):
      I created new center member (vec3) in scene struct:
      scene->center    = vec3(0); scene->primCount = 0; for prim in primitivesInFrustum    scene->center += prim->aabb->center;    scene->primCount++; scene->center = scene->center / scene->primCount Now I'm using this center as center of scene instead of scene->aabb->center and it seems work.
      My question is that what is best way to get center of scene, node or model? How do you get the center for rotation? Any suggestions? Am I doing right? 
      Thanks
    • By bojanzarnoski@gmx.de
      Hello,
      I want to get into coding again by programming a 2D platformer to get started, but i don't know if i should use Java or C# with the unity engine.
      I am pretty fit with Java, but with c# i have to start from scratch. What do you recommend and why?
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!