Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
bounding box collisions
Options
-
26-04-2008 5:29pmI get the following 5 errors with my code when trying to build in VB.
Error 1 error C2065: 'g_boundingBox' : undeclared identifier c:\Documents and Settings\user\Desktop\lab 7\Template\Box.cpp 355
Error 2 error C2228: left of '.min' must have class/struct/union c:\Documents and Settings\user\Desktop\lab 7\Template\Box.cpp 398
Error 3 error C2228: left of '.max' must have class/struct/union c:\Documents and Settings\user\Desktop\lab 7\Template\Box.cpp 399
Error 4 error C2228: left of '.min' must have class/struct/union c:\Documents and Settings\user\Desktop\lab 7\Template\Box.cpp 400
Error 5 error C2228: left of '.max' must have class/struct/union c:\Documents and Settings\user\Desktop\lab 7\Template\Box.cpp 401
problem is I'm not sure what to declare the g_boundingBox as tried a few guesses of float, void but to no avail. The rest of the errors i am unsure of.
code below#include <d3d9.h>
#include <d3dx9.h>
#include <stdio.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#define WINDOW_CLASS "COM428M1 LAB1"
#define WINDOW_NAME "Direct 3D Blank display"
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
// Global variables
LPDIRECT3D9 g_D3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_D3DDevice = NULL; // Our rendering device
struct stBoundingBox
{
stBoundingBox () {min[0] = min[1] = min[2] =
max[0] = max[1] = max[2] = 0;}
float min[3];
float max[3];
};//End stBoundingBox
//Amounts to move the objects
float g_obj1XPos = -1;
float g_obj1MoveAmt = 0;
float g_obj1Dir = -0.02f;
float g_obj2XPos = 1;
float g_obj2MoveAmt = 0;
float g_obj2Dir = 0.02f;
//Test if collision occurred, false if not
bool g_collision = false;
//DirectX font object
LPD3DXFONT g_Font = NULL;
//Create a RECT to position the text onscreen
RECT g_fontPos = {0, 125, 480, 640};
char g_str[64] = {0};
//Vertexbuffer to hold the geometry
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;
//A structure for our custom vertex type
struct stD3DVertex
{
float x,y,z;
unsigned long colour;
};//End stD3DVertex
//Our custom FVF describing the vertex structure
#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
// Matrices.
D3DXMATRIX g_Projection;
D3DXMATRIX g_ViewMatrix;
D3DXMATRIX g_WorldMatrix;
//Forward function declarations
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void Render();
void CleanUp();
bool InitialiseD3D(HWND hWnd, bool fullscreen);
bool InitialiseObjects();
void CreateCamera();
void CalculateBoundingBox(stD3DVertex *vList, int totalVerts, stBoundingBox *bb);
bool OnCollision();
bool CreateGeometry();
void SetLighting();
void UpdateObjects();
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show)
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0, 0,
hInst, NULL, NULL, NULL, NULL,
WINDOW_CLASS, NULL };
RegisterClassEx(&wc);
// Create the application's window
HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
100, 100, WINDOW_WIDTH, WINDOW_HEIGHT, GetDesktopWindow(), NULL,
wc.hInstance, NULL);
if(InitialiseD3D(hWnd, false))
{
// Show the window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
// Enter the message loop
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}//End the if
else
Render();
}//End the while
}//End the if
// Release any and all resources.
CleanUp();
// Unregister our window.
UnregisterClass(WINDOW_CLASS, wc.hInstance);
return 0;
}//End WinMain function
//The MsgProc function handles incoming messages from the OS
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_KEYUP:
if(wParam == VK_ESCAPE) PostQuitMessage(0);
break;
}//End the switch
return DefWindowProc(hWnd, msg, wParam, lParam);
}//End MsgProc function
bool InitialiseD3D(HWND hWnd, bool fullscreen)
{
D3DDISPLAYMODE displayMode;
// Create the D3D object.
g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_D3D == NULL) return false;
// Get the desktop display mode.
if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
&displayMode))) return false;
// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
if(fullscreen)
{
d3dpp.Windowed = FALSE;
d3dpp.BackBufferWidth = WINDOW_WIDTH;
d3dpp.BackBufferHeight = WINDOW_HEIGHT;
}
else
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = displayMode.Format;
// //Create the D3DDevice
if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &g_D3DDevice)))
{
//We have failed to create a hardware supported device, so attempt software rendering
if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_D3DDevice)))
{
//DirectX must not be installed
MessageBox(NULL, "CreateDevice() failed! Make sure you have DirectX 9 installed.", "Error!", MB_OK);
return false;
}
}//End if
// Create any objects we will be displaying.
if(!InitialiseObjects()) return false;
return true;
}//End InitialiseD3D
void Render()
{
// Clear the backbuffer.
g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
// Begin the scene. Start rendering.
g_D3DDevice->BeginScene();
// Display if collision occurred.
g_Font->DrawText(NULL, g_str, -1, &g_fontPos, DT_CENTER, D3DCOLOR_XRGB(255,255,255));
// Apply the view (camera).
g_D3DDevice->SetTransform(D3DTS_VIEW, &g_ViewMatrix);
// Set stream.
g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0, sizeof(stD3DVertex));
// Draw square 1.
D3DXMatrixTranslation(&g_WorldMatrix, g_obj1XPos + g_obj1MoveAmt, 0, 0);
g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix);
g_D3DDevice->SetFVF(D3DFVF_VERTEX);
g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
// Draw square 2.
D3DXMatrixTranslation(&g_WorldMatrix, g_obj2XPos + g_obj2MoveAmt, 0, 0);
g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix);
g_D3DDevice->SetFVF(D3DFVF_VERTEX);
g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
// End the scene. Stop rendering.
g_D3DDevice->EndScene();
// Display the scene.
g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}//End the render function
//CleanUp - Releases any resources we used
void CleanUp()
{
if( g_D3DDevice != NULL)
g_D3DDevice->Release();
if( g_D3D != NULL)
g_D3D->Release();
if( g_VertexBuffer != NULL)
g_VertexBuffer->Release();
if( g_Font != NULL)
g_Font->Release();
g_VertexBuffer = NULL;
g_Font = NULL;
g_D3DDevice = NULL;
g_D3D = NULL;
}//End Cleanup function
bool InitialiseObjects()
{
CreateGeometry();
SetLighting();
CreateCamera();
return true;
}
void CreateCamera()
{
// Set the projection matrix.
D3DXMatrixPerspectiveFovLH(&g_Projection, 45.0f, WINDOW_WIDTH/WINDOW_HEIGHT, 0.1f, 1000.0f);
g_D3DDevice->SetTransform(D3DTS_PROJECTION, &g_Projection);
// Define camera information.
D3DXVECTOR3 cameraPos(0.0f, 0.0f, -5.0f);
D3DXVECTOR3 lookAtPos(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 upDir(0.0f, 1.0f, 0.0f);
// Build view matrix.
D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos, &lookAtPos, &upDir);
}//End CreateCamera
void CalculateBoundingBox(stD3DVertex *vList, int totalVerts, stBoundingBox *bb)
{
if(!vList || totalVerts <=0 || !bb)
return;
for (int i = 0; i < totalVerts; i++)
{
//Check to see if the x,y and z values of the current vert are
// smaller than the current min or greater than the current max values
if(vList.x < bb->min[0]) bb->min[0] = vList.x;
if(vList.x > bb->max[0]) bb->min[0] = vList.x;
if(vList.y < bb->min[1]) bb->min[1] = vList.y;
if(vList.y > bb->max[1]) bb->min[1] = vList.y;
if(vList.z < bb->min[2]) bb->min[2] = vList.z;
if(vList.z > bb->max[2]) bb->min[2] = vList.z;
}//End for
}//End CalculateBoundingBox
bool OnCollision(stBoundingBox bb, float x, float y, float z)
{
if(bb.max[0] <= x) return false;
if(bb.min[0] >= x) return false;
if(bb.max[0] <= y) return false;
if(bb.min[0] >= y) return false;
if(bb.max[0] <= z) return false;
if(bb.min[0] >= z) return false;
//We have a collision!
return true;
}//End OnCollision
bool OnCollision(stBoundingBox &bb1, stBoundingBox &bb2)
{
//Test collision between two bounding boxes
if((bb1.min[0] > bb2.max[0]) || (bb2.min[0] > bb1.max[0])) return false;
if((bb1.min[1] > bb2.max[1]) || (bb2.min[1] > bb1.max[1])) return false;
if((bb1.min[2] > bb2.max[2]) || (bb2.min[2] > bb1.max[2])) return false;
//We have a collision!
return true;
}//End OnCollision
bool CreateGeometry()
{
// Create the font.
if(FAILED(D3DXCreateFont(g_D3DDevice, 18, 0, 0, 1, 0,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, "Arial", &g_Font)))
return false;
// Fill in our structure to draw an object.
// x, y, z, colour, texture coords.
stD3DVertex objData[] =
{
{-0.3f, -0.4f, 0, D3DCOLOR_XRGB(255,255,255)},
{0.3f, -0.4f, 0, D3DCOLOR_XRGB(255,255,255)},
{0.3f, 0.4f, 0, D3DCOLOR_XRGB(255,255,255)},
{0.3f, 0.4f, 0, D3DCOLOR_XRGB(255,255,255)},
{-0.3f, 0.4f, 0, D3DCOLOR_XRGB(255,255,255)},
{-0.3f, -0.4f, 0, D3DCOLOR_XRGB(255,255,255)}
};//End stD3DVertex objData[] =
CalculateBoundingBox(objData, 6, &g_boundingBox);
// Create the vertex buffer.
if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
D3DFVF_VERTEX, D3DPOOL_DEFAULT,
&g_VertexBuffer, NULL))) return false;
// Fill the vertex buffer.
void *ptr;
if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
(void**)&ptr, 0))) return false;
memcpy(ptr, objData, sizeof(objData));
g_VertexBuffer->Unlock();
}//End CreateGeometry
void SetLighting()
{
// Set default rendering states.
g_D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
}//End SetLighting
void UpdateObjects()
{
// Move objects. If position limit is hit, switch directions.
g_obj1MoveAmt += g_obj1Dir;
if(g_obj1MoveAmt > 2) g_obj1Dir *= -1;
if(g_obj1MoveAmt < -2) g_obj1Dir *= -1;
g_obj2MoveAmt += g_obj2Dir;
if(g_obj2MoveAmt > 2) g_obj2Dir *= -1;
if(g_obj2MoveAmt < -2) g_obj2Dir *= -1;
// Get bounding box for each object.
stBoundingBox newBB1, newBB2;
memcpy(&newBB1, &g_boundingBox, sizeof(stBoundingBox));
memcpy(&newBB2, &g_boundingBox, sizeof(stBoundingBox));
// Move boxes for each object along since they have moved.
newBB1.min[0] = g_boundingBox.min[0] + (g_obj1XPos + g_obj1MoveAmt);
newBB1.max[0] = g_boundingBox.max[0] + (g_obj1XPos + g_obj1MoveAmt);
newBB2.min[0] = g_boundingBox.min[0] + (g_obj2XPos + g_obj2MoveAmt);
newBB2.max[0] = g_boundingBox.max[0] + (g_obj2XPos + g_obj2MoveAmt);
// Test for collision and record results.
g_collision = OnCollision(newBB1, newBB2);
// Create string.
if(g_collision) sprintf(g_str, "Collision: TRUE");
else sprintf(g_str, "Collision: FALSE");
}//End UpdateObjects
if you made it this far thanks0
Comments
-
I dont think youre initializing g_boundingBox anywhere at all. All I see are the min and max calls to it, and it getting passed as a reference: but nowhere is it declared initialized or constructed.0
-
g_boundingBox is (most probably) an instance of the struct stBoundingBox, which you havn't initialised anywhere.
As you said yourself you tried to create g_boundingBox but it wouldn't work, may i suggest thisstBoundingBox g_boundingBox;
which should initalise all the values in g_boundingBox to Zero
And then when you do thismemcpy(&newBB1, &g_boundingBox, sizeof(stBoundingBox));
It'll essentially just set all the values in newBB1 to Zero.
Disclaimer: I'm too bone idle to check this out myself so i could be wrong. Hideously wrong0
Advertisement