Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

coooooode

1.      Cho người chạy theo track trong morfit

#include "Engine\\Include\\mrft_api.h"

#include <iostream.h>

 void main(void)

{

            void Move_man_on_track(DWORD man,DWORD track);

            int rc=Morfit_engine_load_world("nguoi2.wld",".","Bitmaps",USER_DEFINED_BEHAVIOR);

            if(rc==VR_ERROR) {

                        cout << "Failed to load world, aborting

";

                        cout << "look at error.log to see why

";

                        return;

            }

                          Morfit_engine_set_default_rendering_window_title("esc to exit");

            Morfit_engine_maximize_default_rendering_window();

            ShowCursor(FALSE);  // Hide the cursor

            DWORD camera=Morfit_camera_get_default_camera();

            DWORD man=Morfit_object_get_object_using_name("man");

            DWORD track=Morfit_track_get_using_name("tr");

            Morfit_object_set_chase_type(man, CHASE_TRACK);

            Morfit_object_set_track(man,track);

            Morfit_object_set_absolute_speed(man,20);

// nguoi k bi chim duoi dat

            double box[2][3];

            Morfit_object_get_bounding_box(man,box);

            double height = (box[1][2]-box[0][2]);

            double up[3]={0,0,height/2};

            Morfit_object_set_track_offset(man,up);

            while( (GetAsyncKeyState(VK_ESCAPE)&1) ==0 ) {

                        Morfit_engine_render(NULL,camera);         

            }

}

2.      Di chuyen camera và di chuyen doi tuong

#include "Engine\\Include\\mrft_api.h"

#include <iostream.h>

void main(void)

{

      void Move_camera(DWORD camera);

      void MoveCar(DWORD car_handle);

      int rc = Morfit_engine_load_world ( "bai2.wld", ".", "Bitmaps", USER_DEFINED_BEHAVIOR);

      if(rc==VR_ERROR) {

                  cout << "Ban da bi loi khi load the gioi rui !!!

";

                  cout << "look at error.log to see why

";

                  return;

}

      Morfit_engine_set_default_rendering_window_title("To move the camera, use arrow keys, 'f', 'b'  <ESC>  -  Exit");

      Morfit_engine_maximize_default_rendering_window();

      ShowCursor(FALSE);  // Hide the cursor

      DWORD camera=Morfit_camera_get_default_camera();

      Morfit_camera_set_chase_type(camera,NO_CHASE);

      DWORD car=Morfit_object_get_object_using_name("mycar_3");

      Morfit_object_set_direction(car,-1,0,0);

      while( (GetAsyncKeyState(VK_ESCAPE)==0)) { // Loop until escape is pressed

                  MoveCar(car);

                  Move_camera(camera);

                  Morfit_engine_render(NULL,camera);         

      }

}

void Move_camera(DWORD camera)

{

      double x,y,z;

      //Gets camera's location coordinates

      Morfit_camera_get_location(camera, &x,&y,&z);

      if(GetAsyncKeyState('T') < 0) // move camera up

                  z +=25;

      if(GetAsyncKeyState('G') < 0) // move camera down

                  z -=25;

      if(GetAsyncKeyState('F') < 0) // move camera right

                  y +=25;

      if(GetAsyncKeyState('H') < 0) // move camera left

                  y -=25;

      if(GetAsyncKeyState('B') < 0) // move camera back

                  x +=25;

      if(GetAsyncKeyState('F') < 0) // move camera forward

                  x -=25;

      Morfit_camera_set_location(camera, x, y, z);

      return;

}

void MoveCar(DWORD car_handle)

{

      const int speed = 20;

      if (GetAsyncKeyState(VK_UP)<0)

                  Morfit_object_move(car_handle,OBJECT_SPACE,-speed,0,0);

      if (GetAsyncKeyState(VK_DOWN)<0)

                  Morfit_object_move(car_handle,OBJECT_SPACE,speed,0,0);

      if (GetAsyncKeyState(VK_LEFT)<0)

                  Morfit_object_move(car_handle,OBJECT_SPACE,0,-speed,0);

      if (GetAsyncKeyState(VK_RIGHT)<0)

                  Morfit_object_move(car_handle,OBJECT_SPACE,0,speed,0);

      if (GetAsyncKeyState(VK_SPACE)<0)

                  Morfit_object_rotate_z(car_handle,90,OBJECT_SPACE);

      return;

}

