Changes

4,632 bytes added ,  07:57, 30 August 2008
Line 1,240: Line 1,240:  
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:
 
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;
+
<source lang="cpp">Rectangle Node1, Node2, Node3, Node4;</source>
    
Rectangle is a structure defined as:
 
Rectangle is a structure defined as:
 +
 +
<source lang="cpp">Public Attributes
 +
f32 x
 +
 +
X position.
 +
f32 y
 +
 +
Y position.
 +
f32 width
 +
 +
Width of rectangle.
 +
f32 height
 +
 +
Height of rectangle.
 +
 +
Detailed Description
 +
 +
Basic data for a rectangle.</source>
 +
 +
Now we need to fill these attributes.  To simplify things, let’s say our screen resolution is 800x600, now we need to separate 800x600 into 4 equal squares like in figure 4. So we take the resolution and divided it by square root of n number of nodes, so square root of 4 is 2, 800 x 600 divided by 2 = 400 x 300, this is the size of each of our nodes and will be filled in as follows:
 +
 +
<source lang="cpp">Node1.height = 300; Node1.width = 400;</source>
 +
 +
And continue on for the rest of the nodes filling in the same values. Next we need the x and y values of the nodes so the program knows where they are on the screen. This is easily done considering the top left of the screen to be (0,0) and x increases moving left with y increasing moving down the screen, look at figure 5 for a visual representation of all coordinates.
 +
 +
[[Image:Flexfig5.png]]
 +
 +
If you recall, all coordinates for images (and objects alike) are for the top left corner of the image/object, so our x and y values would look like this:
 +
 +
<source lang="cpp">Node1.x = 0; Node1.y = 0;
 +
Node2.x = 400; Node2.y = 0;
 +
Node3.x = 0; Node3.y = 300;
 +
Node4.x = 400, Node 4.y = 300;</source>
 +
 +
If you still don’t see where I got the values look at the coordinates at the top left corner of every node, they correspond to the values filled in for their x and y values.
 +
 +
Next we check collision against each of the nodes, not the objects that we are actually colliding with within the nodes, we don’t care about weather or not we are colliding with them (note, some additional coding may be required dependant on how your program is set up), so we would do it like this:
 +
 +
<source lang="cpp">If (Player.CollidesWith(&Node1)) {  // check node1
 +
  // other stuff here }
 +
 +
If (Player.CollidesWith(&Node2)) { // check node2
 +
  // other stuff here }
 +
 +
If (Player.CollidesWith(&Node3)) { // check node3
 +
  // other stuff here }
 +
 +
If (Player.CollidesWith(&Node4)) { // check node4
 +
  // other stuff here }
 +
</source>
 +
 +
 +
What this does is check what nodes we are in, each will return true if we are in that node and execute ‘other stuff’ (I’ll get to what the other stuff is in just a moment) otherwise it will check the next node. Now we will get into that other stuff. Now that we know which node(s) the player is in, we know what we don’t have to check. If you don’t get what I mean look at figure 6:
 +
 +
[[Image:Flexfig6.png]]
 +
 +
First our program will check Node1, because Player isn’t colliding with or ‘in’ Node1, this will return false and it will move to Node2, Node’s 2 and 3 will return false, but Node4 will return true and execute other stuff, this other stuff is the collision checks that are within that node so let’s replace other stuff with the other collision checks for the objects that are in Node4:
 +
 +
<source lang="cpp">If (Player.CollidesWith(&Node1)) {  // check node1
 +
        if ( Player.CollidesWith(&object1))
 +
            Player.x = oldX;
 +
            Player.y = oldY;
 +
 +
.
 +
.
 +
Etc
 +
        if ( Player.CollidesWith(&objectN))
 +
            Player.x = oldX;
 +
            Player.y = oldY;
 +
}</source>
 +
 +
See now, we have cut 100 checks (25 objects in each node was the example), down to just 29, 4 for the nodes and 25 for the objects in the node, that’s 71 less checks and exactly the same result as 100 checks! Now consider the following figure (7)
 +
 +
[[Image:Flexfig7.png]]
 +
 +
First it will check Node1, this will return true as Player is inside Node1, it will then proceed to check all of the objects in Node1 against player, once this has finished, it will check Node2, which again will return true and check collision for the objects in Node2. Then it will check Nodes 3 and 4 which will both return false. This is now 54 checks rather than 100.
 +
 +
This hasn’t been the best way of implementing a Node based collision system, it would have been better to create a node class then instance it for easier use, although I don’t have the time. This system is similar to one called a quad tree, although it may be over kill to implement such a sophisticated system in what will mostly be 2D programs, the basics are that it will split your plane into 4 equal sized nodes just like figure 4, then check collision against those nodes, this is where it gets a little more complicated. Once it knows which nodes you are in, it will then split that node into another 4 nodes like in figure 8:
 +
 +
[[Image:Flexfig8.png]]
    
== EXAMPLE PROGRAMS  ==
 
== EXAMPLE PROGRAMS  ==
48

edits