sphere box collision
Posted: Fri Feb 05, 2010 11:28 pm
hey,
i'm creating a 3D ping pong game and i'm stuck on the collision part..how would i check if the sphere has hit the box.
my code:
i'm creating a 3D ping pong game and i'm stuck on the collision part..how would i check if the sphere has hit the box.
my code:
Code: Select all
#include <windows.h>
#include <iostream>
#include <irrlicht.h>
#pragma comment(lib,"Irrlicht.lib")
using namespace std;
using namespace irr;
using namespace gui;
using namespace core;
using namespace scene;
using namespace video;
class MyEventReceiver: public IEventReceiver {
public:
// This is the one method that we have to implement
virtual bool OnEvent(const SEvent& event) {
// Remember whether each key is down or up
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
// This is used to check whether a key is being held down
virtual bool IsKeyDown(EKEY_CODE keyCode) const {
return KeyIsDown[keyCode];
}
MyEventReceiver() {
for (u32 i = 0; i < KEY_KEY_CODES_COUNT; ++i)
KeyIsDown[i] = false;
}
private:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
int main()
{
//bool collision;
MyEventReceiver receiver;
IrrlichtDevice *device = createDevice(EDT_DIRECT3D9,dimension2d<u32>(800,648),32,false,false,false,&receiver);
IVideoDriver *driver = device->getVideoDriver();
ISceneManager *smgr = device->getSceneManager();
IGUIEnvironment *guienv = device->getGUIEnvironment();
device->setWindowCaption(L"3D Ping Pong");
device->getCursorControl()->setVisible(false);
ISceneNode *maincamera = smgr->addCameraSceneNode();
maincamera->setPosition(vector3df(0,0,0));
ISceneNode *ball = smgr->addSphereSceneNode();
ball->setScale(vector3df(0.3,0.3,0.3));
ball->setPosition(vector3df(0,0,45));
ISceneNode *paddle1 = smgr->addCubeSceneNode();
paddle1->setScale(vector3df(0.2,0.9,0.3));
paddle1->setPosition(vector3df(-35,0,45));
ISceneNode *paddle2 = smgr->addCubeSceneNode();
paddle2->setScale(vector3df(0.2,0.9,0.3));
paddle2->setPosition(vector3df(35,0,45));
vector3df paddle1pos;
vector3df paddle2pos;
vector3df ballpos;
//paddle1->setRotation(vector3df(0,-45,0));
while(device->run())
{
maincamera->setRotation(vector3df(0,0,0));
if(device->isWindowActive())
{
driver->beginScene(true,true,SColor(255,255,255,255));
smgr->drawAll();
driver->endScene();
if(receiver.IsKeyDown(KEY_UP))
{
paddle1pos = paddle1->getPosition();
paddle1pos.Y += 0.006;
paddle1->setPosition(paddle1pos);
}else if(receiver.IsKeyDown(KEY_DOWN))
{
paddle1pos = paddle1->getPosition();
paddle1pos.Y -= 0.006;
paddle1->setPosition(paddle1pos);
}else if(receiver.IsKeyDown(KEY_LEFT))
{
ballpos = ball->getPosition();
ballpos.X -= 0.006;
ball->setPosition(ballpos);
}else if(receiver.IsKeyDown(KEY_RIGHT))
{
ballpos = ball->getPosition();
ballpos.X += 0.006;
ball->setPosition(ballpos);
}
}
}
device->drop();
return 0;
}