3.      Chuyen camera (2 xe, 2 track, 2 camera  moi xe mot track mot camera)

#include "Engine\\Include\\mrft_api.h"

#include <iostream.h>

void main(void)

{

      DWORD MoveCamera();      

      int rc=Morfit_engine_load_world("bai4.wld",".","Bitmaps",USER_DEFINED_BEHAVIOR);

      if(rc==VR_ERROR) {

                  cout << "Failed to load world, aborting

";

                  cout << "look at error.log to see why

";

                  return;

      }

      Morfit_engine_set_default_rendering_window_title("An phim 'C' de chuyen camera va esc to exit");

      Morfit_engine_maximize_default_rendering_window();

      ShowCursor(FALSE);  // Hide the cursor

      DWORD camera;

      while( (GetAsyncKeyState(VK_ESCAPE)&1) ==0 ) {

                  camera = MoveCamera();

                  Morfit_engine_render(NULL,camera);         

      }

}

DWORD MoveCamera()

{

      static DWORD camera = Morfit_camera_get_default_camera();

      static DWORD object_handle = Morfit_object_get_first_object();

      enum movement_mode {Move, RotateX, RotateY, RotateZ, Bank,HeadAngle, Tilt, Zoom};

      static movement_mode mode=Move;

      const speed = 50;

      if (GetAsyncKeyState('C')<0)

      {

                  camera=Morfit_camera_get_next_camera(camera);

                  if (camera==NULL) camera=Morfit_camera_get_first_camera();

                  mode = Move;

      }

      return camera;

}

4.      Va chạm của các quả bóng (các quả bóng để động)

#include "Engine\\Include\\mrft_api.h"

#include<iostream.h>

void main()

{

            void init(DWORD ball);

            void restart_stopped_balls(DWORD ball[10]);

            int        rc=Morfit_engine_load_world("qua bong.wld",".","Bitmaps",USER_DEFINED_BEHAVIOR);

            if(rc==VR_ERROR) {

                        cout << "Failed to load world, aborting

";

                        cout << "look at error.log to see why

";

                        return;

            }

            Morfit_engine_set_default_rendering_window_title("Va cham giua cac qua bong");

            Morfit_engine_maximize_default_rendering_window();

            ShowCursor(FALSE);

            DWORD camera=Morfit_camera_get_default_camera();

            DWORD ball[10];

            ball[0]=Morfit_object_get_object_using_name("ball1");

            ball[1]=Morfit_object_get_object_using_name("ball2");

            ball[2]=Morfit_object_get_object_using_name("ball3");

            ball[3]=Morfit_object_get_object_using_name("ball4");

            ball[4]=Morfit_object_get_object_using_name("ball5");

            ball[5]=Morfit_object_get_object_using_name("ball6");

            ball[6]=Morfit_object_get_object_using_name("ball7");

            ball[7]=Morfit_object_get_object_using_name("ball8");

            ball[8]=Morfit_object_get_object_using_name("ball9");

            ball[9]=Morfit_object_get_object_using_name("ball10");

            for(int i=0;i<10;i++)

                        init(ball[i]);

            while ((GetAsyncKeyState(VK_ESCAPE)&1)==0)

            {

                        restart_stopped_balls(ball);                                

                        Morfit_engine_render(NULL,camera);       

            }

            Morfit_engine_close();

            exit(0);

}

            void init(DWORD ball)

            {

                        Morfit_object_set_chase_type(ball,CHASE_PHYSICS);

                        double speed[3]={rand(),rand(),rand()};

                        if(speed[2]<0) speed[2]=-speed[2];

                        Morfit_object_set_speed(ball,speed);

                        Morfit_object_set_absolute_speed(ball,rand()*200);

                        Morfit_object_set_max_speed(ball,150);

                        Morfit_object_set_friction(ball,0);

                        Morfit_object_set_elasticity(ball,.7);

                        double force[3]={0,0,-1};

                        Morfit_object_set_force(ball,force);

            }

            void restart_stopped_balls(DWORD ball[10])

            {

                        double speed;

                        for(int i=0;i<10;i++)

                        {

                                    speed=Morfit_object_get_absolute_speed(ball[i]);

                                    if (speed<.5)

                                                init(ball[i]);

                        }

                        return;

            }

