Colission with bullets

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Culando
Posts: 12
Joined: Mon Jan 29, 2007 3:20 am

Colission with bullets

Post by Culando »

Sorry if this belongs in the beginners forum.

I have a code to make a bullet animate across the screen whenever a key is pressed, but I can't seem to get it to collide with my enemy properly. Here's the code snippet.

Code: Select all

		if(borka_coll.intersectsWithBox(zap_coll)){
			borka->setMaterialTexture( 0, driver->getTexture("../../media/boxy.tga") );
		}else{
			borka->setMaterialTexture( 0, driver->getTexture("../../media/boxyred.tga") );
		}
		if ( receiver.Keys[ KEY_KEY_W ] && !bKeyWasPressed){
			IAnimatedMesh* mesh = smgr->getMesh("../../media/bullet1.x");
			IAnimatedMeshSceneNode* zap = smgr->addAnimatedMeshSceneNode( mesh );

			zap->setMaterialFlag(video::EMF_LIGHTING, false);
			zap->setMaterialTexture( 0, driver->getTexture("../../media/ProjectileBlue.tga") );
			zap->setRotation(core::vector3df(0,270,0));
			zap->setScale(core::vector3df(.5,.5,.5));

			scene::ISceneNodeAnimator* anim2 = 0;
			scene::ISceneNodeAnimator* dele2 = 0;

			// set flight line

			anim2 = smgr->createFlyStraightAnimator(core::vector3df(boxy_x,boxy_y,boxy_z),core::vector3df(boxy_x - 40,boxy_y,boxy_z), 3000,false);
			dele2 = smgr->createDeleteAnimator(30000);
			zap->addAnimator(anim2);
			zap->addAnimator(dele2);

			anim2->drop();
		}
Borka is the enemy node, zap is the bullet node.

Currently everything works but the actual collision detection.

Edit: I actually just thought of a workaround that may work, but I'd still like to know if a more direct solution exists.
Robert Y.
Posts: 212
Joined: Sun Jan 28, 2007 11:23 pm

Post by Robert Y. »

Where is your TriangleSelector or MetaSelector code? I don't see any actual collision detection code in your example...
The createFlyStraightAnimator() you use just lets it fly a straight line, without collision detection. Either you calculate how far it should fly before, or you should use collision detection.

In the examples there is some nice code to show how you can do this.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Ummm, I think this is his collision detection code...

Code: Select all

if(borka_coll.intersectsWithBox(zap_coll)){ 
         borka->setMaterialTexture( 0, driver->getTexture("../../media/boxy.tga") ); 
      }else{ 
         borka->setMaterialTexture( 0, driver->getTexture("../../media/boxyred.tga") ); 
      } 
I don't know what boxy_x, boxy_y and boxy_z are initialized to, but I think your animator should fly from bullet start position, wherever that is, to the target. Right now it flies to some position that is 40 units to the side of the starting position. If that is not where the enemy is, then it isn't going to work.

The other issue is the bullet can fly right through the collision box without being detected. Bullets are fast, and bounding boxes are pretty small. If the bullet is outside the bounding box at time N, it might be all the way to the other side of the bounding box at time N+1. The collision test might not work because the bullet won't always be inside the bounding box at the time your test is ran. It may be safer to use a ray-box intersection test when the bullet is fired.

Travis
Culando
Posts: 12
Joined: Mon Jan 29, 2007 3:20 am

Post by Culando »

Perhaps I should have been more specific. My fault.

This is going to be a 2.5D scrolling space shooter. The bullets are always fired from the player's position (boxy_x, _y, _z) and go in a straight line. Right now it'll stop before it passes the screen, but that's not finalized.

There is no real "target" other than a point straight across the screen.

Maybe I'll just do a raw coordinate test. >>
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Hybrid already gave you the right answer; you want a line/box intersection, not box/box, or you'll risk having object pass right through each other.

How are you calculating/from where are you obtaining "borka_coll" and "zap_coll"?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Culando
Posts: 12
Joined: Mon Jan 29, 2007 3:20 am

Post by Culando »

I am calculating the bounding boxes with getTransformedBoundingBox(). It works with a collision between the enemy and the player. But perhaps it is as you've said that the bullet (zap) is going through too fast and not being detected. I just have to figure out how to make it with a line/box collision.
olivehehe_03
Posts: 157
Joined: Tue Mar 20, 2007 8:30 am

Post by olivehehe_03 »

Generally bullets aren't done using an actual object with physics, but rather by tracing a line from the player to wherever the player is looking, getting any collisions along that line, and then drawing a sprite (the bullet graphic) and moving it down the line. This way might be a bit easier and more reliable than using bounding boxes
Tell me what you cherish most. Give me the pleasure of taking it away.
Post Reply