First off thanks for looking at this I have looked all over (both here and on the net) and tried several things trying to get this to work.
I am using Visual Studio 2012 with DirectX (June 2010) SDK. I am attempting to write a fuctuin for sprite colission detection. Basicly to stop the player sprite from walking through walls. Below is the code sections that I think are relevent. If you need more please let me know.
// Sprite structure
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;
float prevx, prevy; // added for previous x, y position
D3DCOLOR color;
SPRITE()
{
frame = 0;
columns = 1;
width = height = 0;
scaling = 1.0f;
rotation = 0.0f;
startframe = endframe = 0;
direction = 1;
starttime = delay = 0;
velx = vely = 0.0f;
prevx = prevy = 0;
color = D3DCOLOR_XRGB(255,255,255);
}
};
bool CheckforCol()
{
if (Collision(girl,office_wall0)||Collision(office_wall0,girl))
{
PlaySound(beep);
return true;
}
else
{
return false;
}
}
void MoveSprite(char direction)
{
// update sprite position
switch (direction)
{
case 'l':
girl.prevx = girl.x;
girl.x -= 32;
girl.startframe = girl.frame = 6;
girl.endframe = 7;
if (CheckforCol())
girl.x = girl.prevx;
break;
case 'r':
girl.prevx = girl.x;
girl.x += 32;
girl.startframe = girl.frame = 2;
girl.endframe = 3;
if (CheckforCol())
girl.x = girl.prevx;
break;
case 'u':
girl.prevy = girl.y;
girl.y -= 32;
girl.startframe = girl.frame = 4;
girl.endframe = 5;
if (CheckforCol())
girl.y = girl.prevy;
break;
case 'd':
girl.prevy = girl.y;
girl.y += 32;
girl.startframe = girl.frame = 0;
girl.endframe = 1;
if (CheckforCol())
girl.y = girl.prevy;
break;
}
if (girl.x < TILEW*10 || girl.y < TILEW*5
|| girl.x > TILEW*(10+mapW-1) || girl.y > TILEH*(5+mapH-1) || CheckforCol())
{
PlaySound(beep);
switch (direction)
{
case 'l':
girl.x += 32;
break;
case 'r':
girl.x -= 32;
break;
case 'u':
girl.y += 32;
break;
case 'd':
girl.y -= 32;
break;
}
}
}
// Bounding box collision detection
int Collision(SPRITE sprite1, SPRITE sprite2)
{
RECT rect1;
rect1.left = sprite1.x;
rect1.top = sprite1.y;
rect1.right = sprite1.x + sprite1.width * sprite1.scaling;
rect1.bottom = sprite1.y + sprite1.height * sprite1.scaling;
RECT rect2;
rect2.left = sprite2.x;
rect2.top = sprite2.y;
rect2.right = sprite2.x + sprite2.width * sprite2.scaling;
rect2.bottom = sprite2.y + sprite2.height * sprite2.scaling;
RECT dest; // ignored
return IntersectRect(&dest, &rect1, &rect2);
}
// store version 1
void store1()
{
while (storeLevel == 1)
{
//update input devices
DirectInput_Update();
//clear the scene
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,100), 1.0f, 0);
if (GetTickCount() - start >= 60)
{
// reset timing
start = GetTickCount();
if (KEY_DOWN(VK_DOWN)) // down
{
MoveSprite('d');
}
if (KEY_DOWN(VK_UP)) // up
{
MoveSprite('u');
}
if (KEY_DOWN(VK_LEFT)) // left
{
MoveSprite('l');
}
if (KEY_DOWN(VK_RIGHT)) // right
{
MoveSprite('r');
}
if (d3ddev->BeginScene())
{
// DrawSurface(backbuffer, 0, 0, intro);
// ScrollScreen(TILEW*12, TILEH*14);
DrawSurface(backbuffer, TILEW*10, TILEH*5, gameMap);
spriteobj->Begin(D3DXSPRITE_ALPHABLEND);
Sprite_Animate(girl.frame, girl.startframe, girl.endframe, 1, girl.starttime, 150);
Sprite_Draw_Frame(imgGirl, girl.x, girl.y, girl.frame, 32, 32, 2);
Sprite_Draw_Frame(imgBlank, office_wall0.x, office_wall0.y, 0, office_wall0.width, office_wall0.height, 1);
spriteobj->End();
//stop rendering
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
}
//Escape key ends program
//exit when escape key is pressed
if (KEY_DOWN(VK_ESCAPE))
{
storeLevel = 0;
gameover = true;
}
}
}
Sorry thats a lot of code but I am pretty sure my problem is somewhere in these sections. The sprites draw fine but when ever the player sprite (girl)moves from any direction over near the wall sprite (office_wall0) it does not respond. Can someone help me figure this out?
Thanks,
Brandon