Small enemy collision problem[Solved]

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.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

night_hawk wrote:Yeah, copy-pasting is good, but doesn't work. Copy-paste-edit! Now that's what works.
Decide-requirements-create-unit-tests-based-on-requirements-identify-code-that-promises-to-fulfill-those-requirements-copy-paste-edit-compare-test-results-against-expected-results-and-requirements.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

screwgh wrote:So the bottom line is that I should think more about the aspects of colission and try to figure a own colission detection and a shoot meganism?

a little offtopic
I always thought that good copy pasting of code was a programmers way of living why try to invent something that already excist. After all my excpierence is that with copying code you still have to get into code and background of everything but it gets you motivated to get started. And after all you spend the same ammount of time, it is not lazyness just a motivation isue.

But now I am already motivated to make it work so I while get deeper in the colission detection and shoot meganism. Hopefully something good will rollout.

Thanks for the reply.
Yes, you are and that's pretty good in itself to keep a positive spin on it.

For now, just keep walking the path you're at the moment, untilllll....

Until....

Until....

Until....

Beep

<power out>

You get the idea, you'll eventually figure it out the FlyStraightAnimator doesn't have the attributes you need to implement the stuff you like.
Image
robertgarand
Posts: 69
Joined: Fri Feb 22, 2008 6:47 pm
Location: montreal
Contact:

That is why I sended you my snippet ;)

Post by robertgarand »

Iknow it isn't state of the art programming, but the principle is there,
adapted to Irr engine. (I get 100+ fps on a laptop with x1000s vertex)

I agree with you, it is not bad practice to copy/paste,
if you understand the coding, or it always get back hard to you,
application crash without a clue about ...
And to Acki:
you're right about the engine doing what it is suppose to do, the coding behind it relies on us
to Rogerborg:
I love your link collision detection strategies.


PS:You two are very good references on this forum, plus you're funny :P

regards,
Robert
If it can't be writen, it can't exist
screwgh
Posts: 19
Joined: Tue Oct 14, 2008 12:27 am

New progress

Post by screwgh »

Hi,

After rethinking the shoot and started from scratch. I got finaly something working still with the flyStraightAnimator because this works good with straight bullet flying. I changed the billboard with a sphere. And changed some things. He hits the native enemy and then the bullet disapears it is not all good coding but it works. Hopefully it wil work with enemy`s that are moving.

The only thing I need to do is wall colission but that worked before so I gonna take that code from the demo.

After all it took me a while to get this far and if I have something good working I will post the code. For now I gonna look if it works with enemys who are moving around.

Thanks for all the reply`s I appreciate it very much. It got me thinking about :).


Edit----
Yes it works with moving enemys this is good news and a big step forward. Just the wall collision and I have a small FPS game :D. Besides AI offcourse :D
screwgh
Posts: 19
Joined: Tue Oct 14, 2008 12:27 am

The solution

Post by screwgh »

Hi everybody,

I am still working to get collision working like I want but like I said it does pretty much like what I want so here I give the solution what I thought off. Hopefully I help someone with the code.

Collision.h

Code: Select all

//Including the files


//defining the namespaces to acces easier


//Including the dll file


//Defining variables


//Defining functions

bool collision(ISceneNode* one, ISceneNode* two, int size) 
{ 
	if(two->getPosition().getDistanceFrom(one->getPosition()) < size) 
	{
		return true; 
	}
		return false; 
} 


bool collisionA(ISceneNode* one, vector3df two, int size) 
{ 
	if(two.getDistanceFrom(one->getPosition()) < size) 
	{
		return true; 
	}
		return false; 
} 


bool collisionB(ISceneNode* one, ISceneNode* two) 
{ 
	if(one->getTransformedBoundingBox().intersectsWithBox(two->getTransformedBoundingBox()) )
	{
		return true; 

	}
   return false; 
} 


//Defining classes
shoot2.h //

Code: Select all

//Including the files


//defining the namespaces to acces easier


//Including the dll file


//Defining variables


