Direct3D学习笔记(四)——碰撞检测(Bounding Box)
程序员文章站
2024-03-16 16:40:34
...
一、碰撞检测函数(MyDirectX.cpp)
int Collision(SPRITE sprite1, SPRITE sprite2) {
RECT rect1, rect2;
rect1.left = (long)sprite1.x;
rect1.right = (long)sprite1.y;
rect1.top = (long)sprite1.x + sprite1.width * sprite1.scaling;
rect1.bottom = (long)sprite1.y + sprite1.height * sprite1.scaling;
rect2.left = (long)sprite2.x;
rect2.right = (long)sprite2.y;
rect2.top = (long)sprite2.x + sprite2.width * sprite2.scaling;
rect2.bottom = (long)sprite2.y + sprite2.height * sprite2.scaling;
RECT dest;
return IntersectRect(&dest, &rect1, &rect2);
}
在MyDirectX,h中添加
int Collision(SPRITE sprite1, SPRITE sprite2);
在MyDirectX.h头文件中申明构造(放在文件引用下面)
struct SPRITE {
float x, y;
int frame, columns;
int width, height;
float scaling, rotation;
int startframe, endframe;
int starttime, delay;
int direction;
float velx, vely;
D3DCOLOR color;
SPRITE() {
frame = 0;
columns = 1;
width = height = 0;
scaling = 1.0f;
rotation = 0.0f;
direction = 1;
starttime = delay = 1;
velx = vely = 0.0f;
color = D3DCOLOR_XRGB(255, 255, 255);
}
};
二、MyGame.cpp
#include "MyDirectX.h";
using namespace std;
const string APPTITLE = "Bounding Box Demo";
const int SCREENW = 1024;
const int SCREENH = 768;
LPD3DXSPRITE spriteobj=NULL; //纹理
SPRITE ship, asteroid1, asteroid2;
LPDIRECT3DTEXTURE9 imgShip = NULL;
LPDIRECT3DTEXTURE9 imgAsteroid = NULL;
bool Game_Init(HWND window) {
Direct3D_Init(window, SCREENW, SCREENH, false);
DirectInput_Init(window);
imgShip = LoadTexture("F:/image/ship.png");
if (!imgShip)return false;
imgAsteroid = LoadTexture("F:/image/asteroid.png");
if (!imgAsteroid)return false;
ship.x = 450;
ship.y = 300;
ship.width = ship.height = 128;
asteroid1.x = 50;
asteroid1.y = 200;
asteroid1.width = asteroid1.height = 60;
asteroid1.columns = 8;
asteroid1.startframe = 0;
asteroid1.endframe = 63;
asteroid1.velx = -2.0f;
asteroid2.x = 900;
asteroid2.y = 500;
asteroid2.width = asteroid2.height = 60;
asteroid2.columns = 8;
asteroid2.startframe = 0;
asteroid2.endframe = 63;
asteroid2.velx = 2.0f;
return true;
}
void Game_Run(HWND window) {
if (!d3ddev) return;
DirectInput_Update();
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 100), 1.0f, 0);
if (Key_Down(DIK_UP)) {
ship.y -= 1.0f;
if (ship.y < 0)ship.y = 0;
}
if (Key_Down(DIK_DOWN)) {
ship.y += 1.0f;
if (ship.y > SCREENH - ship.height) {
ship.y = SCREENH - ship.height;
}
}
asteroid1.x += asteroid1.velx;
if (asteroid1.x<0 || asteroid1.x>SCREENW - asteroid1.width) {
asteroid1.velx *= -1;
}
Sprite_Animate(asteroid1.frame, asteroid1.startframe, asteroid1.endframe, asteroid1.direction, asteroid1.starttime, asteroid1.delay);
asteroid2.x += asteroid2.velx;
if (asteroid2.x<0 || asteroid2.x>SCREENW - asteroid2.width) {
asteroid2.velx *= -1;
}
Sprite_Animate(asteroid2.frame, asteroid2.startframe, asteroid2.endframe, asteroid2.direction, asteroid2.starttime, asteroid2.delay);
if (Collision(ship, asteroid1)) {
asteroid1.velx *= -1;
}
if (Collision(ship, asteroid2)) {
asteroid2.velx *= -1;
}
if (d3ddev->BeginScene()) {
spriteobj->Begin(D3DXSPRITE_ALPHABLEND);
Sprite_Draw_Fram(imgShip, ship.x, ship.y, ship.width, ship.height, ship.frame, ship.columns);
Sprite_Draw_Fram(imgAsteroid, asteroid1.x, asteroid1.y, asteroid1.width, asteroid1.height, asteroid1.frame, asteroid1.columns);
Sprite_Draw_Fram(imgAsteroid, asteroid2.x, asteroid2.y, asteroid2.width, asteroid2.height, asteroid2.frame, asteroid2.columns);
spriteobj->End();
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
if (Key_Down(VK_ESCAPE))gameover = true;
if (controllers[0].wButtons & XINPUT_GAMEPAD_BACK) {
gameover = true;
}
}
void Game_End() {
if (imgShip)imgShip->Release();
if (imgAsteroid)imgAsteroid->Release();
DirectInput_Shutdown();
Direct3D_Shutdown();
}