Changes

4,611 bytes added ,  19:00, 29 August 2008
Line 1,188: Line 1,188:     
  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:
 
  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:
 +
 +
Figure 1: Returns false; Figure 2: Returns true
 +
[[Image:Flexfig1.png‎]]  [[Image:Flexfig2.png‎]]
 +
 +
Figure 1 would return false as they aren’t colliding. Whereas, figure 2 would return true because they are colliding.
 +
 +
Ok, so now we know weather or not they are colliding, but that doesn’t automatically stop them from moving through the object. Usually you will want to stop the image once it is touching the border of the other image, but not overlapping at all that would mean stopping the sprite2 from moving further right in any of the given situations:
 +
 +
[[Image:Flexfig3.png]]
 +
 +
Figure 3: sprite2 will be unable to move any further right without moving above sprite
 +
 +
So how we do we stop sprite2 from moving through sprite when CollidesWith() returns true? You would set up your main loop something like this:
 +
 +
''NOTE: This example assumes you have a structure or class that holds sprite2’s x and y position called Player.''
 +
 +
 +
<source lang="cpp">int main()
 +
{
 +
    init stuff
 +
    load sprite images etc
 +
 +
    while(true)
 +
    {
 +
        int oldX = Player.x;
 +
        get input from wiiMote
 +
        if ( sprite2.CollidesWith(&sprite))
 +
            Player.x = oldX</source>
 +
 +
The previous section of code gets the players x position at the start of every loop and saves it in an int. Then, it makes a call to CollidesWith() and tests if sprite is colliding with sprite2, if this returns true then Player.x will be changed to whatever it was at the start of that loop. You have to make sure that you lay out the get x, THEN input from the wii mote then call the collision function. If you don’t understand why this must be done, I’ll walk you through an example, say Player.x = 100, when it gets to
 +
 +
<source lang="cpp">int oldX = Player.x;</source>
 +
 +
 +
oldX  will be assigned the value of Player.x (100), further, we are pressing left so when it gets the input Player.x is decreased by, say 2, now Player.x = 98. Next thing that happens is that we check for collision, collision returns true therefore we don’t want Player.x to equal 98, so because collision is true Player.x is now assigned the value of oldX which is 100, so now we are moved back to the x location, 100, therefore, stopping the player from moving left even though we are pressing left. Some people have troubles, when collision is true and they try to move away, they become locked onto the image and are unable to move, due to collision being tested after the input this is avoided and works fine for being able to move away.
 +
 +
Now, you may have noticed all of the calls CollidesWith() have only used the first parameter, I’ve stayed away completely from it. So now I shall reveal its functionality. Due to the nature of the collision we’ve been using, it will only work for images at their native resolution and not rotated (amongst other things) so that’s why we have the second parameter. Set it true if you want to add collision detection for objects that are rotated or zoomed, if you aren’t sure how to go about this:
 +
 +
<source lang="cpp">sprite2.CollidesWith(&sprite, true);</source>
 +
 +
There is a cost associated with this though, if you don’t need to check collision on something that’s rotating or zooming, don’t. It’s not very good programming practice and will lower the performance of your program.
 +
 +
One thing that can eat your programs performance is collision detection, for this reason I will introduce a method of vastly improving collision detection speed. Imagine a situation where you have to check 100 different collisions every single loop, it will take a damn long time for your loop to complete. On top of that, if you are detecting collision for images that are zoomed and not at native resolution as well as rotated, you will have a noticeable performance loss.
 +
 +
Chances are, most of those checks will return false, so we’ve just wasted a whole lot of cycles, so now we need a method of limiting the number of collision detections. A simple way of doing this is separating the ‘screen’ into a series of squares, known as nodes. You can’t see these squares on the screen though, consider them imaginary. Say we split our screen into 4 equal nodes, the nodes may look like figure 4.
 +
 +
[[Image:Flexfig4.png]]
 +
 +
The numbering system is simply there so I can explain things easier, so from now on I will refer to the top right node as Node1, the top right Node2 and so on.
 +
 +
Now, you may be wondering “how do we set up these nodes?”, I will do it using Libwiisprites Rectangle object, so we set up four nodes:
 +
 +
Rectangle Node1, Node2, Node3, Node4;
 +
 +
Rectangle is a structure defined as:
    
== EXAMPLE PROGRAMS  ==
 
== EXAMPLE PROGRAMS  ==
48

edits