Line 1,323:
Line 1,323:
[[Image:Flexfig8.png]]
[[Image:Flexfig8.png]]
+
+
It will subdivide further to a specified level, but first it will check again which ‘leaf’ nodes you are in (leaf nodes are simply the nodes that are farthest down the tree). If all this talk of tree’s and leafs is confusing you, you best learn it as these are the proper terms, the parent is a node that has n number of leaf nodes, once a leaf node gets another node attached to it further along it becomes a parent node and the new node is the leaf. Back to our program now, after another check this happens:
+
+
[[Image:Flexfig9.png]]
+
+
Another split would look like this:
+
+
[[Image:Flexfig10.png]]
+
+
It will only subdivide a certain number of times as specified by the programmer, as you can see, it gets rather messy, but once the quadtree has reached the leaf nodes, it will check collision against the objects in each of the leave nodes. The more levels you add to your quad tree the more precise the checks will be but as I said, it gets to a point where you will lose performance rather than gain any. This method wouldn’t be at all difficult using Libwiisprite, just create a Node class and a QuadTree class, if you wish to know more, there are many resources related to computer science and large terrain rendering that feature quad trees as they are widely used for collision detection in large terrains and also sometimes for optimizing graphics rendering engines.
+
+
Something to remember with CollidesWith() is that it will only accept the address of the following objects according to the documentation:
+
+
- Rectangle
+
- Sprite
+
- TiledLayer
+
+
Before I finish up this section on collision detection, I’ll go over a couple more functions from Libwiisprite related to collision detection. The first is DefineCollisionRectangle().
+
+
<source lang="cpp">void wsp::Sprite::DefineCollisionRectangle ( f32 x,
+
f32 y,
+
f32 width,
+
f32 height
+
) </source>
+
+
Defines a collision rectangle. On startup it's the same as Image width and height.
+
+
<source lang="cpp">Parameters:
+
x Offset from the upper left corners position of the sprite.
+
y Offset from the upper left corners position of the sprite.
+
width The width of the collision rectangle.
+
height The height of the collision rectangle.</source>
+
+
It looks long and complicated but really it’s just the same as setting up a Rectangle object. A Collision Rectangle is an area in our plane that we can check against for collision that has 4 sides, exactly the same as our Nodes, so let’s say we wanted to create Nod1 from this, we would do it as such:
+
+
It looks long and complicated but really it’s just the same as setting up a Rectangle object. A Collision Rectangle is an area in our plane that we can check against for collision that has 4 sides, exactly the same as our Nodes, so let’s say we wanted to create Nod1 from this, we would do it as such:
+
+
<source lang="cpp">Node1.DefineCollisionRectangle(0,0,400,300);</source>
+
+
X position = 0, y position = 0 (top left of the screen is (0,0,)), width is 400 pixels and height, 300. Simple enough, now we have a node set up. Instead of using the CollidesWith() function as we have previously been using, we will use a new function in Libwiisprite called GetCollisionRectangle()
+
+
<source lang="cpp">const Rectangle* wsp::Sprite::GetCollisionRectangle
+
( ) const
+
Gets the current collision rectangle.
+
Returns: A pointer to the rectangle.</source>
+
+
Rather than returning true or false, it will return a pointer the current collision rectangle, which we know as being a node. From here, we then check the player against the objects in that node just like before but use pointers for the nodes instead of the object itself. Some additional coding may be required for objects that are colliding with more than one node.
+
+
That’s about as much patience as I have for collision detection, till next time...
+
+
+
== Tiled Layers ==
== EXAMPLE PROGRAMS ==
== EXAMPLE PROGRAMS ==