Zed
Zed

Reputation: 671

XNA coasting/easing between positions

Game overview:


I'm attempting to make a game where the camera follows the player and follows the median route (so moving x+ then x- quickly results in little camera movement) and I've got it so the camera has its own acceleration but I'm not sure how to decelerate.

Values that are relevant

List<double> offset = new List<double>() { 0, 0 }; //Top left map to top left window (World is related to this)
List<double> smiley = new List<double>() { 0, 0 - 5 }; //Player position inside the map
List<double> smileyPos = new List<double>() { 400, 288 + 11 }; //Where the centre of the screen is for the player

List<double> scvel = new List<double>() { 0, 0 }; //Stores how many pixels to move per frame
int maxvel = 8; //Maximum amount of pixels to move per frame
double acc = 0.5; //acceleration (only)

            if (offset[0]+smiley[0] < smileyPos[0])
            {
                if (scvel[0] <= maxvel)
                {
                    scvel[0] += acc;
                }
            }
            if (offset[0]+smiley[0] > smileyPos[0])
            {
                if (scvel[0] >= -maxvel)
                {
                    scvel[0] -= acc;
                }
            }
            if (offset[0] + smiley[0] == smileyPos[0])
            {
                scvel[0] = 0;
            }
            if (offset[1] + smiley[1] < smileyPos[1])
            {
                if (scvel[1] <= maxvel)
                {
                    scvel[1] += acc;
                }
            }
            if (offset[1] + smiley[1] > smileyPos[1])
            {
                if (scvel[1] >= -maxvel)
                {
                    scvel[1] -= acc;
                }
            }
            offset[0] += scvel[0];
            offset[1] += scvel[1];

Output:

Camera smoothly moves to the player but orbits the player

Output I would like:

Camera smoothly eases to the player position with a slight delay to counteract any bumps

The way I have done the camera movement where the only angle is a multiple of 45 - how would I modify this code to allow the camera to go directly to the player and ease in and out?



Solution

offset = Vector2.Lerp(offset, new Vector2((float)(-smiley[0]+smileyPos[0]), (float)(-smiley[1]+smileyPos[1])), (float)0.05); 

and also changing List<int> offset to Vector2 offset

Upvotes: 2

Views: 333

Answers (1)

Steve H
Steve H

Reputation: 5529

Looks like a perfect use for Vector2.Lerp(position, goal, amount)

position == where the object is currently.

goal == where you want the object to smoothly go to.

amount == the percentage of the way it will cover this frame.

So the amount is set to somewhere between 0 and 1. if you set it to .5, it would cover half the distance to the goal each frame. If you think about it, since half the distance is an ever decreasing amount each frame, it will actually move less each frame giving it the appearance of decelerating as it approaches the goal.

You can find the best amount by trial and error. most likely a low number like less than 0.1.

Upvotes: 1

Related Questions