shooting from projectile, formula for line.end?,draw3dline

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
jsmurthy4
Posts: 18
Joined: Mon Sep 10, 2007 4:04 pm

shooting from projectile, formula for line.end?,draw3dline

Post by jsmurthy4 »

Hi,
i attached a projector as a child to a camera.when I shoot, if the projector is pointing towards the enemy then he has to die or else not.
For above task I took a line and to get enemy node I will use getSceneNodeFromRayBB before that I want to see how line moves by using draw3dline

Code: Select all

While()
{
line.start=projectors->getAbsolutePosition();
line.end=line.start+(camera->getTarget()-line.start).normalize()*1000.0f;
draw3dline(line.start,line.end,white);
}
I am able to see a line from projector (line.start) but line.end is ending some where else, as I move camera or rotate, line.end is not horizontal to projector.

:?: camera->getTarget() what will return only postion or rotation or both ?

:?: Let suppose camera position is (0,0,0) and line.start =(0,0,0) then
(Camera->getTarget()-line.start).normalize will be what? I mean what is unit vector and how it works?

:?: I think line.end formula is wrong that else we have to use?

help me :shock:
Last edited by jsmurthy4 on Thu Apr 24, 2008 4:11 am, edited 1 time in total.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

When drawing a 3D line like that you need to setup the driver's tranformation matrix and rendering material.

The API explains how to do this if you search for the draw3dline function.

To answer you questions:

camera->getTarget returns a 3D position which is the point in the world that the camera is looking at. That's basically all you need for this, not rotation or anything.

Your second question would depend on what the camera's target is, it will not be changed by the camera's position as the result is a unit vector which is bascially just a direction in 3D space and its length is 1. This can be used to tell your projectile (what i assume you mean by projector) in which direction to fire.

You line.end should be fine, it's just rendering wrong because you haven't setup the driver's transformation matrix properly.
Image Image Image
jsmurthy4
Posts: 18
Joined: Mon Sep 10, 2007 4:04 pm

Post by jsmurthy4 »

Thanks Jp,
for ur reply,
i declared driver's transformation matrix check it

Code: Select all

lineMaterial.Lighting = false; 
    lineMaterial.Thickness = 3.f; 

	while(device->run() && driver)
	{
		int campos=myupdate::update(killer,camera); 
		driver->beginScene(true,true,0);
		
		line.start=pointer->getAbsolutePosition();
		line.end=(camera->getTarget()-line.start).normalize()*1000.0f;
		 
		driver->setTransform(video::ETS_WORLD, core::matrix4()); 
		 driver->setMaterial(lineMaterial);
		driver->draw3DLine(line.start,line.end,video::SColor(255, 0,0,255)); 
		
		
		smgr->drawAll();
		
		
		driver->endScene();
			}
	 driver->drop();
	 return 0;

}
in the above code if put

Code: Select all

line.end=line.start+(camera->getTarget()-line.start).normalize()*1000.0f;
starting point and ending point are coming close so i am unable to see line

:?: i try to draw line in blue color but it displays in bkground skycolor is there any wrong ?

:?: How to draw a straight line to projectile?
Last edited by jsmurthy4 on Thu Apr 24, 2008 2:45 pm, edited 1 time in total.
jsmurthy4
Posts: 18
Joined: Mon Sep 10, 2007 4:04 pm

draw3dline

Post by jsmurthy4 »

Hi,
i look some examples which are provided my irrlicht developers
example-7 ,collision example in that example i put draw3dline below draw3dtriangle. i can't see line but triangle is perfect.

Code: Select all

#include <irrlicht.h>
#include <iostream>

using namespace irr;

#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif


