Hello, I wanna how can i make character going up stairs.
If you know, let me know plz.
Code: Select all
enum
{
ID_IsNotPickable = 0,
IDFlag_IsPickable = 1 << 0,
IDFlag_IsHighlightable = 1 << 1
};
scene::IAnimatedMeshSceneNode* node;
scene::ICameraSceneNode* camera;
class MyEventReceiver : public irr::IEventReceiver
{
public:
int State;
int Roll;
MyEventReceiver ()
{
State = Roll = 0;
}
~MyEventReceiver() {}
virtual bool OnEvent(const irr::SEvent& event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
{
switch(event.KeyInput.Key)
{
case irr::KEY_KEY_W:
case irr::KEY_KEY_S:
if (event.KeyInput.PressedDown)
{
if (event.KeyInput.Key == irr::KEY_KEY_S)
{
State = -1;
}
else
State = 1;
}
else
State = 0;
break;
case irr::KEY_KEY_A:
case irr::KEY_KEY_D:
if (event.KeyInput.PressedDown)
{
if (event.KeyInput.Key == irr::KEY_KEY_A)
Roll = -1;
else
Roll = 1;
}
else
Roll = 0;
break;
default:
break;
}
}
return true;
}
};
int main()
{
IrrlichtDevice *device =
createDevice(irr::video::EDT_OPENGL, core::dimension2d<u32>(640, 480), 16, false);
if (device == 0)
return 1; // could not create selected driver.
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
irr::gui::IGUIEnvironment *pGuiEnv = device->getGUIEnvironment();
device->getFileSystem()->addZipFileArchive("../../media/map-20kdm2.pk3");
scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
MyEventReceiver Receiver;
scene::IMeshSceneNode* q3node = 0;
node = 0;
irr::scene::ISceneNode* TargetGirlnode;
irr::gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();
device->setEventReceiver(& Receiver);
if (q3levelmesh)
q3node = smgr->addOctreeSceneNode(q3levelmesh->getMesh(0), 0, IDFlag_IsPickable);
if (q3node)
{
q3node->setPosition(core::vector3df(-1350,-130,-1400));
q3node->setPosition(core::vector3df(-2000,-300,-1000));
}
camera = smgr->addCameraSceneNodeFPS(0, 100.0f, .3f, ID_IsNotPickable, 0, 0, true, 3.f);
camera->setPosition(core::vector3df(0,70,-200));
device->getCursorControl()->setVisible(false);
node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/faerie.md2"),
0, IDFlag_IsPickable | IDFlag_IsHighlightable);
node->setPosition(core::vector3df(50,-30,50)); // Put its feet on the floor.
node->setScale(core::vector3df(1,1,1)); // Make it appear realistically scaled
node->setMD2Animation(scene::EMAT_RUN);
node->setAnimationSpeed(30.f);
video::SMaterial material;
material.setTexture(0, driver->getTexture("../../media/faerie2.bmp"));
material.Lighting = true;
material.NormalizeNormals = true;
node->getMaterial(0) = material;
TargetGirlnode = smgr->addEmptySceneNode(node);
TargetGirlnode->setPosition(irr::core::vector3df(1,0,0));
scene::IMetaTriangleSelector* SumSelector = smgr->createMetaTriangleSelector();
scene::ITriangleSelector* MapSelector = smgr->createOctreeTriangleSelector(q3node->getMesh(), q3node, 128);
q3node->setTriangleSelector(MapSelector);
SumSelector->addTriangleSelector(MapSelector);
if (SumSelector)
{
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
SumSelector, node, core::vector3df(10,10,10),
core::vector3df(0,-5,0),
core::vector3df(0,15,0));
node->addAnimator(anim);
anim->drop();
MapSelector->drop();
SumSelector->drop();
}
material.setTexture(0, 0);
material.Lighting = false;
scene::ILightSceneNode * light = smgr->addLightSceneNode(0, core::vector3df(-60,100,400),
video::SColorf(1.0f,1.0f,1.0f,1.0f), 600.0f);
light->setID(ID_IsNotPickable); // Make it an invalid target for selection.
scene::ISceneNode* highlightedSceneNode = 0;
scene::ISceneCollisionManager* collMan = smgr->getSceneCollisionManager();
int lastFPS = -1;
material.Wireframe=true;
int lastState = Receiver.State;
irr::core::vector3df TempV3df;
irr::scene::ISceneNode* TempCamera = smgr->addEmptySceneNode(node); //캐릭터 노드에 빈공간에 붙인다.
TempCamera->setPosition(irr::core::vector3df(-50 , 30 ,0));
irr::gui::IGUIStaticText *pstextFPS =
pGuiEnv->addStaticText(L"Frame rate",irr::core::rect<irr::s32>(0,0,100,20),true,true,0,100,true);
irr::u32 uLastTick = device->getTimer()->getTime();
irr::u32 CurrentTick = device->getTimer()->getTime();
irr::u32 fDelta = 0 ;
int sum= 0;
while(device->run())
{
CurrentTick = device->getTimer()->getTime();
fDelta = (int) (CurrentTick - uLastTick) ;
if( fDelta >= 3 ) // 0.3
{
uLastTick = device->getTimer()->getTime();
fDelta = 0;
sum = 0;
if (Receiver.Roll)
{
TempV3df = node->getRotation();
TempV3df.Y += 0.5f*Receiver.Roll;
node->setRotation(TempV3df);
}
if (Receiver.State)
{
TempV3df = TargetGirlnode->getAbsolutePosition() - node->getPosition();
node->setPosition(node->getPosition() + TempV3df * Receiver.State * 0.3f);
}
}
if (device->isWindowActive())
{
driver->beginScene(true, true, 0);
//============================================================//
if (lastState != Receiver.State)
{
if (Receiver.State)
node->setMD2Animation(irr::scene::EMAT_RUN);
else
node->setMD2Animation(irr::scene::EMAT_STAND);
}
lastState = Receiver.State;
camera->setPosition (TempCamera->getAbsolutePosition ());
camera->setTarget(node->getPosition());
//============================================================//
smgr->drawAll();
if (highlightedSceneNode)
{
highlightedSceneNode->setMaterialFlag(video::EMF_LIGHTING, true);
highlightedSceneNode = 0;
}
core::line3d<f32> ray;
ray.start = camera->getPosition();
ray.end = ray.start + (camera->getTarget() - ray.start).normalize() * 1000.0f;
core::vector3df intersection;
core::triangle3df hitTriangle;
char wszbuf[256];
sprintf(wszbuf,"Frame rate : %d", sum);
font->draw(wszbuf, core::rect<s32>(0,0,10,10),video::SColor(255,255,255,255));
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw str = L"Collision detection example - Irrlicht Engine [";
str += driver->getName();
str += "] FPS:";
str += fps;
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
}
device->drop();
return 0;
}