5.      Tạo sóng

#include<conio.h>

#include<stdio.h>

#include<math.h>

#include<iostream.h>

#include "Engine\\Include\\Mrft_api.h"

#define MAX_POLYS 20

void DrawPolygons();

void AddBitmap();

void AddPoint(double point[3],DWORD poly_handle);

void CreatePolygon(double p1[3],double p2[3],double p3[3]);

void Wave();

void main()

{

            //Load an empty world into the engine

            int result = Morfit_engine_load_world("song1.wld",".","Bitmaps",

                                                                                     USER_DEFINED_BEHAVIOR | EDITOR_MODE);

            if(result==VR_ERROR)

            {

                        cout<<"

Failed to load world, aborting

";

                        return;

            }

            Morfit_engine_set_resolution(640,480,16);  

            Morfit_engine_hide_log_window(); //Hide the window with the progress bar

            Morfit_engine_maximize_default_rendering_window();

            Morfit_engine_set_default_rendering_window_title("Console Samples: Wave Sample                                      Press ESC to end");

            ShowCursor(FALSE);

            Morfit_engine_set_perspective_correction_accuracy(0);

            Morfit_engine_set_picture_quality(2000*2000);

            DWORD camera = Morfit_camera_get_default_camera();

            Morfit_camera_set_location(camera,5000,2500,500);

            Morfit_camera_set_direction(camera,-1,0,0);

            DrawPolygons();

            AddBitmap();

            //The main loop

            while( (GetAsyncKeyState(VK_ESCAPE)&1) ==0 )

            {

                        Wave();

                        Morfit_engine_render(NULL,camera);         

            }

            Morfit_engine_close();

}

void DrawPolygons()

{

            double p1[3],p2[3],p3[3],p4[3];

            //initialize points

            p1[0] = 100;   p1[1] = 100;   p1[2] = 0;

            p2[0] = 400;   p2[1] = 100;   p2[2] = 0;

            p3[0] = 400;   p3[1] = 400;   p3[2] = 0;

            p4[0] = 100;   p4[1] = 400;   p4[2] = 0;

            for(int row = 0;row < MAX_POLYS;row++)

            {

                        for(int col = 0;col < MAX_POLYS;col++)

                        {

                                    CreatePolygon(p1,p3,p2);//(add the points clockwise

                                    CreatePolygon(p1,p4,p3); // to make the polygons visible)

                                    //increment the y coordinate by 300

                                    p1[1] += 300;

                                    p2[1] += 300;

                                    p3[1] += 300;

                                    p4[1] += 300;

                        }

                        //increment the x coord. by 300, and reinitialize y to first column

                        p1[0] += 300;   p1[1] = 100;  

                        p2[0] += 300;   p2[1] = 100;  

                        p3[0] += 300;   p3[1] = 400;

                        p4[0] += 300;   p4[1] = 400;

            }

}

void CreatePolygon(double p1[], double p2[], double p3[])

{

            //create the polygon    

            DWORD polygon_handle = Morfit_polygon_create();

            if (!polygon_handle)

                        return;

            //add 3 points to the polygon

            AddPoint(p1,polygon_handle);

            AddPoint(p2,polygon_handle);

            AddPoint(p3,polygon_handle);

            //actually draw the polygon on the screen

            if (OK != Morfit_engine_add_polygon(polygon_handle))

                        return;

}

void AddPoint(double point[3],DWORD poly_handle)