int main()
{
	IrrlichtDevice *device =
		createDevice(video::EDT_DIRECT3D9, core::dimension2d<s32>(800, 640), 16, false);
		
	if (device == 0)
		return 1; // could not create selected driver.

	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();

	
	device->getFileSystem()->addZipFileArchive("../../media/map-20kdm2.pk3");

	
	scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
	scene::ISceneNode* q3node = 0;
	
	if (q3levelmesh)
		q3node = smgr->addOctTreeSceneNode(q3levelmesh->getMesh(0));

	scene::ITriangleSelector* selector = 0;
	
	if (q3node)
	{		
		q3node->setPosition(core::vector3df(-1350,-130,-1400));

		selector = smgr->createOctTreeTriangleSelector(q3levelmesh->getMesh(0), q3node, 128);
		q3node->setTriangleSelector(selector);
	}

	scene::ICameraSceneNode* camera = 
		smgr->addCameraSceneNodeFPS(0, 100.0f, 300.0f, -1, 0, 0, true);
	camera->setPosition(core::vector3df(-100,50,-150));

	if (selector)
	{
		scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
			selector, camera, core::vector3df(30,50,30),
			core::vector3df(0,-3,0), 
			core::vector3df(0,50,0));
		selector->drop();
		camera->addAnimator(anim);
		anim->drop();
	}

	// disable mouse cursor

	device->getCursorControl()->setVisible(false);

	// add billboard

	scene::IBillboardSceneNode * bill = smgr->addBillboardSceneNode();
	bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );
	bill->setMaterialTexture(0, driver->getTexture("../../media/particle.bmp"));
	bill->setMaterialFlag(video::EMF_LIGHTING, false);
	bill->setMaterialFlag(video::EMF_ZBUFFER, false);
	bill->setSize(core::dimension2d<f32>(20.0f, 20.0f));

	// add 3 animated faeries.

	video::SMaterial material;
	material.setTexture(0, driver->getTexture("../../media/faerie2.bmp"));
	material.Lighting = true;

	scene::IAnimatedMeshSceneNode* node = 0;
	scene::IAnimatedMesh* faerie = smgr->getMesh("../../irrlicht-1.4/media/faerie.md2");

	if (faerie)
	{
		node = smgr->addAnimatedMeshSceneNode(faerie);
		node->setPosition(core::vector3df(-70,0,-90));
		node->setMD2Animation(scene::EMAT_RUN);
		node->getMaterial(0) = material;

		node = smgr->addAnimatedMeshSceneNode(faerie);
		node->setPosition(core::vector3df(-70,0,-30));
		node->setMD2Animation(scene::EMAT_SALUTE);
		node->getMaterial(0) = material;

		node = smgr->addAnimatedMeshSceneNode(faerie);
		node->setPosition(core::vector3df(-70,0,-60));
		node->setMD2Animation(scene::EMAT_JUMP);
		node->getMaterial(0) = material;
	}

	material.setTexture(0, 0);
	material.Lighting = false;

	// Add a light

	smgr->addLightSceneNode(0, core::vector3df(-60,100,400),
		video::SColorf(1.0f,1.0f,1.0f,1.0f),
		600.0f);

	scene::ISceneNode* selectedSceneNode = 0;
	scene::ISceneNode* lastSelectedSceneNode = 0;

		
	int lastFPS = -1;

	while(device->run())
	if (device->isWindowActive())
	{
		driver->beginScene(true, true, 0);

		smgr->drawAll();

		core::line3d<f32> line;
		line.start = camera->getPosition();
		line.end = line.start + (camera->getTarget() - line.start).normalize() * 1000.0f;

		core::vector3df intersection;
		core::triangle3df tri;

		if (smgr->getSceneCollisionManager()->getCollisionPoint(
			line, selector, intersection, tri))
		{
			bill->setPosition(intersection);
				
			driver->setTransform(video::ETS_WORLD, core::matrix4());
			driver->setMaterial(material);
			driver->draw3DTriangle(tri, video::SColor(0,255,0,0));

/*************DRAW3DLINE***************************/
			driver->draw3DLine(line.start,line.end,video::SColor(0,0,0,255));
/******************************************/

		}


		selectedSceneNode = smgr->getSceneCollisionManager()->getSceneNodeFromCameraBB(camera);

		if (lastSelectedSceneNode)
			lastSelectedSceneNode->setMaterialFlag(video::EMF_LIGHTING, true);

		if (selectedSceneNode == q3node || selectedSceneNode == bill)
			selectedSceneNode = 0;

		if (selectedSceneNode)
			selectedSceneNode->setMaterialFlag(video::EMF_LIGHTING, false);

		lastSelectedSceneNode = selectedSceneNode;


		/*
		That's it, we just have to finish drawing.
		*/

		driver->endScene();

		
		core::stringw str="Camera-targetX=";
		str+=(int)camera->getTarget().X;
		str+=",Z="; 
		str+=(int)camera->getTarget().Z;
		str+=",lineSt,X=";
		str+=(int)line.start.X;
		str+=",Y=";
		str+=(int)line.start.Y;
		str+=",Z=";
		str+=(int)line.start.Z;
		
		str+=",lineed,X=";
		str+=(int)line.end.X;
		str+=",Y=";
		str+=(int)line.end.Y;
		str+=",Z=";
		str+=(int)line.end.Z;
	
		  device->setWindowCaption(str.c_str());
		
	}

	device->drop();
	
	return 0;
}
:?: why it is not drawing line?
i am using irrlicht 1.4 ver above example is 07.collision example
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Imagine that the line is a piece of fishing line. You can easily see the line when looking at it from the side, but what do you see when you are looking down the end. In the best case you'd see the cross section of the line. But this line doesn't have a cross section.

That is exactly what you are doing here. If you want to see the line, then offset the start point just a little so that you can see the side of it.

Travis
jsmurthy4
Posts: 18
Joined: Mon Sep 10, 2007 4:04 pm

Post by jsmurthy4 »

Thank u,
vitek u are right i offset starting point and i am able to see the line.

:arrow: drawing after smgr->drawAll()
so that u can see the color u set for or else line is displayed in bkground color.

THANK'S JP,VITEK for u help :D
Post Reply