Changes

2,505 bytes added ,  20:28, 28 August 2008
Line 1,166: Line 1,166:     
== Collision Detection ==
 
== Collision Detection ==
 +
 +
Most games these days will have some form of collision detection weather you know it or not, in fact every game I can think of has some form of collision detection weather it stops you moving off the screen or Unreal Tournament 3’s complete physics engine, it all has some form of detecting collision. I will go over the collision detection functions that Libwiisprite supports and how to use them, along with a method of optimizing your collisions using a node based system.
 +
 +
An introduction to what collision detection is: say you’ve just made your game with some pretty graphics, for example, a wall and a box. You know that in real life when your box hits the wall it will stop, sadly though, your game is not real life, therefore you will simply move through (under or over if it’s 2D) your wall that you worked so hard to put there.  So now we need a system that will tell use when our box is touching the wall then we act on it to stop it moving.
 +
 +
This would require some math and a lot of GetWidth() calls, but, Libwiisprite has brought us a bunch of functions that will allow us to check collision without having to even know what math is. First function regarding collision is CollidesWith(), if you have looked at the documentation, you will see 3 different definitions for CollidesWith(), we will first look at the second which looks like this:
 +
 +
<source lang="cpp">bool wsp::Sprite::CollidesWith ( const Sprite * sprite,
 +
bool complete = false
 +
) const</source>
 +
 +
Checks if another sprite collides with this sprite.
 +
 +
<source lang="cpp">Parameters:
 +
sprite The sprite to check.
 +
complete Set this to true, if you also want to use zoom and rotation with the collision detecting.</source>
 +
 +
Now, for a lot of people that are new to collision detection or programming in general, this will make little sense, I’ll show you a small example on how to use it. First we set up two sprite objects (lets assume that we have already passed it an Image object), called sprite and sprite2. This is how we would pass them to the function:
 +
 +
<source lang="cpp">sprite.CollidesWith(&sprite2);</source>
 +
 +
This will test both sprite and sprite2’s locations in terms of their x and y values AS WELL as their x values +width and height values + height. If sprite and sprite2 are colliding then the function will return true else it will return false. If your still a little unsure about what will return true and what will return false have a look at this and behold my awesome Paint skills:
    
== EXAMPLE PROGRAMS  ==
 
== EXAMPLE PROGRAMS  ==
48

edits