Thursday, 30 May 2013

Some Code Snippets

Here I'm going to add some little bits of code that I find handy. Might be some use to somebody.

Desaturate a Colour in C#/XNA

```//c = the colour to desaturate
//amount = the amount of desaturation between 0 and 1
Color desaturate(Color c, float amount)>
{
Color gray = new Color((c.G * 0.59f), (c.R * 0.3f), (c.B * 0.11f));
Color newC = Color.Lerp(gray, c, amount);

return new Color(newC.R, newC.G, newC.B, 255); //the extra 255 is the alpha value. include this to make sure the color is fully opaque
}```

Get Angle Between Two points in C#/XNA

```//p1 = start point
//p2 = end/destination point
public float getAngleFromPoints(Vector2 p1, Vector2 p2)
{
float angle = (float)Math.Atan2(p2.Y - p1.Y, p2.X - p1.X);

if (angle < 0)
{
angle += (float)(2 * Math.PI);
}

return angle;
}```

Convert Between Degrees and Radians in C#/XNA

```//r = the angle in radians to convert
{
return (float)(r * 180 / Math.PI);
}```

```//d = the angle in degrees to convert
{
return (float)(Math.PI * d / 180);
}```

Detect keyboard and controller input in C#/XNA

```public class MyKeyboard
{
private KeyboardState oldState;
private KeyboardState newState;

public MyKeyboard()
{
oldState = Keyboard.GetState();
}

//call this at the start of your game loop
public void init()
{
newState = Keyboard.GetState();
}

//call this at the end of your game loop
public void reset()
{
oldState = newState;
}
/////////////////////////////////////////////////////////////////////////////////
//check if a key is being held down
public bool keyDown(Keys k)
{
if (newState.IsKeyDown(k))
{
return true;
}

return false;
}

//check if a key has just been pressed
public bool keyPressed(Keys k)
{
if (newState.IsKeyDown(k) && oldState.IsKeyDown(k) == false)
{
return true;
}

return false;
}

//check if a key has just been released
public bool keyReleased(Keys k)
{
if (newState.IsKeyDown(k) == false && oldState.IsKeyDown(k))
{
return true;
}

return false;
}
}```

```public class MyController
{

public MyController()
{
}

//call this at the start of your game loop
public void init()
{
}

//call this at the end of your game loop
public void reset()
{
oldState = newState;
}
/////////////////////////////////////////////////////////////////////////////////
//check if a button is being held down
public bool buttonDown(Buttons b)
{
if (newState.IsButtonDown(b))
{
return true;
}

return false;
}

//check if a button has just been pressed
public bool buttonPressed(Buttons b)
{
if (newState.IsButtonDown(b) && oldState.IsButtonDown(b) == false)
{
return true;
}

return false;
}

//check if a button has just been released
public bool buttonReleased(Buttons b)
{
if (newState.IsButtonDown(b) == false && oldState.IsButtonDown(b))
{
return true;
}

return false;
}
////////////////////////////////////////////////////////////////////////////////////
//get the value of the left thumb stick
public Vector2 leftStick()
{
return (new Vector2(newState.ThumbSticks.Left.X, newState.ThumbSticks.Left.Y));
}

//get the value of the right thumb stick
public Vector2 rightStick()
{
return (new Vector2(newState.ThumbSticks.Right.X, newState.ThumbSticks.Right.Y));
}

//get the value of the left trigger
public float leftTrigger()
{
return newState.Triggers.Left;
}

//get the value of the right trigger
public float rightTrigger()
{
return newState.Triggers.Right;
}
}```

Wednesday, 22 May 2013

Easing/Tweening

So a useful tool for game development is a technique called "easing" or "tweening". This is often used for skeletal animation, but is also useful for other tasks. Basically what it involves is updating a value between a start and end point over time. So for example if I wanted to move a sprite across the screen along the x axis starting at x = 200 and ending at x = 1000 for a duration of 1.5 seconds, then the x position of the sprite could be tweened between these two values.

Below I will show you four quadratic easing functions (language is C#, but you can adapt these functions to whatever language you are using):

All the functions will have the same parameters:
currentTime: this is the current time of the tween
start: this is the start value of the tween
diff: this is the difference between the end and start value
length: this is the total time of the tween

Linear (the value will be changed at a constant rate):

```public float Linear(float currentTime, float start, float diff, float length)
{
return diff * currentTime / length + start;
}```

Ease Out (the value will change quickly at first and then slow down)

```public float EaseOut(float currentTime, float start, float diff, float length)
{
currentTime /= length;
return -diff * currentTime*(currentTime-2) + start;
}```

Ease In (the value will change slowly at first and then speed up)

```public float EaseIn(float currentTime, float start, float diff, float length)
{
currentTime /= length;
return diff*currentTime*currentTime + start;
}```

Ease In Ease Out(the value will change slowly at the beginning and end, and change quickly in the middle)

```public float EaseInOut(float currentTime, float start, float diff, float length)
{
if ((currentTime /= length / 2) < 1)
{
return diff / 2 * currentTime * currentTime + start;
}

return -diff / 2 * ((--currentTime) * (currentTime - 2) - 1) + start;
}```

Example:
So let's use the example from above. We want to move a sprite along the x axis between x = 200 and x =  1000 for a duration of 1.5 seconds. We would set up the parameters like this:

currentTime: you'd need to set up a timer for the tween and update it using your game engine/framework's delta time (time between last frame and current frame)
start: in this case 200
diff: in this case 1000 - 200 = 800
length: in this case 1.5

so call this line every frame:
```spriteX = Linear(timer, 200, 800, 1.5);
```

Tuesday, 21 May 2013

New Blog

So in the interest of letting people know what I'm doing on a (hopefully) regular basis instead of hiding in my cave and then pouncing on the internet with a finished game that no one has ever heard of, I'm setting up this blog where I will post updates on project progress and other random gamedev stuff.

Enjoy :)