//------------------------------------------------------ //重力計算・床との衝突判定を組み込んだプログラム //------------------------------------------------------ #include #include #include #pragma comment(lib, "d3d9.lib") #pragma comment(lib, "d3dx9.lib") #pragma comment(lib, "winmm.lib") #pragma comment(lib, "d3d8.lib") void GetKey(); //キー入力取得 float myx=0; //キャラクター現在地x座標 float myy=0; //キャラクター現在地y座標 float myz=0; //キャラクター現在地z座標 void CollisionVertical(); //落下距離計算 //----------------------------------------------------------------------------- // Global variables //----------------------------------------------------------------------------- LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device //Xファイル1 LPD3DXMESH g_pMesh = NULL; // Our mesh object in sysmem D3DMATERIAL9* g_pMeshMaterials = NULL; // Materials for our mesh LPDIRECT3DTEXTURE9* g_pMeshTextures = NULL; // Textures for our mesh DWORD g_dwNumMaterials = 0L; // Number of mesh materials //Xファイル2 LPD3DXMESH g_pMesh1 = NULL; // Our mesh object in sysmem D3DMATERIAL9* g_pMeshMaterials1 = NULL; // Materials for our mesh LPDIRECT3DTEXTURE9* g_pMeshTextures1 = NULL; // Textures for our mesh DWORD g_dwNumMaterials1 = 0L; // Number of mesh materials DWORD timeaf , timebf; //ジャンプ時間記録 float v0; //物体の落下初速度 //----------------------------------------------------------------------------- // Name: InitD3D() // Desc: Initializes Direct3D //----------------------------------------------------------------------------- HRESULT InitD3D( HWND hWnd ) { // Create the D3D object. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; // Set up the structure used to create the D3DDevice. Since we are now // using more complex geometry, we will create a device with a zbuffer. D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // Create the D3DDevice if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } // Turn on the zbuffer g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); // Turn on ambient lighting g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff ); return S_OK; } //----------------------------------------------------------------------------- // Name: InitGeometry() // Desc: Load the mesh and build the material and texture arrays //----------------------------------------------------------------------------- HRESULT InitGeometry() { LPD3DXBUFFER pD3DXMtrlBuffer; // Load the mesh from the specified file if( FAILED( D3DXLoadMeshFromX( TEXT("map1.x"), D3DXMESH_SYSTEMMEM, g_pd3dDevice, NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, &g_pMesh ) ) ) { // If model is not in current folder, try parent folder if( FAILED( D3DXLoadMeshFromX( "..\\map1.x", D3DXMESH_SYSTEMMEM, g_pd3dDevice, NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, &g_pMesh ) ) ) { MessageBox(NULL, "Could not find map1.x", "Meshes.exe", MB_OK); return E_FAIL; } } // We need to extract the material properties and texture names from the // pD3DXMtrlBuffer D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer(); g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials]; g_pMeshTextures = new LPDIRECT3DTEXTURE9[g_dwNumMaterials]; for( DWORD i=0; i 0 ) { // Create the texture if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, d3dxMaterials[i].pTextureFilename, &g_pMeshTextures[i] ) ) ) { // If texture is not in current folder, try parent folder const TCHAR* strPrefix = TEXT("..\\"); const int lenPrefix = lstrlen( strPrefix ); TCHAR strTexture[MAX_PATH]; lstrcpyn( strTexture, strPrefix, MAX_PATH ); lstrcpyn( strTexture + lenPrefix, d3dxMaterials[i].pTextureFilename, MAX_PATH - lenPrefix ); // If texture is not in current folder, try parent folder if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, strTexture, &g_pMeshTextures[i] ) ) ) { MessageBox(NULL, "Could not find texture map", "Meshes.exe", MB_OK); } } } } // Done with the material buffer pD3DXMtrlBuffer->Release(); return S_OK; } //----------------------------------------------------------------------------- // Name: InitGeometry1() // Desc: Load the mesh and build the material and texture arrays //----------------------------------------------------------------------------- HRESULT InitGeometry1() { LPD3DXBUFFER pD3DXMtrlBuffer; // Load the mesh from the specified file if( FAILED( D3DXLoadMeshFromX( "chara1.x", D3DXMESH_SYSTEMMEM, g_pd3dDevice, NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials1, &g_pMesh1 ) ) ) { // If model is not in current folder, try parent folder if( FAILED( D3DXLoadMeshFromX( "..\\chara1.x", D3DXMESH_SYSTEMMEM, g_pd3dDevice, NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials1, &g_pMesh1 ) ) ) { MessageBox(NULL, "Could not find chara1.x", "Meshes.exe", MB_OK); return E_FAIL; } } // We need to extract the material properties and texture names from the // pD3DXMtrlBuffer D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer(); g_pMeshMaterials1 = new D3DMATERIAL9[g_dwNumMaterials1]; g_pMeshTextures1 = new LPDIRECT3DTEXTURE9[g_dwNumMaterials1]; for( DWORD i=0; i 0 ) { // Create the texture if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, d3dxMaterials[i].pTextureFilename, &g_pMeshTextures1[i] ) ) ) { // If texture is not in current folder, try parent folder const TCHAR* strPrefix = TEXT("..\\"); const int lenPrefix = lstrlen( strPrefix ); TCHAR strTexture[MAX_PATH]; lstrcpyn( strTexture, strPrefix, MAX_PATH ); lstrcpyn( strTexture + lenPrefix, d3dxMaterials[i].pTextureFilename, MAX_PATH - lenPrefix ); // If texture is not in current folder, try parent folder if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, strTexture, &g_pMeshTextures1[i] ) ) ) { MessageBox(NULL, "Could not find texture map", "Meshes.exe", MB_OK); } } } } // Done with the material buffer pD3DXMtrlBuffer->Release(); return S_OK; } //----------------------------------------------------------------------------- // Name: Cleanup() // Desc: Releases all previously initialized objects //----------------------------------------------------------------------------- VOID Cleanup() { if( g_pMeshMaterials != NULL ) delete[] g_pMeshMaterials; if( g_pMeshTextures ) { for( DWORD i = 0; i < g_dwNumMaterials; i++ ) { if( g_pMeshTextures[i] ) g_pMeshTextures[i]->Release(); } delete[] g_pMeshTextures; } if( g_pMesh != NULL ) g_pMesh->Release(); if( g_pd3dDevice != NULL ) g_pd3dDevice->Release(); if( g_pD3D != NULL ) g_pD3D->Release(); if( g_pMeshMaterials1 != NULL ) delete[] g_pMeshMaterials1; if( g_pMeshTextures1 ) { for( DWORD i = 0; i < g_dwNumMaterials1; i++ ) { if( g_pMeshTextures1[i] ) g_pMeshTextures1[i]->Release(); } delete[] g_pMeshTextures1; } if( g_pMesh1 != NULL ) g_pMesh1->Release(); } //----------------------------------------------------------------------------- // Name: SetupMatrices() // Desc: Sets up the world, view, and projection transform matrices. //----------------------------------------------------------------------------- VOID SetupMatrices(int k) { // For our world matrix, we will just leave it as the identity D3DXMATRIXA16 matWorld; if(k==1){ //マップ描画なら D3DXMatrixTranslation(&matWorld, 0, 0, 0); //移動行列を生成 g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld); } else if(k==2){ //キャラクター描画なら D3DXMatrixTranslation(&matWorld, myx, myy+3.0f, myz); //移動行列を生成 g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld); } D3DXVECTOR3 vEyePt( myx+0.0f, myy+5.0f, myz-5.0f ); //カメラ位置 D3DXVECTOR3 vLookatPt( myx, myy+1.5f, myz+1.0f ); //見る場所 D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f ); //上方位置 D3DXMATRIXA16 matView; D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec ); g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView ); D3DXMATRIXA16 matProj; D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); } //----------------------------------------------------------------------------- // Name: Render() // Desc: Draws the scene //----------------------------------------------------------------------------- VOID Render(){ // Clear the backbuffer and the zbuffer g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ){ // Setup the world, view, and projection matrices SetupMatrices(1); // Meshes are divided into subsets, one for each material. Render them in // a loop for( DWORD i=0; iSetMaterial( &g_pMeshMaterials[i] ); g_pd3dDevice->SetTexture( 0, g_pMeshTextures[i] ); // Draw the mesh subset g_pMesh->DrawSubset( i ); } // End the scene g_pd3dDevice->EndScene(); SetupMatrices(2); for( DWORD j=0; jSetMaterial( &g_pMeshMaterials1[j] ); g_pd3dDevice->SetTexture( 0, g_pMeshTextures1[j] ); // Draw the mesh subset g_pMesh1->DrawSubset( j ); } // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } //----------------------------------------------------------------------------- // Name: MsgProc() // Desc: The window's message handler //----------------------------------------------------------------------------- LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){ switch( msg ) { case WM_CREATE: timebf = timeGetTime(); //物体の落下開始時刻記録(物体が浮いた状態で始まるので) v0 = 0; //物体の落下初速度を初期化 return 0; case WM_DESTROY: Cleanup(); PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, msg, wParam, lParam ); } //----------------------------------------------------------------------------- // Name: WinMain() // Desc: The application's entry point //----------------------------------------------------------------------------- INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) { // Register the window class WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "orerpg", NULL }; RegisterClassEx( &wc ); // Create the application's window HWND hWnd = CreateWindow( "orerpg", "rpg", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL ); // Initialize Direct3D if( SUCCEEDED( InitD3D( hWnd ) ) ) { // Create the scene geometry if( SUCCEEDED( InitGeometry() )&&SUCCEEDED( InitGeometry1() ) ) { // Show the window ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); // Enter the message loop MSG msg; ZeroMemory( &msg, sizeof(msg) ); while( msg.message!=WM_QUIT ) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else{ CollisionVertical(); //物理計算 GetKey(); //キー入力取得 Render(); //画面へ描画 } } } } UnregisterClass( "orerpg", wc.hInstance ); return 0; } void GetKey(){ //右上移動 if(GetAsyncKeyState(VK_UP)&0x8000&&GetAsyncKeyState(VK_RIGHT)&0x8000){ myz+=0.1f; myx+=0.1f; return; } //左上移動 else if(GetAsyncKeyState(VK_UP)&0x8000&&GetAsyncKeyState(VK_LEFT)&0x8000){ myz+=0.1f; myx-=0.1f; return; } //右下移動 else if(GetAsyncKeyState(VK_DOWN)&0x8000&&GetAsyncKeyState(VK_RIGHT)&0x8000){ myz-=0.1f; myx+=0.1f; return; } //左下移動 else if(GetAsyncKeyState(VK_DOWN)&0x8000&&GetAsyncKeyState(VK_LEFT)&0x8000){ myz-=0.1f; myx-=0.1f; return; } //上移動 if(GetAsyncKeyState(VK_UP)&0x8000){ myz+=0.1f; } //右移動 else if(GetAsyncKeyState(VK_RIGHT)&0x8000){ myx+=0.1f; } //下移動 else if(GetAsyncKeyState(VK_DOWN)&0x8000){ myz-=0.1f; } //左移動 else if(GetAsyncKeyState(VK_LEFT)&0x8000){ myx-=0.1f; } //ジャンプ if(GetAsyncKeyState(VK_SPACE)&0x8000){ myy+=3.0f; //重力計算面倒なので、とりあえず瞬間的に物体を3だけ上方へわーぷ timebf = timeGetTime(); //ジャンプ時間記録 v0 = 0; //初速度初期化 } return; } void CollisionVertical(){ BOOL bHit, gHit; DWORD pFaceIndex; float pDist; //レイの長さ float v1; //物体の現在の速度 static int g = 9.8; //重力。 D3DXIntersect(g_pMesh , &D3DXVECTOR3(myx , myy+3.0f , myz) , &D3DXVECTOR3(0 , -1 , 0) , &gHit, &pFaceIndex, NULL, NULL, &pDist, NULL, NULL ); //レイを下方(0,-1,0)へ飛ばす if(gHit){ //レイを出した方向にメッシュがあるなら if( 0.6 < pDist ){ //メッシュまでの距離が1以上なら timeaf = timeGetTime(); float ti = (float)(timeaf - timebf)*0.001; //扱いやすい形式に置換 myy = myy - ( ( v0*( ti ) ) + ( 0.5*g*( ti )*( ti ) ) ); //落下距離計算 h = 1/2gt^2 v1 = v0 + ( g*( ti ) ); //現在の速度を計算 V1 = Vo + gt v0 = v1; //次の初速度として使うため、現在速度をv0へ記録 timebf = timeaf; //同上 } } else if(!gHit){ //メッシュより下にキャラクターが落ちたり、メッシュ外にキャラクターが出たら //myy = 0.0f; } return; }