Bonjour. J'ai créé l'animation d'un cube à l'aide de l'algorithme de copland :
-j'utilise le temps écoulé entre deux frames afin de faire un mouvement fluide. Ce mouvement est effectivement fluide.
Seul problème, le cube à un mouvement saccadé ou trop soudain au départ et à chaque fois qui j'appuie sur une flèche directionnelle (j'utilise les évènements pour le déplacer au clavier), et une fois le mouvement lancé, il devient fluide, mais le départ reste gênant.
Quelqu'un aurait-il une solution svp ?
Hors ligne
Sans code, non
Hors ligne
Voici mon fichier principal :
#include "stdafx.h" #include <irrlicht.h> #include <iostream> #include <windows.h> #include <tchar.h> #include <vector> #include <exception> #include <fstream> #include <string> #include "Fonctions.h" using namespace irr; using namespace std; using namespace core; using namespace scene; using namespace video; using namespace io; using namespace gui; using namespace Fonctions; #pragma comment(lib, "Irrlicht.lib") IrrlichtDevice * Device ; ISceneManager * Smgr ; IVideoDriver * Driver ; IGUIEnvironment * Guienv ; IAnimatedMesh * MeshDecor ; ISceneNode * Decor ; IAnimatedMeshSceneNode * Drone ; ICameraSceneNode * CameraExterne ; ICameraSceneNode * CameraInterne ; ICameraSceneNode * Cameras[2] ; bool PremierTour ; int Zoom ; float AngleMax ; class MyEventReceiver : public IEventReceiver { public: virtual bool OnEvent(const SEvent& event) { if (Drone != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT) { switch(event.KeyInput.Key) { case KEY_RIGHT: case KEY_LEFT : { MoveDrone (Device, Drone, 0, 0, (event.KeyInput.Key == KEY_RIGHT) ? -10.0f : 10.0f, (event.KeyInput.Key == KEY_RIGHT) ? -1.0f : 1.0f, 0, 0, CameraExterne, CameraInterne, PremierTour, AngleMax) ; } break ; case KEY_DOWN : case KEY_UP : { MoveDrone (Device, Drone, (event.KeyInput.Key == KEY_UP) ? 10.0f : -10.0f, 0, 0, 0, 0,(event.KeyInput.Key == KEY_UP) ? 1.0f : -1.0f, CameraExterne, CameraInterne, PremierTour, AngleMax) ; } break ; case KEY_NUMPAD8: case KEY_NUMPAD2: { MoveDrone (Device, Drone, 0, 0, 0, 0, (event.KeyInput.Key == KEY_NUMPAD8) ? 1.0f : -1.0f, 0, CameraExterne, CameraInterne, PremierTour, AngleMax) ; } break ; case KEY_NUMPAD4: case KEY_NUMPAD6: { MoveDrone (Device, Drone, 0, (event.KeyInput.Key == KEY_NUMPAD6) ? 10.0f : -10.0f, 0, 0, 0, 0, CameraExterne, CameraInterne, PremierTour, AngleMax) ; } break ; case KEY_ADD : { if (Zoom < 6) { CameraInterne->setFOV ( CameraInterne->getFOV() - 0.174f) ; ++Zoom; } } break ; case KEY_SUBTRACT: { if (Zoom > 0) { CameraInterne->setFOV ( CameraInterne->getFOV() + 0.174f) ; --Zoom; } } break ; case KEY_PRIOR : { if (AngleMax > -180.0f) { AngleMax -= 10.0f ; core::vector3df Target = CameraInterne->getTarget(); core::vector3df PosCam = CameraInterne->getPosition(); float Z = Target.Z - PosCam.Z; float Y = Z * tan (AngleMax/ 180 * 3.141592654f); core::vector3df RotCam = CameraInterne->getRotation(); CameraInterne->setRotation (core::vector3df(RotCam.X - 10.0f, 0.0f, 0.0f)) ; CameraInterne->setTarget (core::vector3df (Target.X, Y, Target.Z)); cout << AngleMax << endl; } } break ; case KEY_NEXT : { if (AngleMax < 180.0f) { AngleMax += 10.0f ; core::vector3df RotCam = CameraInterne->getRotation(); CameraInterne->setRotation (core::vector3df(RotCam.X + 10.0f, 0.0f, 0.0f)) ; cout << AngleMax << endl; } } break ; case KEY_KEY_A : { core::vector3df posix = Drone->getRotation(); cout << posix.X << " " << posix.Y << " " << posix.Z << endl; } } if(event.KeyInput.PressedDown) PremierTour = false ; else { PremierTour = true ; core::vector3df posDrone = Drone->getRotation(); Drone->setRotation (core::vector3df(0, posDrone.Y, 0)); CameraInterne->setRotation(Drone->getRotation()); } return true; } return false; } }; int main (void) { VInt_t VI = Resolution (); MyEventReceiver Receiver; Device = createDevice( video::EDT_OPENGL, dimension2d<s32>(VI[0],VI[1]), 32, false, false, false, &Receiver); Smgr = Device->getSceneManager() ; Driver = Device->getVideoDriver(); Guienv = Device->getGUIEnvironment(); Driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false); ISceneNode * Box= Smgr->addSkyBoxSceneNode( Driver->getTexture("../../../media/irrlicht2_up.jpg"), Driver->getTexture("../../../media/irrlicht2_dn.jpg"), Driver->getTexture("../../../media/irrlicht2_lf.jpg"), Driver->getTexture("../../../media/irrlicht2_rt.jpg"), Driver->getTexture("../../../media/irrlicht2_ft.jpg"), Driver->getTexture("../../../media/irrlicht2_bk.jpg")); Driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true); Box->setScale(core::vector3df (10.0f,10.0f,10.0f)); IAnimatedMesh* Mesh = Smgr->getMesh("../../media/drone.x"); Drone = Smgr-> addAnimatedMeshSceneNode (Mesh); Drone->setMaterialFlag(EMF_LIGHTING, false); Drone->setMaterialTexture( 0, Driver->getTexture("../../../media/drone.bmp") ); Drone->setPosition (core::vector3df (0, 0, 0)); Drone->setScale (core::vector3df (0.02f, 0.02f, 0.02f)); Drone->setMaterialFlag(EMF_LIGHTING, true); Drone->getMaterial(0).EmissiveColor.set(0,0,0,0); /*Drone = Smgr->addCubeSceneNode(50.0f, 0, -1, core::vector3df(0,0,0), core::vector3df(0,0,0)) ; Drone->setMaterialTexture(0, Driver->getTexture("../../media/t351sml.jpg")); Drone->setMaterialFlag(video::EMF_LIGHTING, false);*/ CreateCameras (Smgr, Drone, Cameras) ; CameraExterne = Cameras[0] ; CameraInterne = Cameras[1] ; VRect_t VR = SplitScreen() ; PremierTour = true ; Zoom = 0; AngleMax = 0; IAnimatedMesh* mesh = Smgr->getMesh("../../media/sydney.md2"); IAnimatedMeshSceneNode* node = Smgr->addAnimatedMeshSceneNode( mesh ); node->setMaterialFlag(EMF_LIGHTING, false); node->setFrameLoop(0, 310); node->setMaterialTexture( 0, Driver->getTexture("../../media/sydney.bmp") ); node->setPosition (core::vector3df (0, 0, 100.0f)); CreateCollision (node, mesh, Smgr, Drone); core::vector3df pos = Drone->getPosition(); core::vector3df rot = Drone->getRotation(); cout << pos.X << " " << pos.Y << " " << pos.Z << endl; cout << rot.X << " " << rot.Y << " " << rot.Z << endl; while(Device->run()) { Driver->setViewPort(rect<s32>(0,0,VI[0],VI[1])); Driver->beginScene(true, true, SColor(255,100,101,140)); Smgr->setActiveCamera(CameraExterne); Driver->setViewPort(VR[0]); Smgr->drawAll(); Driver->setViewPort(VR[1]); Smgr->setActiveCamera(CameraInterne); Driver->setViewPort(VR[1]); Smgr->drawAll(); Guienv->drawAll(); Driver->endScene(); } Device->drop(); return 0; } voici les fonctions qui m'intéressent en question : void MoveCameras (ISceneNode * Drone, ICameraSceneNode * CameraExterne, ICameraSceneNode * CameraInterne, float CameraRotation) { core::vector3df PosDrone = Drone->getPosition(); CameraExterne->setTarget (PosDrone); CameraInterne->setPosition(PosDrone); core::vector3df RotDrone = Drone->getRotation(); CameraInterne->setRotation(core::vector3df( RotDrone.X + CameraRotation, RotDrone.Y, RotDrone.Z)); } // MoveCameras() void MoveDrone (IrrlichtDevice * Device, scene::ISceneNode* Drone, int VRotationX, int VRotationY, int VRotationZ, int VTranslationX, int VTranslationY, int VTranslationZ, ICameraSceneNode * CameraExterne, ICameraSceneNode * CameraInterne, bool PremierTour, float CameraRotation) { static u32 lastTime, Time; if (PremierTour) { lastTime = 0; Time = 0; } else { Time = Device->getTimer()->getRealTime(); } core::vector3df v = Drone->getPosition(); core::vector3df vr = Drone->getRotation(); if (vr.X >= 10.0f || vr.X <= -10.0f) VRotationX = 0; if (vr.Z >= 10.0f || vr.Z <= -10.0f) VRotationZ = 0; float vrRadY = -vr.Y / 180 * 3.141592654f; float CosTargetX = cos (vrRadY + (3.14f/2.0f)); float SinTargetZ = sin (vrRadY + (3.14f/2.0f)); float CosLateralX = cos ((vrRadY + (3.14f))); float SinLateralZ = sin ((vrRadY + (3.14f))); v.X += ( ((float) VTranslationZ * CosTargetX + CosLateralX * (float) VTranslationX ) * 50.0f) * ( (Time - lastTime) / 1000.0f); v.Y += ( ((float) VTranslationY) * 50.0f) * ( (Time - lastTime) / 1000.0f); v.Z += ( ((float) VTranslationZ * SinTargetZ + SinLateralZ * (float) VTranslationX ) * 50.0f) * ( (Time - lastTime) / 1000.0f); vr.X += ((float) VRotationX); //( ((float) VRotationX) * 360.0f) * ( (Time - lastTime) / 1000.0f); vr.Y += ((float) VRotationY); //( ((float) VRotationY) * 360.0f) * ( (Time - lastTime) / 1000.0f); vr.Z += ((float) VRotationZ); //( ((float) VRotationZ) * 360.0f) * ( (Time - lastTime) / 1000.0f); cout << vr.Y << endl; Drone->setPosition(v); Drone->setRotation(vr); if (PremierTour) { Time = Device->getTimer()->getRealTime(); } lastTime = Time ; MoveCameras (Drone, CameraExterne, CameraInterne, CameraRotation) ; } // MoveDrone() void CreateCollision (ISceneNode * Decor, IAnimatedMesh * Mesh, ISceneManager* Smgr, scene::ISceneNode * Drone) { scene::ITriangleSelector* selector = Smgr->createOctTreeTriangleSelector(Mesh->getMesh(0), Decor, 128); Decor->setTriangleSelector(selector); selector->drop(); scene::ISceneNodeAnimator* anim = Smgr->createCollisionResponseAnimator( selector, Drone, core::vector3df(30,50,30), core::vector3df(0,0,0), core::vector3df(0,50,0)); Drone->addAnimator(anim); anim->drop(); } // CreateCollision()
A noter que ce n'est encore qu'une ébauche.
Hors ligne