//Defining functions
void shoot2()
{
	ICameraSceneNode* camera = smgr->getActiveCamera();

	vector3df start = camera->getPosition(); 
	vector3df end = (camera->getTarget() - start);
	end.normalize();
	start += end*8.0f;
	end = start + (end * camera->getFarValue());

	f32 length = (f32)(end - start).getLength();
	const f32 speed = 0.8f;
	time = (u32)(length / speed);



	if(!bullet)
	{
		bullet = smgr->addSphereSceneNode(3.0f, 16, 0, -1, start, vector3df(0,0,0), vector3df(1.0f,1.0f,1.0f));
		bullet->setMaterialFlag(EMF_LIGHTING, false);

		anim = smgr->createFlyStraightAnimator(start, end, time); 
		bullet->addAnimator(anim);
		anim->drop();	

		anim = smgr->createDeleteAnimator(time);
		bullet->addAnimator(anim);
		anim->drop();
		
	}

	if(hit==true)
		{
			anim = smgr->createDeleteAnimator(1);
			bullet->addAnimator(anim);
			anim->drop();
			score++;
			hit=false;
			printf("Score: %d \n", score);
		}
}

//Defining classes
A bit of the main.cpp code from the while loop

Code: Select all

	while(device->run())
	{

		if( keys[KEY_KEY_X])
		{
			shoot2();
		}
		if( keys[KEY_KEY_P])
		{
			printf("PosX: %4.2f \n" , camera->getPosition().X);
			printf("PosY: %4.2f \n" , camera->getPosition().Y);
			printf("PosZ: %4.2f \n" , camera->getPosition().Z);
		}

		driver->beginScene(true, true, SColor(255,100,101,140));	//Must draw between begin and end. Begin drawing
		//////////////////////////////////////////////////////////////Begin drawing
		
		if(enemy)
		{
			enemy->grab();
			//moving enemy just for testing colission
			//vector3df v = enemy->getPosition();
			//v.Z +=1.0;
			//enemy->setPosition(v);
			if(collisionA(enemy, wayB, 20))
				printf("Ben er");

		}
			
		if(bullet && enemy)
		{
			enemy->grab();
			bullet->grab();
			if(collisionB(bullet, enemy))
			{
				printf("Hit\n");
				hit=true;
				shoot2();
				state=6;
				
			}
			if(collisionB(bullet, scene))
			{
				//printf("Hit wall");
			}
		}

		//if(collision(enemy, camera, 100))
		//	{
				//printf("Enemy sees you\n");
				//state=2;			
		//	}

		Iterate();

		smgr->drawAll();											//Scenemanager drawing
		

		font->draw(L"Score: ",rect<s32>(0,10,300,50),SColor(255,255,255,255));

		if(bullet && bullet->drop())
			bullet=0;

		if(enemy && enemy->drop())
			enemy=0;

		//Draw crosshair
		driver->draw2DRectangle( SColor(255,255,255,255), rect<s32>(cenW,cenH-8,cenW+2,cenH-3) ); //above 
		driver->draw2DRectangle( SColor(255,255,255,255), rect<s32>(cenW+5,cenH,cenW+10,cenH+2) ); //right 
		driver->draw2DRectangle( SColor(255,255,255,255), rect<s32>(cenW,cenH+5,cenW+2,cenH+10) ); //down 
		driver->draw2DRectangle( SColor(255,255,255,255), rect<s32>(cenW-8,cenH,cenW-3,cenH+2) ); //left 
		driver->draw2DRectangle( SColor(255,255,255,255), rect<s32>(cenW,cenH,cenW+2,cenH+2) ); //center of screen
	
		guienv->drawAll();											//Gui environment drawing
Just a bit of main.h

Code: Select all

//Defining variables
bool keys[irr::KEY_KEY_CODES_COUNT];//For key handling
IrrlichtDevice *device;
ITriangleSelector* selector=0;
ISceneManager* smgr; 
IAnimatedMeshSceneNode* enemy = 0;
ISceneNodeAnimator* anim = 0;
ISceneNodeAnimator* anim2 = 0;
ISceneNode * bullet = 0;
bool hit = false;
bool hitw = false;
u32 time;
int score = 0;
ITextSceneNode* textScore =0;

//Creating waypoints for enemy to walk
vector3df wayA = vector3df(627.97, 352.00, 660.74);
vector3df wayB = vector3df(615.87, 352.00, -455.00);


int screenW = 800;
int screenH = 600;
int cenW = screenW / 2;
int cenH = screenH / 2;
SMaterial material;

//Defining classes
class main : public IEventReceiver
This is the code that I needed to make my own bullet (sphere) and delete it etc...

Thanks for all you`re replys and help.

----------------Edit 14-11-08
Hi, today I looked back at all the code with still no wall detection. But I looked it through and after fice minutes I had wall detection and is all my detection working like hell. I want to thank you all for all the replys.

Soon I will pasting my code
Post Reply