AI for 3rd person camera !?
AI for 3rd person camera !?
Hi guy's thank's for entering this thread. i have a couple of question, i hope
you want to help me
note: I made it with irrlicht 11.0 + MSVC 2003.
Map with Gile[s] and converted into my3d.
1. I try to implement the 3rd person camera code from boogle's and
the AI code from "emil" (thank's guys for your code ). i compile it and
it done! but the node i assume as enemy, did not try to go towards me.
he only rotate along the path!
I try to find the incorrectness of my code, and i found that the collision
is not worked well. if i remove the collision from my player, it's done
but if i try to made the collision the same thing is hapen again
why is that????? please help me!
i try to posted my code, but is too long, is it okay with this forum?
2. i made map with Gile[s] and convert it to my3d file, but every time
my character move, every edege of my map looks jittery. for ex:
i have a house in my map, and every time i move, the edge of the house that connect with the ground will jittery. why ??
please somebody help the noob's here
you want to help me
note: I made it with irrlicht 11.0 + MSVC 2003.
Map with Gile[s] and converted into my3d.
1. I try to implement the 3rd person camera code from boogle's and
the AI code from "emil" (thank's guys for your code ). i compile it and
it done! but the node i assume as enemy, did not try to go towards me.
he only rotate along the path!
I try to find the incorrectness of my code, and i found that the collision
is not worked well. if i remove the collision from my player, it's done
but if i try to made the collision the same thing is hapen again
why is that????? please help me!
i try to posted my code, but is too long, is it okay with this forum?
2. i made map with Gile[s] and convert it to my3d file, but every time
my character move, every edege of my map looks jittery. for ex:
i have a house in my map, and every time i move, the edge of the house that connect with the ground will jittery. why ??
please somebody help the noob's here
bil':)
-
- Posts: 518
- Joined: Tue Mar 29, 2005 9:02 pm
- Location: Alex,Egypt
- Contact:
Hi
if your code is so big,try to post a small part of it ,so that any one her could
help you.
thanks
if your code is so big,try to post a small part of it ,so that any one her could
help you.
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/
Ok this is my code
I have a couple of question here as a newbie, than'x before for your help:)
1.I got this code from this forum, code for 3rd person view from this forum by Boogle's
and it try to implement it with "emil" TMOVE Class. it is succesfully compile,
but noting happen.
i try to make my enemy move if he see my character but my enemy only move along the path
not try to go towards me:(
is my code wrong?
somebody please help the newby here:(
2.i have a question too about this 3rd person camera i used. i add distance to this camera
in main loop like this :
camera->setTarget(p1->getPosition());
f64 distance = camera->getPosition().getDistanceFrom(camera->getTarget()+core::vector3df(-500,HEIGHT,0));
core::vector3df cam_movedir = (camera->getTarget()+core::vector3df(-500,HEIGHT,0))-camera->getPosition();
cam_movedir.normalize();
but everytime i want to rotate my camera, it's always turn to the
position, why is that? help me.
3.I make my map with Gile[s] and transfer it to my3d file, and sucessfully loaded.
But every time i move my character, every edge of my map become jitery. for example:
i have a house in my map and every time i move the edge between the house and
the land become jitter.
what makes this problem, is the camera or what?
i'm sorry if the code is messy
1.I got this code from this forum, code for 3rd person view from this forum by Boogle's
and it try to implement it with "emil" TMOVE Class. it is succesfully compile,
but noting happen.
i try to make my enemy move if he see my character but my enemy only move along the path
not try to go towards me:(
is my code wrong?
somebody please help the newby here:(
2.i have a question too about this 3rd person camera i used. i add distance to this camera
in main loop like this :
camera->setTarget(p1->getPosition());
f64 distance = camera->getPosition().getDistanceFrom(camera->getTarget()+core::vector3df(-500,HEIGHT,0));
core::vector3df cam_movedir = (camera->getTarget()+core::vector3df(-500,HEIGHT,0))-camera->getPosition();
cam_movedir.normalize();
but everytime i want to rotate my camera, it's always turn to the
position, why is that? help me.
3.I make my map with Gile[s] and transfer it to my3d file, and sucessfully loaded.
But every time i move my character, every edge of my map become jitery. for example:
i have a house in my map and every time i move the edge between the house and
the land become jitter.
what makes this problem, is the camera or what?
Code: Select all
///////////////////////////////////
#include <irrlicht.h>
#include <iostream>
#include <stdio.h>
#include <wchar.h>
using namespace irr;
using namespace scene;
using namespace core;
using namespace video;
using namespace io;
using namespace gui;
#pragma comment(lib, "Irrlicht.lib")
scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;
scene::IAnimatedMeshSceneNode* Model = 0;
#define C_UP 1
#define C_DOWN 2
#define C_LEFT 4
#define C_RIGHT 8
#define C_CAML 16
#define C_CAMR 32
#define MOVESPEED 2.0f
#define ROTSPEED 4.0f
#define HEIGHT 500.0f
scene::ICameraSceneNode* camera;
scene::IAnimatedMeshSceneNode* p1;
scene::IAnimatedMeshSceneNode* EnemyNode;
float cam_rotate=2;
float p1_rotate=0;
float moving=0;
unsigned int controls=0;
bool playerisrunning;
bool playerisstanding;
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;
}
};
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
{
unsigned int k=0;
switch(event.KeyInput.Key) {
case(KEY_UP):
k=C_UP;
break;
case(KEY_DOWN):
k=C_DOWN;
break;
case(KEY_LEFT):
k=C_LEFT;
break;
case(KEY_RIGHT):
k=C_RIGHT;
break;
case(KEY_KEY_A):
k=C_CAML;
break;
case(KEY_KEY_S):
k=C_CAMR;
break;
}
//if(k) {
// if(event.KeyInput.PressedDown)
// controls |= k;
// else
// controls ^= k;
// return true;
// }
if(k)
{
if(event.KeyInput.PressedDown)
{
controls |= k;
if(!playerisrunning)
{
p1->setFrameLoop(320,360);
playerisrunning = true;
playerisstanding = false;
}
}
else
{
controls ^= k;
//if(!controls & C_UP + C_DOWN +C_LEFT + C_RIGHT)
if(!playerisstanding)
{
//p1->setFrameLoop(0, 79*8);
p1->setMD2Animation(irr::scene::EMAT_STAND);
playerisstanding = true;
playerisrunning =false;
}
}
return true;
}
}
return false;
}
};
void resolve_controls_RE() {
if(controls & C_UP + C_DOWN)
moving = controls & C_UP ? MOVESPEED : -MOVESPEED;
if(controls & C_LEFT + C_RIGHT) {
p1_rotate += controls & C_RIGHT ? ROTSPEED : (360.0f-ROTSPEED);
if(p1_rotate>=360.0f)
p1_rotate -= 360.0f;
p1->setRotation(core::vector3df(0,p1_rotate,0));
}
if(controls & C_CAML + C_CAMR)
cam_rotate = controls & C_CAMR ? ROTSPEED : -ROTSPEED;
}
void resolve_controls_WW() {
p1_rotate=0;
switch(controls & C_UP+C_DOWN+C_LEFT+C_RIGHT) {
case(C_UP+C_LEFT):
p1_rotate+=45.0f;
case(C_LEFT):
p1_rotate+=45.0f;
case(C_DOWN+C_LEFT):
p1_rotate+=45.0f;
case(C_DOWN):
p1_rotate+=45.0f;
case(C_DOWN+C_RIGHT):
p1_rotate+=45.0f;
case(C_RIGHT):
p1_rotate+=45.0f;
case(C_UP+C_RIGHT):
p1_rotate+=45.0f;
case(C_UP):
core::matrix4 matty;
matty.buildCameraLookAtMatrixLH(camera->getPosition(),camera->getTarget(),camera->getUpVector());
p1_rotate+=matty.getRotationDegrees().Z+270.0f;
while(p1_rotate>=360.0f) {
p1_rotate-=360.0f;
}
float foo = p1->getRotation().Y-p1_rotate;
if(foo<0)
foo=-foo;
if(foo<45.0f || foo>360.0f-45.0f)
moving = MOVESPEED;
if(foo>ROTSPEED || foo>360.0f-ROTSPEED) {
if(foo<180.0f)
if(p1->getRotation().Y>p1_rotate)
p1->setRotation(p1->getRotation() + core::vector3df(0,360.0f-ROTSPEED*2,0));
else
p1->setRotation(p1->getRotation() + core::vector3df(0,ROTSPEED*2,0));
else
if(p1->getRotation().Y<p1_rotate)
p1->setRotation(p1->getRotation() + core::vector3df(0,360.0f-ROTSPEED*2,0));
else
p1->setRotation(p1->getRotation() + core::vector3df(0,ROTSPEED*2,0));
if(p1->getRotation().Y>=360.0f)
p1->setRotation(p1->getRotation() - core::vector3df(0,360.0f,0));
}
}
if(controls & C_CAML + C_CAMR)
cam_rotate = controls & C_CAMR ? ROTSPEED : -ROTSPEED;
}
int main()
{MyEventReceiver receiver;
device = createDevice(video::EDT_DIRECTX9,core::dimension2d<s32>(1024, 768),16, false, false);
device->setEventReceiver(&receiver);
if (device == 0)
return 1; // could not create selected driver.
irr::scene::IAnimatedMesh* mesh;
scene::ITriangleSelector* selector = 0;
video::IVideoDriver* driver = device->getVideoDriver(); scene::ISceneManager* smgr = device->getSceneManager();
gui::IGUIEnvironment* env = device->getGUIEnvironment(); driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
mesh = smgr->getMesh("data/untitled.my3d");
scene::ISceneNode* test_scene = smgr->addOctTreeSceneNode(mesh->getMesh(0));
test_scene->setPosition( irr::core::vector3df(0,0,0) );
test_scene->setScale( irr::core::vector3df(1,1,1) );
test_scene->setRotation( irr::core::vector3df(0,0,0) );
test_scene->setMaterialFlag(video::EMF_LIGHTING, true);
selector = smgr->createOctTreeTriangleSelector(mesh->getMesh(0),test_scene, 128);
test_scene->setTriangleSelector(selector);
selector->drop();
device->getCursorControl()->setVisible(false);
mesh = smgr->getMesh("karakter/Chastity.md2");
p1 = smgr->addAnimatedMeshSceneNode( mesh );
p1->setScale(core::vector3df(3,3,3));
p1->setPosition(core::vector3df(100,590,100));
p1->setRotation(core::vector3df(0,-90,0));
scene::ISceneNode* p1_target = smgr->addEmptySceneNode(p1);
p1_target->setPosition(core::vector3df(1,0,0));
p1->setMaterialFlag(video::EMF_LIGHTING, false);
p1->setMD2Animation(irr::scene::EMAT_STAND);
p1->setAnimationSpeed(30);
p1->setMaterialTexture(0, driver->getTexture("karakter/Chastity1.pcx"));
core::vector3df dimensions = mesh->getBoundingBox().MaxEdge - mesh->getBoundingBox().MinEdge; scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(selector, p1, dimensions, core::vector3df(0,-100,0), core::vector3df(0,50,0));
p1->addAnimator(anim);
anim->drop();
camera = smgr->addCameraSceneNode(0, core::vector3df(710,1000,1000), core::vector3df(0,0,150));
mesh = smgr->getMesh("karakter/Chastity.md2");
EnemyNode = smgr->addAnimatedMeshSceneNode( mesh );
EnemyNode->setScale(core::vector3df(3,3,3));
EnemyNode->setPosition(core::vector3df(300,600,100));
EnemyNode->setMaterialFlag(video::EMF_LIGHTING, false);
EnemyNode->setMD2Animation(irr::scene::EMAT_STAND);
EnemyNode->setAnimationSpeed(30);
EnemyNode->setMaterialTexture(0, driver->getTexture("karakter/Chastity1.pcx"));
core::vector3df dimensions2 = mesh->getBoundingBox().MaxEdge - mesh->getBoundingBox().MinEdge;
scene::ISceneNodeAnimator* anim2 = smgr->createCollisionResponseAnimator(selector,EnemyNode, dimensions2,
core::vector3df(0,-100,0),core::vector3df(0,50,0));
EnemyNode->addAnimator(anim2);
anim2->drop();
TMove* MyNode = new TMove(EnemyNode,smgr);
MyNode->addPathPoint(vector3df(100,0,30));
MyNode->addPathPoint(vector3df(100,0,-30));
MyNode->addPathPoint(vector3df(50,0,-80));
MyNode->addPathPoint(vector3df(0,0,-80));
// here we specify the rotation speed and movment speed
MyNode->setRotationSpeed(0.3);
MyNode->setMoveSpeed(0.0001);
scene::IParticleSystemSceneNode* ps;
ps = smgr->addParticleSystemSceneNode(false);
ps->setMaterialFlag(video::EMF_LIGHTING, false);
ps->setPosition(core::vector3df(-654,0,0));
ps->setScale(core::vector3df(5,5,5));
ps->setParticleSize(core::dimension2d<f32>(20,20));
ps->setMaterialTexture(0, driver->getTexture("./sprites/particle_white.bmp"));
ps->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);
scene::IParticleEmitter* emb = ps->createBoxEmitter(
core::aabbox3d<f32>(-7.00f,0.00f,-7.00f,7.00f,1.00f,7.00f),
core::vector3df(0.00f,0.03f,0.00f),
60,200,
video::SColor(0,255,255,100), video::SColor(0,0,255,0),
100,3400,108);
ps->setEmitter(emb);
emb->drop();
scene::IParticleAffector* paf = ps->createFadeOutParticleAffector(video::SColor(0,0,0,0),1000);
ps->addAffector(paf);
paf->drop();
scene::IParticleAffector* pgaf = ps->createGravityAffector(core::vector3df(0.00f,0.30f,0.00f),2400); ps->addAffector(pgaf);
pgaf->drop();
ILightSceneNode *light = smgr->addLightSceneNode(
0, core::vector3df(0,5000,0),
video::SColorf(1.0f, 1.0f, 1.0f, 1.0f), 30000.0f
);
light->getLightData().AmbientColor = video::SColorf(0.5f, 0.5f, 0.5f, 1.0f);
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
smgr->addSkyBoxSceneNode(
driver->getTexture("skybox/rocky_up.jpg"),
driver->getTexture("skybox/rocky_dn.jpg"),
driver->getTexture("skybox/rocky_lf.jpg"),
driver->getTexture("skybox/rocky_rt.jpg"),
driver->getTexture("skybox/rocky_ft.jpg"),
driver->getTexture("skybox/rocky_bk.jpg"));
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
int lastFPS = -1;
while(device->run())
{
driver->beginScene(true, true, video::SColor(255,90,90,156));
smgr->drawAll();
driver->endScene();
resolve_controls_WW();
//resolve_controls_RE();
if (MyNode->ScanArea(p1,5000))
{
f32 Mx;
// if enemy far from player start to move forarard to player
if (MyNode->GetDistance(p1) < 500) Mx = 0.1;
// if enamy is near of player stop moveing and start to attack
if (MyNode->GetDistance(p1) < 10) Mx = 0;
MyNode->MoveForward(Mx);
}
core::vector3df p1_movedir = p1_target->getAbsolutePosition() - p1->getPosition();
if(moving) {
p1->setPosition(p1->getPosition()+(p1_movedir*moving));
moving=0;
}
camera->setTarget(p1->getPosition());
f64 distance = camera->getPosition().getDistanceFrom(camera->getTarget()+core::vector3df(-500,HEIGHT,0));
core::vector3df cam_movedir = (camera->getTarget()+core::vector3df(-500,HEIGHT,0))-camera->getPosition();
cam_movedir.normalize();
if(distance>(50.0f+MOVESPEED))
camera->setPosition(camera->getPosition()+(cam_movedir*(MOVESPEED*3)));
else if(distance<50.0f)
camera->setPosition(camera->getPosition()-(cam_movedir*(MOVESPEED)));
if(cam_rotate) {
core::vector3df crossy = (camera->getTarget()-camera->getPosition()).crossProduct(camera->getUpVector());
crossy.normalize();
camera->setPosition(camera->getPosition()-crossy*cam_rotate*6);
cam_rotate=0;
}
}
device->drop();
return 0;
}
bil':)
Oops sorry guys, i have made it
but now i have another problem, it seems that the collision i use
will not make the node to go toward me:( hiks....hiks
somebody please help me again:(
note :
i run this with irrlicht 11.0.
MSVC 2003.
map file my3d, made with Gile[s].
Character .md2
but now i have another problem, it seems that the collision i use
will not make the node to go toward me:( hiks....hiks
somebody please help me again:(
note :
i run this with irrlicht 11.0.
MSVC 2003.
map file my3d, made with Gile[s].
Character .md2
Code: Select all
#include <irrlicht.h>
#include <iostream>
#include <stdio.h>
#include <wchar.h>using namespace irr;
using namespace scene;
using namespace core;
using namespace video;
using namespace io;
using namespace gui;
#pragma comment(lib, "Irrlicht.lib")
enum MStatue
{
scan,
found
};
class TMove
{
bool flag;
vector3df Curforward;
f32 RotSpeed;
ISceneNode* ScnNode;
ISceneManager* SceneMgr;
ITriangleSelector* Selector;
array<triangle3df> Triangles;
array<vector3df> Path;
array<f32> StartAngle;
array<f32> EndAngle;
vector3df CurRot;
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,f32 srtAngl = 0,f32 endAngl = 360)
{
Path.push_back( point );
StartAngle.push_back(srtAngl);
EndAngle.push_back(endAngl);
}
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)
{
startAngl = StartAngle[indx];
endAngl = EndAngle[indx];
if (fctr == 0)
{
if (CurRot.Y < startAngl)ScnNode->setRotation(vector3df(0,startAngl,0));
if (CurRot.Y > endAngl)ScnNode->setRotation(vector3df(0,endAngl,0));
}
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);
}
CurRot = ScnNode->getRotation();
CurRot.Y += RotSpeed;
if (CurRot.Y > 360) CurRot.Y -= 360;
if (CurRot.Y >= startAngl) RotSpeed *= -1;
if (CurRot.Y <= endAngl) RotSpeed *= -1;
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;
}
};
scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;
scene::IAnimatedMeshSceneNode* Model = 0;
#define C_UP 1
#define C_DOWN 2
#define C_LEFT 4
#define C_RIGHT 8
#define C_CAML 16
#define C_CAMR 32
#define MOVESPEED 2.0f // deklarasi kecepatan pergerakan karakter dan kamera
#define ROTSPEED 4.0f // deklarasi kecepatan rotasi kamera
#define HEIGHT 500.0f // deklarasi ketinggian kamera dari karakter
scene::ICameraSceneNode* camera;
scene::IAnimatedMeshSceneNode* p1;
scene::IAnimatedMeshSceneNode* p2;
float cam_rotate=2;
float p1_rotate=0;
float moving=0;
unsigned int controls=0;
bool playerisrunning;
bool playerisstanding;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
{
unsigned int k=0;
switch(event.KeyInput.Key) {
case(KEY_UP):
k=C_UP;
break;
case(KEY_DOWN):
k=C_DOWN;
break;
case(KEY_LEFT):
k=C_LEFT;
break;
case(KEY_RIGHT):
k=C_RIGHT;
break;
case(KEY_KEY_A):
k=C_CAML;
break;
case(KEY_KEY_S):
k=C_CAMR;
break;
}
//if(k) {
// if(event.KeyInput.PressedDown)
// controls |= k;
// else
// controls ^= k;
// return true;
// }
if(k)
{
if(event.KeyInput.PressedDown)
{
controls |= k;
if(!playerisrunning)
{
p1->setFrameLoop(320,360);
playerisrunning = true;
playerisstanding = false;
}
}
else
{
controls ^= k;
//if(!controls & C_UP + C_DOWN +C_LEFT + C_RIGHT)
if(!playerisstanding)
{
//p1->setFrameLoop(0, 79*8);
p1->setMD2Animation(irr::scene::EMAT_STAND);
playerisstanding = true;
playerisrunning =false;
}
}
return true;
}
}
return false;
}
};
/*kelas untuk pergerakan karakter*/
void resolve_controls_RE() {
/*
gaya1: up=forward, down=back, left & right = rotate. susah :( */
if(controls & C_UP + C_DOWN)
moving = controls & C_UP ? MOVESPEED : -MOVESPEED;
if(controls & C_LEFT + C_RIGHT) {
p1_rotate += controls & C_RIGHT ? ROTSPEED : (360.0f-ROTSPEED);
if(p1_rotate>=360.0f)
p1_rotate -= 360.0f;
p1->setRotation(core::vector3df(0,p1_rotate,0));
}
if(controls & C_CAML + C_CAMR)
cam_rotate = controls & C_CAMR ? ROTSPEED : -ROTSPEED;
}
void resolve_controls_WW() {
/*gaya2: lebe susa:(*/
p1_rotate=0;
switch(controls & C_UP+C_DOWN+C_LEFT+C_RIGHT) {
case(C_UP+C_LEFT):
p1_rotate+=45.0f;
case(C_LEFT):
p1_rotate+=45.0f;
case(C_DOWN+C_LEFT):
p1_rotate+=45.0f;
case(C_DOWN):
p1_rotate+=45.0f;
case(C_DOWN+C_RIGHT):
p1_rotate+=45.0f;
case(C_RIGHT):
p1_rotate+=45.0f;
case(C_UP+C_RIGHT):
p1_rotate+=45.0f;
case(C_UP):
core::matrix4 matty;
matty.buildCameraLookAtMatrixLH(camera->getPosition(),camera->getTarget(),camera->getUpVector());
p1_rotate+=matty.getRotationDegrees().Z+270.0f;
while(p1_rotate>=360.0f) {
p1_rotate-=360.0f;
}
float foo = p1->getRotation().Y-p1_rotate;
if(foo<0)
foo=-foo;
if(foo<45.0f || foo>360.0f-45.0f)
moving = MOVESPEED;
if(foo>ROTSPEED || foo>360.0f-ROTSPEED) {
if(foo<180.0f)
if(p1->getRotation().Y>p1_rotate)
p1->setRotation(p1->getRotation() + core::vector3df(0,360.0f-ROTSPEED*2,0));
else
p1->setRotation(p1->getRotation() + core::vector3df(0,ROTSPEED*2,0));
else
if(p1->getRotation().Y<p1_rotate)
p1->setRotation(p1->getRotation() + core::vector3df(0,360.0f-ROTSPEED*2,0));
else
p1->setRotation(p1->getRotation() + core::vector3df(0,ROTSPEED*2,0));
if(p1->getRotation().Y>=360.0f)
p1->setRotation(p1->getRotation() - core::vector3df(0,360.0f,0));
}
}
if(controls & C_CAML + C_CAMR)
cam_rotate = controls & C_CAMR ? ROTSPEED : -ROTSPEED;
}int main()
{MyEventReceiver receiver;
device = createDevice(video::EDT_DIRECTX9, core::dimension2d<s32>(1024, 768),16, false, false);
device->setEventReceiver(&receiver);
if (device == 0)
return 1; // could not create selected driver.
irr::scene::IAnimatedMesh* mesh;
scene::ITriangleSelector* selector = 0;
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
gui::IGUIEnvironment* env = device->getGUIEnvironment(); driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
//driver->setFog(video::SColor(0,175,175,195),false, 0.0f, 2000.0f, 0.001f, false, false);
//driver->setFog(video::SColor(0,175,175,195), true, 250, 1000, 0, false,false);
driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
mesh = smgr->getMesh("data/untitled.my3d");
scene::ISceneNode* test_scene = smgr->addOctTreeSceneNode(mesh->getMesh(0));
test_scene->setPosition( irr::core::vector3df(0,0,0) );
test_scene->setScale( irr::core::vector3df(1,1,1) );
test_scene->setRotation( irr::core::vector3df(0,0,0) );
test_scene->setMaterialFlag(video::EMF_LIGHTING, true);
selector = smgr->createOctTreeTriangleSelector(mesh->getMesh(0),test_scene, 128);
test_scene->setTriangleSelector(selector);
selector->drop();
//test_scene->setMaterialFlag(EMF_FOG_ENABLE,true); //enables fog
driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
scene::IAnimatedMesh* bilenv = smgr->getMesh("data/untitled.my3d");
scene::ISceneNode* envi = 0;
if (bilenv)
envi = smgr->addOctTreeSceneNode(bilenv->getMesh(0));
// scene::ITriangleSelector* selector = 0;
if (envi)
{ envi->setMaterialFlag(video::EMF_LIGHTING, true);
envi->setPosition(core::vector3df(0,0,0));
selector = smgr->createOctTreeTriangleSelector(
bilenv->getMesh(0), envi, 128);
envi->setTriangleSelector(selector);
selector->drop();
}*/
//coding pohon?
//scene::ISceneNode* node = 0;
//scene::ISceneNodeAnimator* collider;
//node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("data/temple01.my3d"));
//node->setMaterialTexture( 0, driver->getTexture("3ds/plant1.bmp") );
//node->setMaterialFlag(EMF_LIGHTING, false);
//node->setVisible(true);
//node->setPosition(core::vector3df(900,600,1100));
/*koding for kase ilang mouse cursor, perlu nda ??*/
device->getCursorControl()->setVisible(false);
//video::ITexture* lightTexture=device->getVideoDriver()->getTexture("terrain/terr_light.jpg");
/*kode untuk environment, no good lia ulang!!*/
// add camerascene::
/*kode untuk karakter-masih perlu dipikirkan kembali??*/
mesh = smgr->getMesh("karakter/Chastity.md2");
p1 = smgr->addAnimatedMeshSceneNode( mesh );
p1->setScale(core::vector3df(3,3,3));
p1->setPosition(core::vector3df(100,0,100));//590
p1->setRotation(core::vector3df(0,-90,0));
//p1->setMaterialFlag(video::EMF_FOG_ENABLE,true);
scene::ISceneNode* p1_target = smgr->addEmptySceneNode(p1);
p1_target->setPosition(core::vector3df(1,0,0));
p1->setMaterialFlag(video::EMF_LIGHTING, false);
p1->setMD2Animation(irr::scene::EMAT_STAND);
//p1->setFrameLoop(320, 360); //(320,360)
p1->setAnimationSpeed(30);
p1->setMaterialTexture(0, driver->getTexture("karakter/Chastity1.pcx"));
/*kode untuk karakter collision, mar masih perlu dipikirkan lagi ???
core::vector3df dimensions = mesh->getBoundingBox().MaxEdge - mesh->getBoundingBox().MinEdge;
//scene::ISceneNodeAnimator* collision = smgr->createCollisionResponseAnimator(selector,p1,dimensions);
//p1->addAnimator(collision);
// create collision response animator and attach it to the camera
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
selector, p1, dimensions,
core::vector3df(0,-100,0),
core::vector3df(0,50,0));
p1->addAnimator(anim);
anim->drop();*/
mesh = smgr->getMesh("karakter/Chastity.md2");
p2 = smgr->addAnimatedMeshSceneNode( mesh );
p2->setScale(core::vector3df(3,3,3));
p2->setPosition(core::vector3df(50,590,100));
p2->setRotation(core::vector3df(0,-90,0));
p2->setMaterialFlag(video::EMF_LIGHTING, false);
p2->setMD2Animation(irr::scene::EMAT_STAND);
p2->setAnimationSpeed(30);
p2->setMaterialTexture(0, driver->getTexture("karakter/Chastity1.pcx"));
/*kode untuk karakter collision, mar masih perlu dipikirkan lagi ???*/
/*core::vector3df dimensions2 = mesh->getBoundingBox().MaxEdge - mesh->getBoundingBox().MinEdge;
//scene::ISceneNodeAnimator* collision = smgr->createCollisionResponseAnimator(selector,p1,dimensions);
//p1->addAnimator(collision);
// create collision response animator and attach it to the camera
scene::ISceneNodeAnimator* anim2 = smgr->createCollisionResponseAnimator(
selector, p2, dimensions2,
core::vector3df(0,-100,0),
core::vector3df(0,50,0));
p2->addAnimator(anim2);
anim2->drop();*/
TMove* MyNode = new TMove(p2,smgr);
//here our enemy will no seeing through the room walls
MyNode->setTriangleSelector(test_scene->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);
/*kode untuk kamera collision*/
camera = smgr->addCameraSceneNode(0, core::vector3df(710,1000,1000), core::vector3df(0,0,150));
/*kode untuk kamera collision
scene::ISceneNodeAnimator* collision;
collision = smgr->createCollisionResponseAnimator(
selector,camera,core::vector3df(20,20,20),
core::vector3df(0,0,0),
core::vector3df(0,0,0));
camera->addAnimator(collision);
collision->drop();
*/
//SNOW
//try snow
/*
scene::IParticleSystemSceneNode* ps = 0;
ps = smgr->addParticleSystemSceneNode(false);
ps->setPosition(core::vector3df(800,590,1000));
ps->setScale(core::vector3df(2,2,2));
ps->setParticleSize(core::dimension2d<f32>(5.0f, 5.0f));
scene::IParticleEmitter* em = ps->createBoxEmitter(
core::aabbox3d<f32>(-270,-200,-270,270,300,300),
core::vector3df(0.01f,-0.02f,0.0f),
400,800,
video::SColor(0,255,255,255), video::SColor(0,255,255,255),
1800,3000);
ps->setEmitter(em);
em->drop();
scene::IParticleAffector* paf =
ps->createGravityAffector(core::vector3df(-0.05f,-0.03f, 0.0f), 2000);
ps->addAffector(paf);
paf->drop();
ps->setMaterialFlag(video::EMF_LIGHTING, false);
ps->setMaterialTexture(0, driver->getTexture("snowparticle.bmp"));
ps->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);
*/
//-----------------------------------------------------
//particle system
//-----------------------------------------------------
scene::IParticleSystemSceneNode* ps;
ps = smgr->addParticleSystemSceneNode(false);
ps->setMaterialFlag(video::EMF_LIGHTING, false);
ps->setPosition(core::vector3df(-654,0,0));
ps->setScale(core::vector3df(5,5,5));
ps->setParticleSize(core::dimension2d<f32>(20,20));
ps->setMaterialTexture(0, driver->getTexture("./sprites/particle_white.bmp"));
ps->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);
scene::IParticleEmitter* emb = ps->createBoxEmitter(
core::aabbox3d<f32>(-7.00f,0.00f,-7.00f,7.00f,1.00f,7.00f),
core::vector3df(0.00f,0.03f,0.00f),
60,200,
video::SColor(0,255,255,100), video::SColor(0,0,255,0),
100,3400,108);
ps->setEmitter(emb);
emb->drop();
scene::IParticleAffector* paf = ps->createFadeOutParticleAffector(video::SColor(0,0,0,0),1000);
ps->addAffector(paf);
paf->drop();
scene::IParticleAffector* pgaf = ps->createGravityAffector(core::vector3df(0.00f,0.30f,0.00f),2400); ps->addAffector(pgaf);
pgaf->drop();
//-----------------------------------------------------------------------------------------------//
// create sun-light
//-----------------------------------------------------
ILightSceneNode *light = smgr->addLightSceneNode(
0, core::vector3df(0,5000,0),
video::SColorf(1.0f, 1.0f, 1.0f, 1.0f), 30000.0f
);
light->getLightData().AmbientColor = video::SColorf(0.5f, 0.5f, 0.5f, 1.0f);
// attach billboard to light
/*
IBillboardSceneNode* bill = smgr->addBillboardSceneNode(
light, core::dimension2d<f32>(30000, 30000)
);
bill->setMaterialFlag(video::EMF_LIGHTING, false);
bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
bill->setMaterialTexture(0, driver->getTexture("sprites/particle_white.bmp"));
*/
/*kode untuk skybox*/
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
smgr->addSkyBoxSceneNode(
driver->getTexture("skybox/rocky_up.jpg"),
driver->getTexture("skybox/rocky_dn.jpg"),
driver->getTexture("skybox/rocky_lf.jpg"),
driver->getTexture("skybox/rocky_rt.jpg"),
driver->getTexture("skybox/rocky_ft.jpg"),
driver->getTexture("skybox/rocky_bk.jpg"));
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
int lastFPS = -1;
while(device->run())
{
driver->beginScene(true, true, video::SColor(255,90,90,156));
smgr->drawAll();
driver->endScene();
/*kode pemanggilan kelas untuk pengontrolan karakter*/
resolve_controls_WW();
//resolve_controls_RE();
/*kode untuk menentukan/mengetahui lokasi karakter*/
core::vector3df p1_movedir = p1_target->getAbsolutePosition() - p1->getPosition();
if(moving) {
p1->setPosition(p1->getPosition()+(p1_movedir*moving));
moving=0;
}
/*kode untuk posisi kamera dari karakter*/
camera->setTarget(p1->getPosition());
f64 distance = camera->getPosition().getDistanceFrom(camera->getTarget()+core::vector3df(-500,HEIGHT,0));
core::vector3df cam_movedir = (camera->getTarget()+core::vector3df(-500,HEIGHT,0))-camera->getPosition();
cam_movedir.normalize();
/*kode untuk kecepatan kamera follow*/
if(distance>(50.0f+MOVESPEED))
camera->setPosition(camera->getPosition()+(cam_movedir*(MOVESPEED*2)));
else if(distance<50.0f)
camera->setPosition(camera->getPosition()-(cam_movedir*(MOVESPEED)));
/*kode untuk putaran kamera*/
if(cam_rotate) {
core::vector3df crossy = (camera->getTarget()-camera->getPosition()).crossProduct(camera->getUpVector());
crossy.normalize();
camera->setPosition(camera->getPosition()-crossy*cam_rotate*6);
cam_rotate=0;
}
if (MyNode->ScanArea(p1,500))
{
f32 Mx; Mx=0;
// if enemy far from player start to move forarard to player
if (MyNode->GetDistance(p1) < 500) Mx = 0.1;
// if enamy is near of player stop moveing and start to attack
if (MyNode->GetDistance(p1) < 10) Mx = 0;
MyNode->MoveForward(Mx);
}
}
device->drop();
return 0;
}
bil':)
Generally you don't post more then a few lines you think are causing the problem or at most a single file or sections of code.
About like you have there is the max you should ever post at one time.
If you solve your problem post the answer to it so others don't fall down the same hole.
If you solve your problem either deleting unnecessary posts or editing them would be very tidy. editing it with the corrections would be wonderful and stating as such.
If you do need to include massive amounts of code you can host them on some free web space or commercial if you have some most ISP's provide you with 10-20mb of space. look into it call your isp and ask them if you must. some even offer as much as 100mb or more.
If it seems to be a very common problem you should make a new thread so others can locate information about there problems more easily.
Generally you want to keep your posts as small as possible but what you have here is still acceptable. I suggest you edit the 2nd post with the corrections you made.
I'm not familiar with your Issue otherwise I would try to be more helpful.
Good Luck!
About like you have there is the max you should ever post at one time.
If you solve your problem post the answer to it so others don't fall down the same hole.
If you solve your problem either deleting unnecessary posts or editing them would be very tidy. editing it with the corrections would be wonderful and stating as such.
If you do need to include massive amounts of code you can host them on some free web space or commercial if you have some most ISP's provide you with 10-20mb of space. look into it call your isp and ask them if you must. some even offer as much as 100mb or more.
If it seems to be a very common problem you should make a new thread so others can locate information about there problems more easily.
Generally you want to keep your posts as small as possible but what you have here is still acceptable. I suggest you edit the 2nd post with the corrections you made.
I'm not familiar with your Issue otherwise I would try to be more helpful.
Good Luck!
-
- Posts: 518
- Joined: Tue Mar 29, 2005 9:02 pm
- Location: Alex,Egypt
- Contact:
Hi b1_77y
sorry for not answer you till now.
i think that the collosion system of Irrlicht always apply to the enemy node
after my class handle it.but i do not know about irrlicht collosion,may Nike
enlighten us in this point.
to be more clear i will explain more detail.
if you look at the code you will noticed that ScanArea will execute firest
then sceneMgr->DrawALL come,in scanarea the enamy will move in it's
path then in DrawAll function enamy will move according to collision of
irrlicht which override Scanarea movement and this is the problem.
sorry for not answer you till now.
i think that the collosion system of Irrlicht always apply to the enemy node
after my class handle it.but i do not know about irrlicht collosion,may Nike
enlighten us in this point.
to be more clear i will explain more detail.
if you look at the code you will noticed that ScanArea will execute firest
then sceneMgr->DrawALL come,in scanarea the enamy will move in it's
path then in DrawAll function enamy will move according to collision of
irrlicht which override Scanarea movement and this is the problem.
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/
well, I was thinking about the AI movement too; and I tried to apply this colision handling principe: the enemy will walk always the line enemy-you what will always be possible 'couse you must see him to activate (the enemy node is droven, this event can be detected with irrlicht) his behaviour and therefore exists a line between you and him; but the player can change his location of course and head behind some coner or edge, causing the enemy stop. See, the AI is working only if he is droven so can pass a line between player and himself without colision being possible.... another thing that should be checked is if the enemy will pass a deadly chasm while reaching you , ----I also dissabled tha activation of enemy behavior, if the angle of his sight with player node is konvex (u see his back)....I got quite cute enemy behavior with this 2 simple things, wouldn't have said when I was applying it