Line 1,165:
Line 1,165:
- [[user:WiiPhlex|WiiPhlex]]
- [[user:WiiPhlex|WiiPhlex]]
−
== Collision Detection ==
−
Description: Shows how to test for collision between various objects, does not contain an example for node based collision detection (yet).
−
−
<source lang="cpp">#include <stdio.h>
−
#include <stdlib.h>
−
#include <gccore.h>
−
#include <wiiuse/wpad.h>
−
#include <fat.h>
−
−
#include <wiisprite.h>
−
using namespace wsp;
−
−
GameWindow gwd; // Initializes and renders our scene.
−
−
Sprite sprite; // The drawable object we can modify.
−
Sprite sprite2; // another sprite to check collision against.
−
−
Image image; // Holds our image/texture from the SD Card.
−
Image image2; // hold the other image/tecture from SD card.
−
−
Rectangle rect; // rectangle object as an example.
−
Quad quad; //quad object for rect.
−
−
struct player {
−
−
u32 xpos, ypos
−
−
} Player;
−
−
void checkCollision(u32 oldX, u32 oldY);
−
−
int main(int argc, char **argv) {
−
−
fatInitDefault();
−
−
gwd.InitVideo();
−
−
LayerManager manager(3);
−
−
if(image.LoadImage("libwiisprite.png") != IMG_LOAD_ERROR_NONE)exit(0);
−
if(image.LoadImage("libwiisprite2.png") != IMG_LOAD_ERROR_NONE)exit(0);
−
−
sprite.SetImage(&image);
−
sprite.SetPosition(Player.xpos, Player.ypos);
−
−
sprite2.SetImage(&image2);
−
sprite2.SetPosition(200,200);
−
−
manager.Append(&sprite);
−
manager.Append(&Rectangle);
−
−
rect.width = 20; rect.height = 20;
−
rect.x = 50; rect.y = 50;
−
−
quad.SetRectangle(&rect);
−
−
WPAD_Init();
−
WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);
−
−
while(1) {
−
u32 oldX = Player.xpos, oldY = Player.ypos;
−
−
WPAD_ScanPads();
−
u32 pressed = WPAD_ButtonsHeld(WPAD_CHAN_0);
−
−
if(pressed & WPAD_BUTTON_UP)
−
Player.ypos -= 1;
−
−
if(pressed & WPAD_BUTTON_DOWN)
−
Player.ypos += 1;
−
−
if(pressed & WPAD_BUTTON_LEFT)
−
Player.xpos -= 1;
−
−
if(pressed & WPAD_BUTTON_RIGHT)
−
Player.xpos += 1;
−
−
sprite.SetPosition(Player.xpos, Player.ypos);
−
−
checkCollision();
−
−
sprite.SetPosition(Player.xpos, Player.ypos);
−
−
manager.Draw(0, 0);
−
gwd.Flush();
−
}
−
−
manager.Draw(0, 0);
−
gwd.Flush();
−
−
return 0;
−
}
−
−
void checkCollision(u32 oldX, u32 oldY)
−
{
−
if (sprite.CollidesWith(&sprite2)) {
−
Player.x = oldX;
−
Player.y = oldY;
−
}
−
−
if (sprite.CollidesWith(&quad)) {
−
Player.x = oldX;
−
Player.y = oldY;
−
}
−
−
−
}</source>
== EXAMPLE PROGRAMS ==
== EXAMPLE PROGRAMS ==