Slide 2d Vector to destination over a period of time

Posted by SchautDollar on Game Development See other posts from Game Development or by SchautDollar
Published on 2012-10-02T16:51:24Z Indexed on 2012/10/02 21:53 UTC
Read the original article Hit count: 146

Filed under:
|

I am making a library of GUI controls for games I make with XNA. I am currently developing the library as I make a game so I can test the features and find errors/bugs and hopefully smash them right away.

My current issue is on a slide feature I want to implement for my base class that all controls inherit. My goal is to get the control to slide to a specified point over a specified amount of time. Here is the #region containing the code

#region Slide
    private bool sliding;
    private Vector2 endPoint;
    private float slideTimeLeft;
    private float speed;
    private bool wasEnabled;
    private Vector2 slideDirection;
    private float slideDistance;

    public void Slide(Vector2 startPoint, Vector2 endPoint, float slideTime) {
        this.location = startPoint;
        Slide(endPoint,slideTime);
    }

    public void Slide(Vector2 endPoint, float slideTime) {
        this.wasEnabled = this.enabled;
        this.enabled = false;
        this.sliding = true;
        Vector2 tempLength = endPoint - this.location;
        this.slideDistance = tempLength.Length(); //Was this.slideDistance = (float)Math.Sqrt(tempLength.LengthSquared());
        this.speed = slideTime / this.slideDistance;
        this.endPoint = endPoint;
        this.slideTimeLeft = slideTime;
    }

    private void UpdateSlide(GameTime gameTime) {
        if (this.sliding) {
            this.slideTimeLeft -= gameTime.ElapsedGameTime.Milliseconds;
            if (this.slideTimeLeft >= 0 ) {
                if ((this.endPoint-this.location).Length() != 0){//Was if (this.endPoint.LengthSquared() > 0 || this.location.LengthSquared() > 0) {
                    this.slideDirection = Vector2.Normalize(this.endPoint - this.location);
                }
                this.location += this.slideDirection * speed * gameTime.ElapsedGameTime.Milliseconds;//This is where I believe the issue is, but I'm not sure. It seems right to me... (Even though it doesn't work)
            }
            else {
                this.enabled = this.wasEnabled;
                this.location = this.endPoint;//After time, the controls position will get set to be the endpoint.
                this.sliding = false;   
            }
        }
    }
    #endregion 

this.location is the location of the control elsewhere defined in the class. I have looked at this blog as a huge reference and have googled around quite and have looked on many forums but can't find anything that shows how to implement it.

Please and Thanks for your time!

EDIT:

I have switched this line "this.location += this.slideDirection * speed * gameTime.ElapsedGameTime.Milliseconds;" several times to see what it does. My issue is getting the control to smoothly move to the end location. It moves after the time has expired, but It doesn't move other then that except flash in my face.

EDIT2:

I have used the first slide method with 3 parameters and it works except it doesn't do it in a period of time and once it gets to its destination, it starts moving randomly towards the previous location and the end location.

© Game Development or respective owner

Related posts about XNA

Related posts about c#