• Advertisement

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Manuel Berger
      Hello fellow devs!
      Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
      My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

      In Pseudo-Code:
      angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
      index = (int) (angle / (PI / 5));
      PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

      Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

      What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

      I can't get my head around it. Any suggestions? Is the whole approach doomed?

      Thank you in advance for any input!
       
    • By Alexander Nazarov
      Hello. I'm newby in Unity and just start learning basics of this engine. I want to create a game like StackJump (links are below). And now I wondering what features do I have to use to create such my game. Should I use Physics engine or I can move objects changing transform manually in Update().
      If I should use Physics can you in several words direct me how can I implement and what I have to use. Just general info, no need for detailed description of developing process.
      Game in PlayMarket
      Video of the game
    • By _RoboCat_
      Hi,
      Can anyone point me into good direction how to resolve this?
      I have flat mesh made from many quads (size 1x1 each) each split into 2 triangles. (made procedural)
      What i want to achieve is : "merge" small quads into bigger ones (show on picture 01), English is not my mother language and my search got no result... maybe i just form question wrong.
      i have array[][] where i store "map" information, for now i'm looking for blobs of same value in it -> and then for each position i create 1 quad. and on end create mesh from all.
      is there any good algorithm for creating mesh between random points on same plane? less triangles better. Or "de-tesselate" this to bigger/less triangles/quads?
      Also i would like to find "edges" and create "faces" between edge points (picture 02 shows what i want to achieve).
      No need for whole code, just if someone can point me in good direction would be nice.
      Thanks


    • By Karol Plewa
      Hi, 
       
      I am working on a project where I'm trying to use Forward Plus Rendering on point lights. I have a simple reflective scene with many point lights moving around it. I am using effects file (.fx) to keep my shaders in one place. I am having a problem with Compute Shader code. I cannot get it to work properly and calculate the tiles and lighting properly. 
       
      Is there anyone that is wishing to help me set up my compute shader?
      Thank you in advance for any replies and interest!
    • By Dave Haylett
      Hi all. My project is coming along wonderfully, and am starting to consider alpha deployment, and would like your advice.
      My project need access to 10,000 small PNG image files at runtime, each is only a few kilobytes each, which during development I used to load in directly from a fixed path on my HDD whenever one was needed (obviously not a solution for go-live), using something like this:
      img = new WriteableBitmap(new BitmapImage(new Uri(@screenshotsPath + filename)));
      The image would then be blitted onto a buffer screen, etc. etc. At a time, a few dozen would be being used.
      Now I'm thinking about deployment, and also when I produce an update to my app, there could be more images to add to the folders. So I'm considering the best way of a) deploying the images to the user as part of the project, and b) how to most easily handle updates to the app, whereby more images will be added.
      I have just experimented with adding them all as a Resource (!). This inflated the exe from 10mb to 100mb (not a major problem), increased the compile time from 3 secs to 30 secs (annoying), increased RAM usage from 500mb to 1.5gb (not a major problem either), but means that it solves my fixed directory issue, distribution issue, and update issue, simply by having the files all stuck into the executable. Here's the new code I'm using:
      img = BitmapFactory.FromResource("Shots/" + filename);
      The next thing I was going to try was to mark them as Content > Copy if Newer. This would resolve the executable size and RAM usage (and also the directory issue as well), however it seems that I'd need to highlight them all, and move them from Resource to Content. As an up-front job this isn't too bad, but as I add new images to the project, I'll need to go in and do this every time, which gets annoying, as the VS2015 default is Resource. Also, I'm not sure how this would work in terms of updates. Would something like ClickOnce deployment recognise new PNGs and install them to the users?
       
      I also have 3,000 ZIP files (~500kb each) which also need deploying and updating in the same way. These are currently read directly from my HDD until I can find a permanent solution for adding these to the project as well.
      Can anyone thing of a better way of doing what I'm trying to achieve?
      Thanks for any help folks.
       
  • Advertisement