`
fxrz12
  • 浏览: 25199 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

人物行走碰撞测试码

 
阅读更多

使用的IDE:CodeBlocks 10.05
Irrlicht版本:1.8
操作系统:win7

 

项目结构:
Irrlicht test world-player-war
---Sources
   --src
     ----Faerie.cpp
     ----World.cpp
     ----main.cpp
----Headers
   --include
     ----EventReceiver.h
     ----Faerie.h
     ----World.h

 

#include "../include/World.h"
#include "../include/Faerie.h"
class World world;
class Faerie faerie1;
class Faerie faerie2;
int main(int argc, char* argv[])
{
    cEventReceiver eventReceiver;
    // 初始化世界
    world.Init();

    // 准备地图等场景
    world.Prepare();

    // 初始化要加载的精灵
    faerie1.Initialize(world.device,world.mapSelector,1);
    faerie2.Initialize(world.device,world.mapSelector,2);
    // 传入事务接收器
    world.device->setEventReceiver(&eventReceiver);

    // 主程序执行启动
    while(world.device->run() && (eventReceiver.Keys[KEY_ESCAPE] == false))
    {
        world.MainLoop(faerie1.faerieNode,&eventReceiver);
        faerie1.OnAnima(&eventReceiver);
    }
    world.Finish();

    return 0;
}

 

#ifndef WORLD_H
#include <irrlicht.h>
#include <sys/time.h>
#include <iostream>
#define WORLD_H


using namespace irr;
#include "EventReceiver.h"
using namespace core;
using namespace scene;
using namespace video;
using namespace io;

enum
{
	// I use this ISceneNode ID to indicate a scene node that is
	// not pickable by getSceneNodeAndCollisionPointFromRay()
	ID_IsNotPickable = 0,

	// I use this flag in ISceneNode IDs to indicate that the
	// scene node can be picked by ray selection.
	IDFlag_IsPickable = 1 << 0,
	//IDFlag_IsPickable = 0,

	// I use this flag in ISceneNode IDs to indicate that the
	// scene node can be highlighted.  In this example, the
	// homonids can be highlighted, but the level mesh can't.
	IDFlag_IsHighlightable = 1 << 1
	//IDFlag_IsHighlightable = 1
};

class World
{
public:
    World();
    virtual ~World();

     // 创建鼠标接收结构
    struct SMouseState
    {
        bool LeftButtonDown;
        SMouseState() : LeftButtonDown(false) { }
    } MouseState;

    const SMouseState & GetMouseState(void) const
    {
        return MouseState;
    }

    struct SParticleImpact
	{
		u32 when;
		core::vector3df pos;
		core::vector3df outVector;
	};
    //游戏主函数
    void Init();
	void Prepare();
	void MainLoop(IAnimatedMeshSceneNode* mainNode,cEventReceiver* eventReceiver);
	void Render();
	void Finish();

	// Irrlicht  vars
	IVideoDriver* driver;
	ISceneManager* smgr;
	//IGUIEnvironment* guienv;
	IrrlichtDevice *device;
	gui::IGUIEnvironment* gui;

    scene::ICameraSceneNode* fps_camera;
    scene::IParticleSystemSceneNode* ps;
    scene::ITriangleSelector* mapSelector;
    scene::ITriangleSelector* cubeSelector;
    scene::IParticleSystemSceneNode* campFire;
    scene::ITerrainSceneNode* terrain;
    scene::ISceneCollisionManager* collMan;
    scene::ISceneNode * selectedSceneNode;
    scene::IBillboardSceneNode * bill;

    scene::IParticleEmitter* em ;

    core::array<SParticleImpact> Impacts;
    core::stringw str;
protected:
private:
    void Play();
    void Shoot(core::vector3df start);
    void CreateParticleImpacts();
    bool hasEmitter;


    f32 speed;//3.f
    f32 camera_round;


    vector3df pFaerie;
    vector3df pCamera;
    core::line3d<f32> ray;
    core::vector3df intersection;

};

#endif // WORLD_H

  

#include "../include/World.h"
#include "../include/EventReceiver.h"

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;

World::World():
    hasEmitter(true)
{
    //ctor
}

World::~World()
{
    //dtor
}

//初始化world
void World::Init()
{
    //初始化参数
    camera_round = -28.f;

    core::dimension2d<u32> resolution ( 800, 600 );
    device = createDevice(EDT_OPENGL,
                          resolution, 32, false, true, false);
    device->setWindowCaption(L"World!");

    driver = device->getVideoDriver();
    smgr = device->getSceneManager();
    collMan = smgr->getSceneCollisionManager();
    gui = device->getGUIEnvironment();
    //生成摄像机
    fps_camera = smgr->addCameraSceneNodeFPS();
    fps_camera->setScale(vector3df(0.1f,0.1f,0.1f));
    fps_camera->setPosition(vector3df(5210.f,1000.f,3110.f));
    fps_camera->setRotation(vector3df(0.f,180.f,0.f));
    device->getCursorControl()->setVisible(false);

}
//准备
void World::Prepare()
{
    //加载地图
    IAnimatedMesh* mesh = smgr->getMesh("../../media/room.3ds");

    smgr->getMeshManipulator()->makePlanarTextureMapping(mesh->getMesh(0), 0.004f);
    ISceneNode* node = 0;

    scene::IAnimatedMeshSceneNode* animModel = smgr->addAnimatedMeshSceneNode(mesh);
    node = animModel;
    node->setDebugDataVisible((scene::E_DEBUG_SCENE_TYPE)(node->isDebugDataVisible()^scene::EDS_MESH_WIRE_OVERLAY));
    node->setMaterialFlag(EMF_LIGHTING, false);
    //创建选择器,附加到场景上
    mapSelector = smgr->createOctreeTriangleSelector(mesh,node);
    node->setTriangleSelector(mapSelector);

    node->setMaterialTexture(0, driver->getTexture("../../media/wall.jpg"));
    node->getMaterial(0).SpecularColor.set(0,0,0,0);
    node->setMaterialFlag(EMF_FOG_ENABLE, false);

    mesh = smgr->addHillPlaneMesh( "waterMesh",
                                   dimension2d<f32>(30,30),
                                   dimension2d<u32>(30,30), 0, 0,
                                   dimension2d<f32>(0,0),
                                   dimension2d<f32>(10,10));


    node = smgr->addWaterSurfaceSceneNode(mesh->getMesh(0), 3.0f, 300.0f, 30.0f);
    node->setPosition(vector3df(0,9,0));

    node->setMaterialTexture(0, driver->getTexture("../../media/stones.jpg"));
    node->setMaterialTexture(1, driver->getTexture("../../media/water.jpg"));

    node->setMaterialType(EMT_REFLECTION_2_LAYER);
    //node->setMaterialFlag(EMF_FOG_ENABLE, true);
    node->setDebugDataVisible(scene::EDS_OFF);
    node->setMaterialFlag(EMF_LIGHTING, false);
    //ISceneNode* terrain = smgr->addMeshSceneNode(smgr->getMesh("../../media/Irr2013123.irr"));
    //加载场景
    IMesh* cubeMesh = smgr->getMesh("../../media/data/smallcube.3ds");
    ISceneNode* cubeNode = smgr->addMeshSceneNode(cubeMesh);
    cubeNode->setMaterialTexture(0, driver->getTexture("../../media/data/crate.jpg"));
    cubeNode->setPosition(vector3df(140,100,40));
    //cubeNode->setScale(vector3df(3,3,2));
    cubeNode->setMaterialFlag(video::EMF_LIGHTING, false);
    cubeNode->setName("cube");
    smgr->addTextSceneNode(gui->getBuiltInFont(),
                           L"Cube-1",
                           video::SColor(100,255,255,255),	cubeNode);

    // Add the billboard.
    bill = smgr->addBillboardSceneNode();
    bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );
    bill->setMaterialTexture(0, driver->getTexture("../../media/fireball.bmp"));
//	bill->setMaterialTexture();
    bill->setMaterialFlag(video::EMF_LIGHTING, false);
    bill->setMaterialFlag(video::EMF_ZBUFFER, false);
    bill->setSize(core::dimension2d<f32>(20.0f, 20.0f));
    bill->setID(ID_IsNotPickable); // This ensures that we don't accidentally ray-pick it
    //cubeNode->;
    //调试物理碰撞检测网格
    //cubeNode->setDebugDataVisible((scene::E_DEBUG_SCENE_TYPE)(cubeNode->isDebugDataVisible()^scene::EDS_MESH_WIRE_OVERLAY));

    cubeSelector = 0;
    cubeSelector = smgr->createTriangleSelector(cubeMesh,cubeNode);
    cubeNode->setTriangleSelector(cubeSelector);
    //selector->drop(); // We're done with this selector, so drop it now.


    // some lights
    /*video::SLight l;
    scene::ILightSceneNode *light = smgr->addLightSceneNode(gamescene,core::vector3df(50,50,50),video::SColorf(1,1,1,1.f),1000);
    l = light->getLightData();
    l.Type = video::ELT_POINT;
    l.AmbientColor = video::SColorf(.35,.35,.35);
    l.SpecularColor = video::SColorf(.7,.7,.5);
    l.DiffuseColor = video::SColorf(1,1,1);
    l.CastShadows = true;

    light->setLightData( l );*/


    //node = smgr->addOctreeSceneNode(mesh,0,0);
    //node->setDebugDataVisible((scene::E_DEBUG_SCENE_TYPE)(node->isDebugDataVisible()^scene::EDS_MESH_WIRE_OVERLAY));
    //创建地图选择器,附加到地图场景上


    //创建精灵碰撞响应动画
    scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
                                          mapSelector, cubeNode, cubeNode->getBoundingBox().MaxEdge,
                                          core::vector3df(0,-10,0),
                                          core::vector3df(0,0,0));

    cubeNode->addAnimator(anim);
    anim->drop();
    anim = 0;

    //创建粒子节点
    campFire = smgr->addParticleSystemSceneNode(false);
    campFire->setPosition(core::vector3df(100,120,600));
    campFire->setScale(core::vector3df(2,2,2));
    //创建粒子发生器(盒子)
    em = campFire->createBoxEmitter(
             core::aabbox3d<f32>(-7,0,-7,7,1,7),// emitter size
             core::vector3df(0.0f,0.06f,0.0f),  // initial direction
             80,100,                            // min and max emit rate
             video::SColor(1,255,255,255),      // darkest color
             video::SColor(1,255,255,255),      // brightest color
             800,2000,10,					// min and max age, angle
             dimension2df(10.f,10.f),		// min size
             dimension2df(20.f,20.f)        // max size
         );

    em->setMinStartSize(core::dimension2d<f32>(20.0f, 10.0f));
    em->setMaxStartSize(core::dimension2d<f32>(20.0f, 10.0f));
    campFire->setEmitter(em);
    em->drop();
    em = 0;
    //附加粒子效果
    scene::IParticleAffector* paf = campFire->createFadeOutParticleAffector();
    campFire->addAffector(paf);
    paf->drop();
    //设定粒子相关属性
    campFire->setMaterialFlag(video::EMF_LIGHTING, false);
    campFire->setMaterialFlag(video::EMF_ZWRITE_ENABLE, false);
    campFire->setMaterialTexture(0, driver->getTexture("../../media/fireball.bmp"));
    campFire->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);


}
//主循环
void World::MainLoop(IAnimatedMeshSceneNode* mainNode,cEventReceiver* eventReceiver)
{

    pCamera= vector3df(mainNode->getPosition().X+sin(PI*fps_camera->getRotation().Y/180)*camera_round,
                       mainNode->getPosition().Y+20.f,
                       mainNode->getPosition().Z+cos(PI*fps_camera->getRotation().Y/180)*camera_round);
    fps_camera->setPosition(pCamera);

    str = L"Irrlicht Engine - Walker example [";
        str += driver->getName();
        str += "] ";
        str += "P:";
        str += fps_camera->getRotation().X;
        str += ",";
        str += fps_camera->getRotation().Y;
        str += ",";
        str += fps_camera->getRotation().Z;
        str += "|";
        str += mainNode->getPosition().X;
        str += ",";
        str += mainNode->getPosition().Y;
        str += ",";
        str += mainNode->getPosition().Z;
        device->setWindowCaption(str.c_str());
    /*if(fps_camera->getRotation().X<=6)
    {
        fps_camera->setRotation(vector3df(6,fps_camera->getRotation().Y,0));
    }
    if(72<=fps_camera->getRotation().X)
    {
        fps_camera->setRotation(vector3df(72,fps_camera->getRotation().Y,0));
    }*/
    ray.start = fps_camera->getPosition();
    ray.end = ray.start +  (fps_camera->getTarget()- ray.start).normalize() * 1000.0f;
    core::triangle3df hitTriangle;
    Play();
    selectedSceneNode = collMan->getSceneNodeAndCollisionPointFromRay(
                            ray,
                            intersection, // This will be the position of the collision
                            hitTriangle, // This will be the triangle hit in the collision
                            IDFlag_IsPickable, // This ensures that only nodes that we have
                            // set up to be pickable are considered
                            0); // Check the entire scene (this is actually the implicit default)
    if(selectedSceneNode)
    {
        bill->setPosition(ray.end);
        bill->setRotation(core::vector3df(fps_camera->getRotation().X,fps_camera->getRotation().Y,fps_camera->getRotation().Z));
        driver->setTransform(video::ETS_WORLD, core::matrix4());
        //driver->setMaterial(material);
        driver->draw3DTriangle(hitTriangle, video::SColor(0,255,0,0));
        //selectedSceneNode->setVisible(false);
        std::cout<<selectedSceneNode->getName()<<std::endl;
        std::cout<<selectedSceneNode->getPosition().X<<","<<selectedSceneNode->getPosition().Y<<","<<selectedSceneNode->getPosition().Z<<std::endl;
        //std::cout<<intersection.X<<","<<intersection.Y<<","<<intersection.Z<<std::endl;
        //std::cout<<hitTriangle.pointA<<","<<hitTriangle.pointB<<","<<hitTriangle.pointC<<std::endl;
        if(strcmp(selectedSceneNode->getName(),"cube")==0)
        {
            //selectedSceneNode->setPosition(mainNode->getPosition());
            std::cout<<"---------------stop-------------"<<std::endl;
            eventReceiver->Move=false;
            //mainNode->setMD2Animation(EMAT_STAND);
        }
        else
        {
            eventReceiver->Move=true;
        }

        if(eventReceiver->GetMouseState().LeftButtonDown)
        {
            std::cout<<"--------------------war-----------------------"<<std::endl;
            selectedSceneNode->setPosition(selectedSceneNode->getPosition()+1);
        }
        else
        {

        }
    }
    CreateParticleImpacts();

    Render();

}
//渲染
void World::Render()
{
    if (device->isWindowActive())
    {

        driver->beginScene(true, true, SColor(255,100,101,140));
        smgr->drawAll();
        driver->endScene();
        /*str = L"Irrlicht Engine - Walker example [";
        str += driver->getName();
        str += "] ";
        str += "P:";
        str += fps_camera->getRotation().X;
        str += ",";
        str += fps_camera->getRotation().Y;
        str += ",";
        str += fps_camera->getRotation().Z;
        device->setWindowCaption(str.c_str());*/
    }
}
//结束
void World::Finish()
{
    device->drop();
}

void World::Play()
{


    //鼠标事件
    if(GetMouseState().LeftButtonDown)
    {
        std::cout<<"click mouse"<<std::endl;
        Shoot(pFaerie);
    }

}

void World::Shoot(core::vector3df start)
{
    scene::ISceneManager* sm = device->getSceneManager();
    scene::ICameraSceneNode* camera = sm->getActiveCamera();

    if (!camera )
        return;

    SParticleImpact imp;
    imp.when = 0;

    // get line of camera

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

    core::triangle3df triangle;

    core::line3d<f32> line(start, end);

    // get intersection point with map
    scene::ISceneNode* hitNode;
    if (sm->getSceneCollisionManager()->getCollisionPoint(
                line, mapSelector, end, triangle, hitNode))
    {
        // collides with wall
        core::vector3df out = triangle.getNormal();
        out.setLength(0.03f);

        imp.when = 1;
        imp.outVector = out;
        imp.pos = end;
    }
    else
    {
        // doesnt collide with wall
        //core::vector3df start = faerieNode->getPosition();
        core::vector3df end = (camera->getTarget() - start);
        end.normalize();
        start += end*8.0f;
        end = start + (end * camera->getFarValue());
    }

    // create fire ball
    scene::ISceneNode* node = 0;
    node = sm->addBillboardSceneNode(0,
                                     core::dimension2d<f32>(25,25), start);

    node->setMaterialFlag(video::EMF_LIGHTING, false);
    node->setMaterialTexture(0, device->getVideoDriver()->getTexture("../../media/fireball.bmp"));
    node->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);

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

    scene::ISceneNodeAnimator* anim = 0;

    // set flight line

    anim = sm->createFlyStraightAnimator(start, end, time);
    node->addAnimator(anim);
    anim->drop();

    anim = sm->createDeleteAnimator(time);
    node->addAnimator(anim);
    anim->drop();

    if (imp.when)
    {
        // create impact note
        imp.when = device->getTimer()->getTime() + (time - 100);
        Impacts.push_back(imp);
    }

}

void World::CreateParticleImpacts()
{
    u32 now = device->getTimer()->getTime();
    scene::ISceneManager* sm = device->getSceneManager();

    for (s32 i=0; i<(s32)Impacts.size(); ++i)
        if (now > Impacts[i].when)
        {
            // create smoke particle system
            scene::IParticleSystemSceneNode* pas = 0;

            pas = sm->addParticleSystemSceneNode(false, 0, -1, Impacts[i].pos);

            pas->setParticleSize(core::dimension2d<f32>(10.0f, 10.0f));

            scene::IParticleEmitter* em = pas->createBoxEmitter(
                                              core::aabbox3d<f32>(-5,-5,-5,5,5,5),
                                              Impacts[i].outVector, 20,40, video::SColor(50,255,255,255),video::SColor(50,255,255,255),
                                              1200,1600, 20);

            pas->setEmitter(em);
            em->drop();

            scene::IParticleAffector* paf = campFire->createFadeOutParticleAffector();
            pas->addAffector(paf);
            paf->drop();

            pas->setMaterialFlag(video::EMF_LIGHTING, false);
            pas->setMaterialFlag(video::EMF_ZWRITE_ENABLE, false);
            pas->setMaterialTexture(0, device->getVideoDriver()->getTexture("../../media/smoke.bmp"));
            pas->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);

            scene::ISceneNodeAnimator* anim = sm->createDeleteAnimator(2000);
            pas->addAnimator(anim);
            anim->drop();

            // delete entry
            Impacts.erase(i);
            i--;
        }
}

 

#ifndef FAERIE_H
#define FAERIE_H
#include <irrlicht.h>
#include "World.h"

using namespace irr;
#include "EventReceiver.h"
using namespace core;
//using namespace scene;
//using namespace video;
//using namespace io;

class Faerie
{
public:
    Faerie();
    void Initialize(IrrlichtDevice *device,scene::ITriangleSelector* mapSelector,s32 n);
    //void setSelector(scene::ITriangleSelector* mapSelector);
    virtual ~Faerie();
    void OnAnima(cEventReceiver* eventReceiver);
    scene::IAnimatedMeshSceneNode* faerieNode;
protected:
private:

    void Move();
    void Turn_face(bool leftOrRight );

    f32 speed;//3.f
    vector3df pFaerie;
    bool turn_back;
    bool status;
    f32 lasttick;
    f32 pX;// 0.f;
    f32 pZ;// 0.f;
    f32 pR;// 0.f;

    IrrlichtDevice *f_device;
};

#endif // FAERIE_H

 

#ifndef CEVENTRECEIVER_H
#define CEVENTRECEIVER_H


class cEventReceiver : public IEventReceiver
{
public:

    cEventReceiver()
    {
        for (int i=0; i<KEY_KEY_CODES_COUNT; i++)
        {
            Keys[i] = false;
            KeysTemp[i] = false;
        }

        mouseX=mouseY=mouseDeltaX=mouseDeltaY=0;
        Move = true;
    }

    // 创建鼠标接收结构
    struct SMouseState
    {
        bool LeftButtonDown;
        SMouseState() : LeftButtonDown(false) { }
    } MouseState;

    const SMouseState & GetMouseState(void) const
    {
        return MouseState;
    }

    ~cEventReceiver() {}

    virtual bool OnEvent(const SEvent& event)
    {
        if (event.EventType==irr::EET_KEY_INPUT_EVENT)
        {
            Keys[event.KeyInput.Key] = false;
            Keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
            return true;
        }

        if (event.EventType==irr::EET_MOUSE_INPUT_EVENT)
        {

            /*mouseX = event.MouseInput.X;
            mouseY = event.MouseInput.Y;
            mouseDeltaX = event.MouseInput.X - mouseX;
            mouseDeltaY = event.MouseInput.Y - mouseY;

            std::cout<<mouseDeltaX<<std::endl;
            return true;*/
        }
        if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
        {
            switch(event.MouseInput.Event)
            {
            case EMIE_LMOUSE_PRESSED_DOWN:
                MouseState.LeftButtonDown = true;

                break;
            case EMIE_LMOUSE_LEFT_UP:
                MouseState.LeftButtonDown = false;

                break;
            case EMIE_RMOUSE_PRESSED_DOWN:

                break;
            case EMIE_MOUSE_WHEEL:
                if(event.MouseInput.Wheel < 0 )
                {

                }
                else if(event.MouseInput.Wheel > 0 )
                {
                }
                break;
            case EMIE_MOUSE_MOVED:
                break;
            default:
                break;
            }
        }
        return false;
    }


    s32 mouseDeltaX;
    s32 mouseDeltaY;

    bool Keys[KEY_KEY_CODES_COUNT];
    bool KeysTemp[KEY_KEY_CODES_COUNT];
    bool Move;

    s32 mouseX;
    s32 mouseY;

private:

};


#endif // EVENTRECEIVER_H

 

#include "../include/Faerie.h"
#include "../include/EventReceiver.h"
#include <irrlicht.h>
Faerie::Faerie()
{
    //ctor
}

Faerie::~Faerie()
{
    //dtor
}

void Faerie::Initialize(IrrlichtDevice *device,scene::ITriangleSelector* mapSelector,s32 n)
{
    //初始化参数
    status = true;
    speed = 3.f;

    if(!device)
        return;
    f_device = device;
    //场景加入精灵
    scene::IAnimatedMesh* mesh;
    ISceneManager* smgr = f_device->getSceneManager();
    mesh = smgr->getMesh("../../media/sydney.md2");
    faerieNode = smgr->addAnimatedMeshSceneNode(mesh);

    smgr->addTextSceneNode(device->getGUIEnvironment()->getBuiltInFont(),
			L"sydney.md2",
			video::SColor(255,255,255,255),	faerieNode);
    //角色坐标设定
    faerieNode->setPosition(vector3df(-40,150,40));
    faerieNode->setScale(vector3df(1,1,1));
    IVideoDriver* driver = f_device->getVideoDriver();
    pFaerie = faerieNode->getPosition();
    faerieNode->setMaterialTexture(0,driver->getTexture("../../media/sydney.BMP"));

    //调试物理碰撞检测网格
    //faerieNode->setDebugDataVisible((scene::E_DEBUG_SCENE_TYPE)(faerieNode->isDebugDataVisible()^scene::EDS_MESH_WIRE_OVERLAY));
    faerieNode->setDebugDataVisible((scene::E_DEBUG_SCENE_TYPE)(faerieNode->isDebugDataVisible()^scene::EDS_BBOX));
    //faerieNode->setAnimationSpeed(13);
    //faerieNode->setMD2Animation(EMAT_STAND);
    //faerieNode->setMD2Animation(EMAT_STAND);
    //faerieNode->setScale(vector3df(4.8f,4.8f,4.8f));
    if(n!=1)
    {
        faerieNode->setName((core::stringw)n+"sydney");
    }

    faerieNode->setMaterialFlag(video::EMF_LIGHTING, false);

    scene::ITriangleSelector* selector = 0;
    selector = smgr->createTriangleSelector(faerieNode);
	faerieNode->setTriangleSelector(selector);
	selector->drop(); // We're done with this selector, so drop it now.

    //创建精灵碰撞响应动画
    scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
                                          mapSelector, faerieNode, faerieNode->getBoundingBox().MaxEdge,
                                          core::vector3df(0,-10,0),
                                          core::vector3df(0,-7,0));

    faerieNode->addAnimator(anim);
    anim->drop();
    anim = 0;


}


void Faerie::OnAnima(cEventReceiver* eventReceiver)
{
    //std::cout<<faerieNode->getPosition().X<<","<<faerieNode->getPosition().Y<<","<<faerieNode->getPosition().Z<<std::endl;
    if(eventReceiver->Keys[KEY_KEY_W])
    {

        if(status)
        {
            status = false;
            faerieNode->setMD2Animation(EMAT_RUN);
        }
        if(eventReceiver->Move)
        {
            Move();
        }


    }
    else
    {
        status = true;
        faerieNode->setMD2Animation(EMAT_STAND);
    }

    if(eventReceiver->Keys[KEY_KEY_A])
    {
        if (f_device->getTimer()->getTime() > lasttick + 10)
        {
            lasttick = f_device->getTimer()->getTime();
            Turn_face(true);
        }

    }

    if(eventReceiver->Keys[KEY_KEY_D])
    {
        if (f_device->getTimer()->getTime() > lasttick + 10)
        {
            lasttick = f_device->getTimer()->getTime();
            Turn_face(false);
        }

    }

    if(eventReceiver->GetMouseState().LeftButtonDown)
    {
        std::cout<<"--------------------war-----------------------"<<std::endl;
        faerieNode->setMD2Animation(EMAT_ATTACK);
    }else
    {

    }
    /*if(eventReceiver->mouseDeltaX>0)
    {
        Turn_face(true);
    }
    else if(eventReceiver->mouseDeltaX<0)
    {
        Turn_face(false);
    }*/


}

void Faerie::Move()
{
    pFaerie = faerieNode->getPosition();
    pX = cos(faerieNode->getRotation().Y*PI/180)*speed;
    pZ = -sin(faerieNode->getRotation().Y*PI/180)*speed;
    faerieNode->setPosition(vector3df(pFaerie.X+pX,faerieNode->getPosition().Y,pFaerie.Z+pZ));

}

//转向
void Faerie::Turn_face(bool leftOrRight)
{
    pR = faerieNode->getRotation().Y;
    if(leftOrRight)
    {
        pR -=6;
        if(pR<0) pR = 360 + pR;
    }
    else
    {
        pR +=6;
        if(pR>360) pR = pR - 360;
    }
    faerieNode->setRotation(vector3df(faerieNode->getRotation().X,
                                      pR,faerieNode->getRotation().Z));

}

 

分享到:
评论

相关推荐

    flex as3 人物行走、详细注释、无废话代码

    本资源针对初学者提供了一个人物行走的实例,它不仅包含完整的代码,还附有详细注释,确保初学者能够理解并运行。以下是这个项目中的关键知识点: 1. **ActionScript 3 (AS3)**: AS3是Flash平台的主要编程语言,...

    VB开发安卓程序-4控制人物行走

    在VB中开发安卓应用后,必须在实际设备或模拟器上进行调试和测试,确保人物行走功能在各种Android设备上都能正常工作。这包括兼容性测试、性能测试和用户体验测试。 7. **资源管理**: 游戏角色的图像资源需要...

    flash cs3人物行走

    在本文中,我们将深入探讨如何使用Adobe Flash CS3创建一个基于鼠标坐标实现人物行走的交互式动画。Flash CS3是一款强大的动画和互动内容创作工具,它允许开发者制作丰富的网络和移动应用程序,包括游戏和动态图形。...

    45度角地图人物行走寻路

    它可能包含了人物行走动画的展示,以及寻路算法的测试用例。开发者可以在这个测试环境中调试和优化寻路逻辑,确保人物在地图上的移动符合预期。 源码分析是理解这一技术的关键,通过查看PlayerTest的源代码,我们...

    基于VC++的RPG行走图

    4. **人物行走逻辑**:为了实现人物的行走,需要编写控制角色移动的函数。这些函数可能涉及到键盘事件处理,根据用户输入的方向(如上、下、左、右)改变角色的位置,并更新对应的行走图帧。同时,还需要考虑边界...

    J2me游戏 重装机兵 源代码

    这款游戏基本实现了必须的所有功能,如碰撞探测、地图转换、NPC对话、事件处理,、横版回合制战斗系统等,并根据原游戏的特色加入了人、车两战斗系统,在玩的过程中,你可以用方向键控制人物行走;回车键弹出游戏菜单...

    unity 9个卡通人物模型

    每个卡通人物模型可能还包括一系列预设的动画,如行走、奔跑、跳跃、攻击等,这些动画可以通过Unity的动画系统进行管理和播放。Unity的Animator组件使得非程序员也能通过直观的界面控制角色的行为,而无需编写复杂的...

    易语言d2d创建人物的步骤教学

    7. **碰撞检测**:利用包围盒技术,编写检测人物与其他物体碰撞的代码。当人物坐标在包围盒内时,触发相应的游戏事件。 8. **测试与优化**:不断测试人物的行为和动画效果,根据反馈进行调整优化,直到达到理想状态...

    Java游戏重装机兵源代码

    这款游戏基本实现了必须的所有功能,如碰撞探测、地图转换、NPC对话、事件处理,、横版回合制战斗系统等,并根据原游戏的特色加入了人、车两战斗系统,在玩的过程中,你可以用方向键控制人物行走;回车键弹出游戏菜单...

    cocos2d恶魔城源代码

    "调试即用"表明这些源代码经过了初步的测试和调试,开发者可以快速地在现有基础上进行二次开发,而无需从零开始。 【知识点详解】 1. **Cocos2d游戏引擎**:Cocos2d是一个跨平台的2D游戏开发框架,支持多种语言...

    actor-collisions.zip文件

    actor-collisions.zip文件很可能包含了与这些动态模型碰撞检测相关的数据或代码,这对于确保仿真中的物理交互真实性和准确性至关重要。 接下来,我们讨论Gazebo 11。Gazebo 11是Gazebo的一个特定版本,发布于2017年...

    UE4 第三人称模型

    6. **碰撞检测**:设置好角色的碰撞组件,确保角色能够正确地与场景中的其他对象交互,例如避免穿模现象。 7. **光照和材质**:调整模型的材质,使其在不同光照环境下看起来更真实。UE4的物理渲染引擎提供了丰富的...

    bullet:JVM Bullet Physics SDK:用于VR,游戏,视觉效果,机器人技术,机器学习等的实时碰撞检测和多物理场仿真

    这对于创建逼真的物理交互环境,比如游戏中的人物碰撞、物体碰撞反馈,或者VR中的手部追踪与环境交互,都是必不可少的。 二、多物理场仿真 Bullet不仅支持基本的刚体动力学,还支持复杂的多物理场仿真。这包括弹性...

    CS 1.6 MDL模型版

    4. **动画制作**:为模型创建不同的动画状态,如人物行走、跑步、射击等,这通常通过在3D软件中制作关键帧,然后导出到SMD文件完成。 5. **编写QC文件**:使用文本编辑器编写QC脚本,定义模型的材质、动画、碰撞盒...

    Android游戏开发系列博文1-19源码免费共享,,童鞋们请评分!

    6. **给群友写的(游戏demo 人物行走)**:这是一个实际的游戏示例,可能包含一个简单的角色行走动画的实现,涉及角色移动逻辑、帧动画以及碰撞检测等。 7. **SQLite**:SQLite是Android内置的轻量级数据库,文件可能...

    OSG漫游系统

    5. **动画**:实现物体的运动动画,如人物行走、物体旋转等。 6. **网络同步**:如果目标是多人在线漫游,还需要处理网络同步和多用户交互。 总的来说,OSG漫游系统为用户提供了定制化3D环境的平台,通过学习和理解...

    【游戏开发】如何让角色动起来

    mhxy_load_people.c可能包含了加载骨骼和权重数据的代码,而mhxy_load_people.exe可能是用于测试或运行动画的可执行程序。 运动学(Kinematics)控制是决定角色如何根据输入指令移动的关键。它分为关节运动学...

    MyCreator3DModel.zip

    它的核心特点是将2D图形通过骨骼结构进行绑定,从而实现复杂的动态效果,如人物行走、战斗动作等。在3D模型换装中,Spine可以用来处理角色的不同装备和服装,实现快速的换装效果,提高游戏的交互性和沉浸感。 在...

    cocos2d-x手机游戏开发:跨iOS、Android和沃Phone平台

    这个案例不仅让读者有机会体验从零开始构建游戏的整个流程,还涵盖了基础篇和高级篇两个层面的内容,包括游戏地图绘制、人物行走、碰撞检测、代码重构和添加更多游戏元素等,这对于理解和掌握游戏开发的完整过程非常...

Global site tag (gtag.js) - Google Analytics