by YoshiInAVoid » May 18th, 2011, 4:47 pm
Accelleration is simple. First off we need to talk about it in a larger scale so as to be simpler, then we will add in the bitshifts to use it in a real situation.
int X, XV;
XV+Pad.Held.Right&&XV<5;
X+=XV;
This is the simple and basic code. The top speed being 5 and the speed it accelerates at being the defult 1. Thier is no code that will de accelerate after letting go, just simple acceleration when you hold right.
Now lets brake it down alot so as it's easy to come to terms with.
int X = 0;
int XV = 0;
if (Pad.Held.Right) {
if (XV < 5) {
XV = XV+1;
}
}
X = X+XV;
There we go, thats very simple, but took a fricking year to type out! As aposed to the 3 line alternative.
So anyway, whats happening is that when you hold right, and XV is not at the max speed, then we can increse XV. Then we move the players X forward depending on the value of XV. So as XV starts off as 0 we will be adding 0 to X, then if you tap the button for a frame it will turn XV to 1 so the player will move at 1 pixel per frame, then hold it down for a frame more and it will move at 2 pixels per frame, and so on until it reaches 5 where no matter whether your holding right or not it will still go at 5 per frame and no faster.
Simplez no?
So now we test! It turns out 5 pixels per frame is waaaayyyyyyyy too fast! How about 2? Then we test, it only takes 2 frames to move XV from 0 to 1, and then to 2 which is hardly noticeable at all, you may as well not use acceleration and make it go straight from 0 to 2.
So what do we do??? BITSHIFTS ARE THE ANSWER TO ALL YOUR PROBLEMS AND MORE!
So, to keep this extra extra simple, we will only <<1 which is the same as timesing by 2, then that means that we can move out player half a pixel a frame, so it goes through 0, to 0.5, to 1, to 1.5, to 2. Twice as many frames, meaning it will take twice as long, and twice as smooth.
So here is that example again:
int X = 0<<1; //The <<1 means multiply by 2
int XV = 0;
if (Pad.Held.Right) {
if (XV < 4) { //4 Is 2<<1 or 2*2 or two multiplyed by two, take your pick!
XV = XV+1;
}
}
X = X+XV;
Preffered method of moving graphic to X position(X>>1);
There we go. What happens do you ask? Well when we hold right, ensted of 2 being the limit, we increse it to 4, this means it will take twice as long to get to the max speed, but also it will move twice as fast wehn it reaches that, so... Then at the end, we move the graphic or sprite to the value of X divided by two, this means that it does not infact go twice as fast, but it will takes twice as long to get to the max speed.
So lets wither that down to the simple code that takes not nearly enough space but does the same thing, (optional):
int X, XV;
XV+Pad.Held.Right&&XV<4;
X+=XV;
Then just remember that when you use PA_SetSpriteX or whatever DSGM does, make the X paramter X>>1
Sorry, Jack, YoshiInAVoid's back!
I'm the original; I'll take your breath away! This fall, YoshiInAVoid rules! Did you miss me, Andy? I sure missed you!
Keep an eye out for me.