{

            double location[6];

            DWORD point_handle;

            //fill location with values and add the point

            location[0] = point[0];

            location[1] = point[1];

            location[2] = point[2];

            location[3] = 0;

            location[4] = 0;

            location[5] = 100;

            point_handle = Morfit_polygon_add_point(poly_handle,location);

            if(!point_handle)

                        return;

}

void AddBitmap()

{

            DWORD bitmap_handle = Morfit_bitmap_load("Bitmaps\\water1",-1 );

            if(!bitmap_handle)

                        return;

            //Add bitmap

            int res = Morfit_group_wrap_a_bitmap(NULL,bitmap_handle,1,1);

            Morfit_engine_set_atmospheric_effect(80,80,110);

}

void Wave()

{

            DWORD time = GetTickCount();

            double coords[3];

            for(DWORD poly=Morfit_polygon_get_first_polygon();poly!=NULL;

                                                            poly = Morfit_polygon_get_next(poly))

            {

                        for(DWORD point=Morfit_polygon_get_first_point(poly);point!=NULL;

                                                                                    point = Morfit_point_get_next_point(point))

                        {                                 

                                    Morfit_point_get_xyz(point,coords);

                                    coords[2] = sin(coords[0]+ coords[1] +(double)time/300)*50;

                                    Morfit_point_set_xyz(point,coords);

                        }

            }

}

6.      Va cham oto va nha

#include "Engine\Include\mrft_api.h"

#include <iostream.h>

void main(void)

{

      void MoveCar(DWORD car_handle,double half_car_length);

      int rc=Morfit_engine_load_world("house_car.wld",".","Bitmaps",USER_DEFINED_BEHAVIOR);

      if(rc==VR_ERROR)

                  {

                  }

      Morfit_engine_set_default_rendering_window_title("Doi tuong va cham   <esc> to exit");

      Morfit_engine_maximize_default_rendering_window();

      DWORD camera=Morfit_camera_get_default_camera();

      DWORD car=Morfit_object_get_object_using_name("mycar");

      Morfit_object_set_direction(car,-1,0,0);

      double box[2][3];

      Morfit_object_get_bounding_box(car, box);

      double half_car_length=(box[1][0]-box[0][0])/2;

      while( (GetAsyncKeyState(VK_ESCAPE)&1) ==0 ) {

                  MoveCar(car,half_car_length);

                  Morfit_engine_render(NULL,camera);         

      }

}

void MoveCar(DWORD car_handle, double half_car_length)

{

      if ((GetAsyncKeyState(VK_LEFT)<0) ==1 )

      {

      Morfit_object_rotate_z(car_handle,10,OBJECT_SPACE);

      }

      if ((GetAsyncKeyState(VK_RIGHT)<0) ==1 )

      {

      Morfit_object_rotate_z(car_handle,-10,OBJECT_SPACE);

      }

      if ((GetAsyncKeyState(VK_SPACE)<0) ==1 )

      {

                  const int speed = 20;

                  double x,y,z;

                  double dir_x,dir_y,dir_z;

                  double front_of_car[3];

                  double ahead_of_car[3];

                  Morfit_object_get_location(car_handle,&x,&y,&z);

                  Morfit_object_get_direction(car_handle,&dir_x,&dir_y,&dir_z);

                  front_of_car[0]=x+half_car_length*dir_x;

                  front_of_car[1]=y+half_car_length*dir_y;

                  front_of_car[2]=z+half_car_length*dir_z;

                  ahead_of_car[0]=front_of_car[0]+speed*dir_x;

                  ahead_of_car[1]=front_of_car[1]+speed*dir_y;

                  ahead_of_car[2]=front_of_car[2]+speed*dir_z;

                  DWORD intersecting_polygon;

                  DWORD intersecting_object;

                  double intersection_point[3];

                  if (Morfit_object_is_movement_possible(car_handle, front_of_car,ahead_of_car,&intersecting_polygon,intersection_point,&intersecting_object))

                              Morfit_object_move(car_handle,OBJECT_SPACE,-speed,0,0);

      }

      return;

}

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: #nga