AI Class For Irrlicht
-
- Posts: 518
- Joined: Tue Mar 29, 2005 9:02 pm
- Location: Alex,Egypt
- Contact:
AI Class For Irrlicht
Hi All
first of all, i want to thank Omer Emad for his great AI stuff.
I have made a TMove class that looks like Omer Demo,so what this class offer for us?
you can attach it to any node that you normally created,so you still manipulate your node as usual,after that
you can scane the around area for a specific target node by using ScaneArea(target,Redius) Function ,
the redius parameter is the distance for our node and the desired Area, if we found it the then we start to
move forward to that target node by using MoveForward(speed) function , then we test the distance between
our node and target node ,if it is less than 30 we start to attak the target node.
see it in the wiki
http://www.irrforge.org/index.php/AI_Class_For_Irrlicht
here is a screen shots,the first is before seeing the target node,and the second after seeing the target node and start to move frowared to it.
first of all, i want to thank Omer Emad for his great AI stuff.
I have made a TMove class that looks like Omer Demo,so what this class offer for us?
you can attach it to any node that you normally created,so you still manipulate your node as usual,after that
you can scane the around area for a specific target node by using ScaneArea(target,Redius) Function ,
the redius parameter is the distance for our node and the desired Area, if we found it the then we start to
move forward to that target node by using MoveForward(speed) function , then we test the distance between
our node and target node ,if it is less than 30 we start to attak the target node.
see it in the wiki
http://www.irrforge.org/index.php/AI_Class_For_Irrlicht
here is a screen shots,the first is before seeing the target node,and the second after seeing the target node and start to move frowared to it.
Magic 2d Library For Irrlicht : http://www.freewebs.com/bcxgl/index.htm
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
godd job
u have done what i done but in less lines of code
mine was abot 450 lines
however u should really try to add beter vision
ie: field of view (people dont have eyes in the back of their heads )
also u should have some code so enemies dont have xray vision and see through walls lol
see my source but i ve made a nice diagram to explain it
(couldnt get my computer to write arabic so i used paint)
u have done what i done but in less lines of code
mine was abot 450 lines
however u should really try to add beter vision
ie: field of view (people dont have eyes in the back of their heads )
also u should have some code so enemies dont have xray vision and see through walls lol
see my source but i ve made a nice diagram to explain it
(couldnt get my computer to write arabic so i used paint)
-
- Posts: 518
- Joined: Tue Mar 29, 2005 9:02 pm
- Location: Alex,Egypt
- Contact:
Hi Omer
ScaneArea works a way that deffer from your code,so the scaneArea function will start to rotate our node
with determined length of vision, so when it faceing the target node it will stop rotating,and then we move
our node to the traget node. so " people dont have eyes in the back of their heads "
for Example
===========
this is a sample of BCX demo,it will rotate node1 around itself and in the same time node1 move in X-X direction
the scaneArea will test if node2 in range 40 "length of vision" and node1 faceing node2 then node1 stop
moveing in x-x direction and stop rotation and then start to move in node2 direction,after that we test
the distance between node1 and node2 if it is less than 10 node1 stop moving.
so how couls i implement you idea that in diagram with my class?,you see scaneArea works defferent waY.
i will make some code for make the model not seeing through walls ,it is just a beggining.
any way a worm thanks for your great work Omer.
ScaneArea works a way that deffer from your code,so the scaneArea function will start to rotate our node
with determined length of vision, so when it faceing the target node it will stop rotating,and then we move
our node to the traget node. so " people dont have eyes in the back of their heads "
for Example
===========
Code: Select all
if MyNode->ScaneArea(node2,40) then
font->draw(ANSITOWIDE("Found YRot =" + str$(node1->getRotation().Y)),rect<s32>(400,10,300,50),SColor(255,255,0,0))
if MyNode->GetDistance(node2) < 100 then Mx = 0.005
if MyNode->GetDistance(node2) < 10 then Mx = 0
MyNode->MoveForward(Mx)
else
x += ix
if x < -30 then ix = 0.01
if x > 30 then ix = -0.01
node1->setPosition(vector3df(x,0,0))
End if
the scaneArea will test if node2 in range 40 "length of vision" and node1 faceing node2 then node1 stop
moveing in x-x direction and stop rotation and then start to move in node2 direction,after that we test
the distance between node1 and node2 if it is less than 10 node1 stop moving.
so how couls i implement you idea that in diagram with my class?,you see scaneArea works defferent waY.
i will make some code for make the model not seeing through walls ,it is just a beggining.
any way a worm thanks for your great work Omer.
Magic 2d Library For Irrlicht : http://www.freewebs.com/bcxgl/index.htm
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
-
- Posts: 518
- Joined: Tue Mar 29, 2005 9:02 pm
- Location: Alex,Egypt
- Contact:
ok i can spcify the start and the end angles of our scaning area,i will do it
Magic 2d Library For Irrlicht : http://www.freewebs.com/bcxgl/index.htm
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
-
- Posts: 518
- Joined: Tue Mar 29, 2005 9:02 pm
- Location: Alex,Egypt
- Contact:
@Omer i did it
ScaneArea could chech a sector instead of entire circle
here is the ScaneArea Function
and here is a sample code
ScaneArea could chech a sector instead of entire circle
here is the ScaneArea Function
Code: Select all
bool ScaneArea(ISceneNode* trgt,f32 redus = 100000.f,f32 startAngl=0,f32 endAngl=360)
{
if (flag)
{
vector3df CurRot;
CurRot = ScnNode->getRotation();
if (CurRot.Y > 360) CurRot.Y -= 360;
if (CurRot.Y >= startAngl) RotSpeed *= -1; // && CurRot.Y <= endAngl)
if (CurRot.Y <= endAngl) RotSpeed *= -1;
CurRot.Y += RotSpeed;
ScnNode->setRotation(CurRot);
Curforward.set(1,0,0);
ScnNode->getAbsoluteTransformation().rotateVect(Curforward);
line3d<f32> lin;
lin.start = ScnNode->getPosition();
lin.end = lin.start + Curforward * redus;
if (trgt->getTransformedBoundingBox().intersectsWithLine(lin))
{
RotSpeed /= 2;
if (RotSpeed < 0.001)
{
ScnNode->setRotation(getTargetAngle(trgt));
}
return TRUE;
}
}
}
Code: Select all
//scane a sector ,start angel = 0 and end angle = 90
if MyNode->ScaneArea(node2,40,0,90) then
or
// scane the entire circle
if MyNode->ScaneArea(node2,40,) then
Magic 2d Library For Irrlicht : http://www.freewebs.com/bcxgl/index.htm
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
-
- Posts: 448
- Joined: Tue Oct 05, 2004 3:24 am
- Location: Boston, MA
Thats very nice to see. WIth alot of people using the engine for game developing AI could prove to be useful. Very clean as well!
Irrlicht Moderator || Game Designer
Learn the basics at </dream.in.code>
Learn the basics at </dream.in.code>
-
- Posts: 518
- Joined: Tue Mar 29, 2005 9:02 pm
- Location: Alex,Egypt
- Contact:
Hi Omeromaremad wrote:well done
the ting i like about ur class that it is very small,clean and eeficent
may be if u make the wall detection i will burn my own class and use urs
i think now you must burn your own class
i did it ScaneArea does not see through walls
here is the code
Code: Select all
bool ScaneArea(ISceneNode* trgt,f32 redus = 100000.f,f32 startAngl=0,f32 endAngl=360)
{
if (flag)
{
vector3df CurRot;
CurRot = ScnNode->getRotation();
if (CurRot.Y > 360) CurRot.Y -= 360;
if (CurRot.Y >= startAngl) RotSpeed *= -1;
if (CurRot.Y <= endAngl) RotSpeed *= -1;
CurRot.Y += RotSpeed;
ScnNode->setRotation(CurRot);
Curforward.set(1,0,0);
ScnNode->getAbsoluteTransformation().rotateVect(Curforward);
line3d<f32> lin;
lin.start = ScnNode->getPosition();
lin.end = lin.start + Curforward * redus;
if (trgt->getTransformedBoundingBox().intersectsWithLine(lin))
{
if (Selector)
{
array<triangle3df> Triangles;
s32 count;
vector3df intersection;
s32 totalcnt = Selector->getTriangleCount();
Triangles.set_used(totalcnt);
Selector->getTriangles(Triangles.pointer(),totalcnt,count,lin);
for(int i =0; i < count; i++)
{
if (Triangles[i].getIntersectionWithLimitedLine(lin,intersection))
{
f64 L1 = ScnNode->getPosition().getDistanceFrom(trgt->getPosition());
f64 L2 = ScnNode->getPosition().getDistanceFrom(intersection);
if (L1 < L2) goto pass;
return FALSE;
}
}
}
pass:
RotSpeed /= 2;
if (RotSpeed < 0.001)
{
ScnNode->setRotation(getTargetAngle(trgt));
}
return TRUE;
}
}
}
Code: Select all
Global roomMesh as IAnimatedMesh ptr
roomMesh = irrSceneMgr->getMesh("../media/room.3ds")
irrSceneMgr->getMeshManipulator()->makePlanarTextureMapping(roomMesh->getMesh(0),0.008f)
Global room = 0 as ISceneNode ptr
room = irrSceneMgr->addOctTreeSceneNode(roomMesh->getMesh(0))
room->setMaterialTexture(0, irrVideo->getTexture("../media/wall.jpg"))
room->setPosition(vector3df(0,-40,0))
Global selector as ITriangleSelector ptr
selector = irrSceneMgr->createOctTreeTriangleSelector(roomMesh->getMesh(0), room, 128)
room->setTriangleSelector(selector)
selector->drop()
dim mesh as IAnimatedMesh ptr
mesh = irrSceneMgr->getMesh("../media/faerie.md2")
Global node1 as IAnimatedMeshSceneNode ptr
node1 = irrSceneMgr->addAnimatedMeshSceneNode( mesh )
node1->setMaterialFlag(EMF_LIGHTING, false)
node1->setMD2Animation(EMAT_STAND)
node1->setMaterialTexture( 0, irrVideo->getTexture("../media/Faerie5.BMP"))
Global node2 as IAnimatedMeshSceneNode ptr
node2 = irrSceneMgr->addAnimatedMeshSceneNode( mesh )
node2->setMaterialFlag(EMF_LIGHTING, false)
node2->setMD2Animation(EMAT_STAND)
node2->setMaterialTexture( 0, irrVideo->getTexture("../media/Faerie5.BMP"))
node2->setPosition(vector3df(-200,0,-20))
irrSceneMgr->addCameraSceneNode(0, vector3df(150,30,-50), vector3df(0,5,0))
!TMove* MyNode = new TMove(node1,irrSceneMgr);
MyNode->setTriangleSelector(room->getTriangleSelector())
Global font as IGUIFont ptr
font = irrDevice->getGUIEnvironment()->getFont("../media/fonthaettenschweiler.bmp")
While irrDevice->run()
static x!,Mx!,ix!=0.01
irrVideo->beginScene(true, true, SColor(0,200,200,200))
if MyNode->ScaneArea(node2,500) then
' if MyNode->ScaneArea(node2,40,0,90) then
font->draw(ANSITOWIDE("Found YRot =" + str$(node1->getRotation().Y)),rect<s32>(400,10,300,50),SColor(255,255,0,0))
if MyNode->GetDistance(node2) < 500 then Mx = 0.1 ': node1->setMD2Animation(EMAT_RUN)
if MyNode->GetDistance(node2) < 10 then Mx = 0 ': node1->setMD2Animation(EMAT_ATTACK)
MyNode->MoveForward(Mx)
else
x += ix
if x < -130 then ix = 0.01
if x > 30 then ix = -0.01
node1->setPosition(vector3df(100,0,x))
End if
font->draw(ANSITOWIDE("Node1 pos =" + str$(node1->getPosition().X)),rect<s32>(400,30,300,50),SColor(255,0,0,255))
font->draw(ANSITOWIDE("Node1 YRot =" + str$(node1->getRotation().Y)),rect<s32>(400,50,300,50),SColor(255,0,0,255))
irrSceneMgr->drawAll()
irrVideo->endScene()
Wend
irrDevice->drop()
Magic 2d Library For Irrlicht : http://www.freewebs.com/bcxgl/index.htm
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
-
- Posts: 518
- Joined: Tue Mar 29, 2005 9:02 pm
- Location: Alex,Egypt
- Contact:
Hi All
i have add new feature for this class.
you could specify a path for your enemy , so that the enemy will move
in the path while it scan the area.
suppose you have a gate in your game and want to protect it with
an enemy, so you could specify a path near to that gate and while the
enemy walk in path he scan the area if he found the player then he
will move froward to the player and start to attack the player.
this idea could be implemented by my class.
here is the code of TMove class
and here is how you could use it
hope that this class will be useful for someone how want to creating a game
feel free to improve and optimize the class,and let me know if you did it
i have add new feature for this class.
you could specify a path for your enemy , so that the enemy will move
in the path while it scan the area.
suppose you have a gate in your game and want to protect it with
an enemy, so you could specify a path near to that gate and while the
enemy walk in path he scan the area if he found the player then he
will move froward to the player and start to attack the player.
this idea could be implemented by my class.
here is the code of TMove class
Code: Select all
enum MStatue
{
scan,
found
};
class TMove
{
bool flag;
vector3df Curforward;
f32 RotSpeed;
ISceneNode* ScnNode;
ISceneManager* SceneMgr;
ITriangleSelector* Selector;
array<triangle3df> Triangles;
array<vector3df> Path;
s32 indx;
f32 fctr;
f32 MovSpeed;
MStatue statue;
public:
TMove(ISceneNode* node,ISceneManager* ScnMgr)
{
ScnNode = node;
SceneMgr = ScnMgr;
RotSpeed = 0.1;
flag = TRUE;
Selector = NULL;
indx = 0;
MovSpeed = 0.0001;
fctr = 0.0;
statue = scan;
}
~TMove() {}
void Disable() { flag = FALSE; }
void Enable() { flag = TRUE; }
void setRotationSpeed(f32 RotSpd) { RotSpeed = RotSpd; }
void setMoveSpeed(f32 MovSpd) { MovSpeed = MovSpd; }
void setTriangleSelector(ITriangleSelector* Sel) { Selector = Sel; }
void addPathPoint(vector3df& point) { Path.push_back( point ); }
bool ScanArea(ISceneNode* trgt,f32 redus = 100000.f,f32 startAngl=0,f32 endAngl=360)
{
if (flag)
{
if (statue == scan)
{
vector3df p = Path[indx+1].getInterpolated(Path[indx],fctr);
fctr += MovSpeed;
if (fctr >= 1.0) {indx++; fctr = 0.0;}
if (indx >= Path.size()-1) indx = 0;
ScnNode->setPosition(p);
}
vector3df CurRot;
CurRot = ScnNode->getRotation();
if (CurRot.Y > 360) CurRot.Y -= 360;
if (CurRot.Y >= startAngl) RotSpeed *= -1;
if (CurRot.Y <= endAngl) RotSpeed *= -1;
CurRot.Y += RotSpeed;
ScnNode->setRotation(CurRot);
Curforward.set(1,0,0);
ScnNode->getAbsoluteTransformation().rotateVect(Curforward);
line3d<f32> lin;
lin.start = ScnNode->getPosition();
lin.end = lin.start + Curforward * redus;
if (trgt->getTransformedBoundingBox().intersectsWithLine(lin))
{
if (Selector)
{
s32 count;
vector3df intersection;
s32 totalcnt = Selector->getTriangleCount();
Triangles.set_used(totalcnt);
Selector->getTriangles(Triangles.pointer(),totalcnt,count,lin);
for(int i =0; i < count; i++)
{
if (Triangles[i].getIntersectionWithLimitedLine(lin,intersection))
{
f64 L1 = ScnNode->getPosition().getDistanceFrom(trgt->getPosition());
f64 L2 = ScnNode->getPosition().getDistanceFrom(intersection);
if (L1 < L2) goto pass;
statue = scan;
return FALSE;
}
}
}
pass:
RotSpeed /= 2;
if (RotSpeed < 0.001)
{
ScnNode->setRotation(getTargetAngle(trgt));
statue = found;
}
return TRUE;
}
}
}
void MoveForward(f32 speed)
{
vector3df newPos= ScnNode->getPosition() + (Curforward * speed);
ScnNode->setPosition(newPos);
}
f64 GetDistance(ISceneNode* trgt)
{
vector3df pos = ScnNode->getPosition();
return pos.getDistanceFrom(trgt->getPosition());
}
vector3df getTargetAngle(ISceneNode* trgt)
{
vector3df v = ScnNode->getPosition();
vector3df r = trgt->getPosition();
vector3df angle;
float x,y,z;
x = r.X - v.X;
y = r.Y - v.Y;
z = r.Z - v.Z;
angle.Y = atan2 (x, z);
angle.Y *= (180 / PI);
angle.Y-=90;
if(angle.Y < 0) angle.Y += 360;
if(angle.Y >= 360) angle.Y -= 360;
float z1 = sqrt(x*x + z*z);
angle.X = atan2 (z1, y);
angle.X *= (180 / PI);
angle.X -= 90;
if(angle.X < 0) angle.X += 360;
if(angle.X >= 360) angle.X -= 360;
return angle;
}
};
Code: Select all
//creating new instance of our class
// and attach it to EnemyNode
TMove* MyNode = new TMove(EnemyNode,irrSceneMgr);
//here our enemy will no seeing through the room walls
MyNode->setTriangleSelector(room->getTriangleSelector());
//here we set our path that must consistes of more than
// 2 points,so enemy will Interpolate between the points of path
MyNode->addPathPoint(vector3df(100,0,30)); //point 0
MyNode->addPathPoint(vector3df(100,0,-130)); //point 1
MyNode->addPathPoint(vector3df(0,0,-130)); //point 2
// here we specify the rotation speed and movment speed
MyNode->setRotationSpeed(0.3);
MyNode->setMoveSpeed(0.0001);
//and here is the main loop of our game
While irrDevice->run()
{
irrVideo->beginScene(true, true, SColor(0,200,200,200))
//scan our area
if (MyNode->ScanArea(node2,500))
{
f32 Mx;
// if enemy far from player start to move forarard to player
if (MyNode->GetDistance(node2) < 500) Mx = 0.1;
// if enamy is near of player stop moveing and start to attack
if (MyNode->GetDistance(node2) < 10) Mx = 0;
MyNode->MoveForward(Mx);
}
....
.....
}
feel free to improve and optimize the class,and let me know if you did it
Magic 2d Library For Irrlicht : http://www.freewebs.com/bcxgl/index.htm
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
-
- Posts: 518
- Joined: Tue Mar 29, 2005 9:02 pm
- Location: Alex,Egypt
- Contact:
thanks Omeromaremad wrote:cool idea im going to implement it for my ai
some thing that will make excellent is very simple
just add animation support
i will add animation support
Magic 2d Library For Irrlicht : http://www.freewebs.com/bcxgl/index.htm
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
-
- Posts: 518
- Joined: Tue Mar 29, 2005 9:02 pm
- Location: Alex,Egypt
- Contact:
Hi ALL
i have added some new function that make the Bullet emulation is so
easy
added function
1-getLineStart()
2-getLineEnd()
3-getBulletCoords(f32 BultSpeed)
the last one is the important one, because it return the coordinats of
our Bullet when the enemy start to attack then player,it Interpolated
the start and the end of line between the enemy and player.
here is the new class code
and here is a demo
i think the code is very simple and easy and self Explaination
wait, there is more comeing soon
thanks
i have added some new function that make the Bullet emulation is so
easy
added function
1-getLineStart()
2-getLineEnd()
3-getBulletCoords(f32 BultSpeed)
the last one is the important one, because it return the coordinats of
our Bullet when the enemy start to attack then player,it Interpolated
the start and the end of line between the enemy and player.
here is the new class code
Code: Select all
enum MStatue
{
scan,
found
};
class TMove
{
bool flag;
vector3df Curforward;
f32 RotSpeed;
ISceneNode* ScnNode;
ISceneManager* SceneMgr;
ITriangleSelector* Selector;
array<triangle3df> Triangles;
array<vector3df> Path;
s32 indx;
f32 fctr;
f32 MovSpeed;
MStatue statue;
line3d<f32> line;
f32 BultFctr;
public:
TMove(ISceneNode* node,ISceneManager* ScnMgr)
{
ScnNode = node;
SceneMgr = ScnMgr;
RotSpeed = 0.1;
flag = TRUE;
Selector = NULL;
indx = 0;
MovSpeed = 0.0001;
fctr = 0.0;
statue = scan;
BultFctr = 0;
}
~TMove() {}
void Disable() { flag = FALSE; }
void Enable() { flag = TRUE; }
void setRotationSpeed(f32 RotSpd) { RotSpeed = RotSpd; }
void setMoveSpeed(f32 MovSpd) { MovSpeed = MovSpd; }
void setTriangleSelector(ITriangleSelector* Sel) { Selector = Sel; }
void addPathPoint(vector3df& point) { Path.push_back( point ); }
vector3df getLineStart() { return line.start;}
vector3df getLineEnd() { return line.end;}
vector3df getBulletCoords(f32 BultSpeed)
{
BultFctr += BultSpeed;
if (BultFctr >= 1.0)BultFctr = 0;
return (line.end.getInterpolated(line.start,BultFctr));
}
bool ScanArea(ISceneNode* trgt,f32 redus = 100000.f,f32 startAngl=0,f32 endAngl=360)
{
if (flag)
{
if (statue == scan)
{
vector3df p = Path[indx+1].getInterpolated(Path[indx],fctr);
fctr += MovSpeed;
if (fctr >= 1.0) {indx++; fctr = 0.0;}
if (indx >= Path.size()-1) indx = 0;
ScnNode->setPosition(p);
}
vector3df CurRot;
CurRot = ScnNode->getRotation();
if (CurRot.Y > 360) CurRot.Y -= 360;
if (CurRot.Y >= startAngl) RotSpeed *= -1;
if (CurRot.Y <= endAngl) RotSpeed *= -1;
CurRot.Y += RotSpeed;
ScnNode->setRotation(CurRot);
Curforward.set(1,0,0);
ScnNode->getAbsoluteTransformation().rotateVect(Curforward);
line.start = ScnNode->getPosition();
line.end = line.start + Curforward * redus;
if (trgt->getTransformedBoundingBox().intersectsWithLine(line))
{
if (Selector)
{
s32 count;
vector3df intersection;
s32 totalcnt = Selector->getTriangleCount();
Triangles.set_used(totalcnt);
Selector->getTriangles(Triangles.pointer(),totalcnt,count,line);
for(int i =0; i < count; i++)
{
if (Triangles[i].getIntersectionWithLimitedLine(line,intersection))
{
f64 L1 = ScnNode->getPosition().getDistanceFrom(trgt->getPosition());
f64 L2 = ScnNode->getPosition().getDistanceFrom(intersection);
if (L1 < L2) goto pass;
statue = scan;
return FALSE;
}
}
}
pass:
RotSpeed /= 2;
if (RotSpeed < 0.001)
{
ScnNode->setRotation(getTargetAngle(trgt));
statue = found;
}
return TRUE;
}
}
}
void MoveForward(f32 speed)
{
vector3df newPos= ScnNode->getPosition() + (Curforward * speed);
ScnNode->setPosition(newPos);
}
f64 GetDistance(ISceneNode* trgt)
{
vector3df pos = ScnNode->getPosition();
return pos.getDistanceFrom(trgt->getPosition());
}
vector3df getTargetAngle(ISceneNode* trgt)
{
vector3df v = ScnNode->getPosition();
vector3df r = trgt->getPosition();
vector3df angle;
float x,y,z;
x = r.X - v.X;
y = r.Y - v.Y;
z = r.Z - v.Z;
angle.Y = atan2 (x, z);
angle.Y *= (180 / PI);
angle.Y-=90;
if(angle.Y < 0) angle.Y += 360;
if(angle.Y >= 360) angle.Y -= 360;
float z1 = sqrt(x*x + z*z);
angle.X = atan2 (z1, y);
angle.X *= (180 / PI);
angle.X -= 90;
if(angle.X < 0) angle.X += 360;
if(angle.X >= 360) angle.X -= 360;
return angle;
}
};
Code: Select all
//creating new instance of our class
// and attach it to EnemyNode
TMove* MyNode = new TMove(EnemyNode,irrSceneMgr);
//here our enemy will no seeing through the room walls
MyNode->setTriangleSelector(room->getTriangleSelector());
//here we set our path that must consistes of more than
// 2 points,so enemy will Interpolate between the points of path
MyNode->addPathPoint(vector3df(100,0,30)); //point 0
MyNode->addPathPoint(vector3df(100,0,-130)); //point 1
MyNode->addPathPoint(vector3df(0,0,-130)); //point 2
// here we specify the rotation speed and movment speed
MyNode->setRotationSpeed(0.3);
MyNode->setMoveSpeed(0.0001);
//here is a defination of bullet
ISceneNode* Bullet = 0;
Bullet = irrSceneMgr->addBillboardSceneNode(0,dimension2d<f32>(25,25));
Bullet->setMaterialFlag(EMF_LIGHTING, false);
Bullet->setMaterialTexture(0, irrVideo->getTexture("../media1/fireball.bmp"));
Bullet->setMaterialType(EMT_TRANSPARENT_ADD_COLOR)
Bullet->setVisible(false) ;
//and here is the main loop of our game
While irrDevice->run()
{
irrVideo->beginScene(true, true, SColor(0,200,200,200))
//scan our area
if (MyNode->ScanArea(node2,500))
{
f32 Mx;
// if enemy far from player start to move forarard to player
if (MyNode->GetDistance(node2) < 500) Mx = 0.1;
// if enamy is near of player stop moveing and start to attack
if (MyNode->GetDistance(node2) < 100)
{
Mx = 0; // stop moveing enemy
Bullet->setVisible(true); //make the bullet visible
// get the new bullet coordenats and set it's position
Bullet->setPosition(MyNode->getBulletCoords(0.001));
}
MyNode->MoveForward(Mx);
}
....
.....
}
wait, there is more comeing soon
thanks
Magic 2d Library For Irrlicht : http://www.freewebs.com/bcxgl/index.htm
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/
http://www.freewebs.com/bcxdx/index.htm
http://groups.yahoo.com/group/bcxdxc/