Vector3 How does it move?

Started by
4 comments, last by curator785 6 years, 2 months ago

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?

 


Thou Curator : Indie Game Development Studio
http://www.thoucurator.com

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 :)

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.

 

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.

 

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

@Scouting Ninja Hey thanks bud yea I been reviewing the tutorial at https://www.khanacademy.org/math/linear-algebra

I'm one of the hobbyist for http://www.thoucurator.com trying to learn more about game programming.  Your post help me kind of solidify the stuff I've been working on so thank you.

 

 


Thou Curator : Indie Game Development Studio
http://www.thoucurator.com

This topic is closed to new replies.

Advertisement