sphere box collision

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
JBGame
Posts: 23
Joined: Thu Feb 04, 2010 10:46 am

sphere box collision

Post by JBGame »

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:

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;
}
Destructavator
Posts: 40
Joined: Sat Feb 06, 2010 9:00 pm
Location: Ohio, USA

Post by Destructavator »

Did you read the tutorials through to number 7, which explains collision? If so, exactly what part are you stuck on?

I don't see any collision manager or collision-related code at all in your posted code - is it in another file or am I missing something here?

Edit: I also noticed that you're trying to rotate the camera, if you want to do this direct with setRotation() I believe you would need to use the "bindTargetAndRotation" function for the camera's node. Otherwise you might have headaches down the road.
JBGame
Posts: 23
Joined: Thu Feb 04, 2010 10:46 am

Post by JBGame »

i'm pretty much stuck at the first bit..

the first param of smgr->addOctreeSceneNode() is an IMesh* and my paddles are ISceneNode*.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

JBGame wrote:my paddles are ISceneNode*.
No, they are IMeshSceneNodes, since addCubeSceneNode() returns an IMeshSceneNode. But you only got a pointer to the ISceneNode part of them.
Try

Code: Select all

IMeshSceneNode *paddle2 = smgr->addCubeSceneNode();
addSphereSceneNode() returns an IMeshSceneNode, too.
"Whoops..."
JBGame
Posts: 23
Joined: Thu Feb 04, 2010 10:46 am

Post by JBGame »

oh i see..still confused lol, how would i add it to smgr->createOctreeTriangleSelector();
CuteAlien
Admin
Posts: 9716
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

You don't need an octree for a ping-pong game. Octrees are an optimization, but that's just overkill here.

Maybe take a look at this thread, which is about pong which tries to do something similar: http://irrlicht.sourceforge.net/phpBB2/ ... highlight=
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
JBGame
Posts: 23
Joined: Thu Feb 04, 2010 10:46 am

Post by JBGame »

hi,

oh ok cool, got some basic collision working :).

i have a question about physics now..will i be able to use newton to add physics with addCubeSceneNode? or will i have to create a newton box or something..:S
still new to all this
CuteAlien
Admin
Posts: 9716
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Hm, search around in the forum for more info about physic wrappers. I think mostly you will set geometry primitives like boxes just once more in the physics engine. Or actually in many cases you only set them in the physics engine as your collision geometry is usually a lot simpler than your level-geometry. The polygon-soup of the world can sometimes be shared between 3D engine and physics engine (not sure about newton+irrlicht right now, but I suppose it should be possible), but once again you sometimes just copy it and give the physic-engine a copy (which is pre-processed with some polygon reduction) ... that kind of stuff really depends on the situation.

For ping-pong... hm... I guess you will give the ball some sphere. And for the table and the paddles you should probably try to go with shared polygons between Irrlicht and the physics engine. Not sure about the net... any static geometry will probably not behave perfect, but for a start it's fine. Another possibility would probably be to simplify the table to a box + another box for the net.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
JBGame
Posts: 23
Joined: Thu Feb 04, 2010 10:46 am

Post by JBGame »

hmm i see..which physic wrapper would be recommended ?
aren't most wrappers outdated and won't work with 1.7? correct me if i'm wrong.
also for irrphysx, don't you need a physx-enabled GPU card?

maybe i should make a new topic hey.. lol
CuteAlien
Admin
Posts: 9716
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Irrlicht 1.7 interfaces have not changed that much. So even if a wrapper is outdated it would probably be easy to fix. And I don't think you need a gpu for physx - it would just be faster. But I don't think speed is of concern in a ping-pong game as there are probably not enough polygons for real trouble.

But I haven't worked with physics in combination with Irrlicht so far myself, so just speaking from theory here. So I can't tell you which engine to use. I do prefer using engines which are opensource, so I would probably use either Bullets or ODE. You might also consider writing your own physics for this. It's an easy case as you don't have to care about any kind of chain-reactions (one ball hitting another etc) and so it would be a good way to learn a little about physics.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
JBGame
Posts: 23
Joined: Thu Feb 04, 2010 10:46 am

Post by JBGame »

doesn't irrlicht have a built in physics engine ? thought i read about it somewhere ;;
making my own basic physics class would be cool, though i don't know how to start, how would you get(or set) node attributes(mass/kg etc..)
CuteAlien
Admin
Posts: 9716
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

No real physics. Irrlicht has a collision system which can be used. And it has a simple physic for moving characters around.

Doing your own physics, well - you would write a class containing all object attributes (size, mass). Then you would write a class containing your physic-state (forces, velocity). Then you would write a class which contains an Irrlicht-SceneNode and your classes for physic state and physic attributes. Next you would write some function (in yet another class usually) which takes the current physic state of all physic objects, changes it according to attributes and physical formulas and maybe set the resulting position for the Irrlicht SceneNode. The formulas you would need are those for Newton mechanics. You would need to look up at least acceleration, gravity and air-resistance. I found out that school-books are often very good sources for that. And you have to think how to handle collisions - Irrlicht can help you find out where they happen, but you have to decide how to react on that (like ball-paddle: add a force-vector to the ball. Ball-table: ... some sort of impulse thing).

Well... certainly you can also just use a physic engine, but you would learn a lot doing that yourself once ;-)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
JBGame
Posts: 23
Joined: Thu Feb 04, 2010 10:46 am

Post by JBGame »

how would you get deltaTime? i'm reading http://gafferongames.com/game-physics/
the author's time() function keeps giving me unresolved external..
CuteAlien
Admin
Posts: 9716
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Delta time is the time between two calls to your physic.
time() is part of the standard c-lib, so not sure how you get an unresolved external for this. But Irrlicht also has an ITimer interface which you can get from the IrrlichtDevice. And the ITimer has a function getTime ().
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
JBGame
Posts: 23
Joined: Thu Feb 04, 2010 10:46 am

Post by JBGame »

Thanks, it's getting all clearer to me now, i'll try to work on my own now :).
Post Reply