//初期化 void BcStaticChara::OnCreate() { //Rigidbodyの初期化 auto PtrGameStage = GetStage<GameStage>(); Rigidbody body; body.m_Owner = GetThis<GameObject>(); body.m_Mass = 1.0f; body.m_Scale = m_Scale; body.m_Quat = m_Qt; body.m_Pos = m_Pos; body.m_CollType = CollType::typeCAPSULE; body.m_IsFixed = true; // body.m_IsDrawActive = true; body.SetToBefore(); PtrGameStage->AddRigidbody(body); //メッシュとトランスフォームの差分の設定 m_MeshToTransformMatrix.affineTransformation( Vec3(1.0f, 1.0f, 1.0f), Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, -1.0f, 0.0f) ); //行列の定義 Mat4x4 World; World.affineTransformation( m_Scale, Vec3(0, 0, 0), m_Qt, m_Pos ); //差分を計算 World = m_MeshToTransformMatrix * World; auto MeshPtr = App::GetApp()->GetResource<MeshResource>(L"MODEL_MESH"); //描画データの構築 m_PtrObj = make_shared<BcDrawObject>(); m_PtrObj->m_OwnShadowmapActive = m_OwnShadowActive; m_PtrObj->m_UsedModelColor = false; m_PtrObj->m_UsedModelTextre = true; m_PtrObj->m_ShadowmapUse = true; m_PtrObj->m_FogEnabled = true; //フォグは黄色っぽく m_PtrObj->m_FogColor = Col4(0.5f, 0.5f, 0.1f, 1.0f); m_PtrObj->m_FogStart = -10.0f; m_PtrObj->m_FogEnd = -30.0f; m_PtrObj->m_MeshRes = MeshPtr; m_PtrObj->m_WorldMatrix = World; m_PtrObj->m_Camera = GetStage<Stage>()->GetCamera(); //シャドウマップ描画データの構築 m_PtrShadowmapObj = make_shared<ShadowmapObject>(); m_PtrShadowmapObj->m_MeshRes = MeshPtr; //描画データの行列をコピー m_PtrShadowmapObj->m_WorldMatrix = World; m_PtrShadowmapObj->m_Camera = GetStage<Stage>()->GetCamera(); }
//初期化 void BoneChara::OnCreate() { //Rigidbodyの初期化 auto PtrGameStage = GetStage<GameStage>(); Rigidbody body; body.m_Owner = GetThis<GameObject>(); body.m_Mass = 1.0f; body.m_Scale = m_Scale; body.m_Quat = m_Qt; body.m_Pos = m_Pos; body.m_CollType = CollType::typeCAPSULE; body.m_IsFixed = true; // body.m_IsDrawActive = true; body.SetToBefore(); PtrGameStage->AddRigidbody(body); //メッシュとトランスフォームの差分の設定 m_MeshToTransformMatrix.affineTransformation( Vec3(1.0f, 1.0f, 1.0f), Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, -1.0f, 0.0f) ); //行列の定義 Mat4x4 World; World.affineTransformation( m_Scale, Vec3(0, 0, 0), m_Qt, m_Pos ); //差分を計算 World = m_MeshToTransformMatrix * World; auto MeshPtr = App::GetApp()->GetResource<MeshResource>(L"Chara_R_MESH"); //描画データの構築 m_PtrObj = make_shared<SimpleDrawObject>(); m_PtrObj->m_MeshRes = MeshPtr; m_PtrObj->m_WorldMatrix = World; m_PtrObj->m_Camera = GetStage<Stage>()->GetCamera(); m_PtrObj->m_UsedModelColor = false; m_PtrObj->m_UsedModelTextre = true; m_PtrObj->m_OwnShadowmapActive = m_OwnShadowActive; m_PtrObj->m_ShadowmapUse = true; m_PtrObj->BoneInit(); m_PtrObj->AddAnimation(L"Default", 0, 100, true, 20.0f); m_PtrObj->ChangeCurrentAnimation(L"Default"); //シャドウマップ描画データの構築 m_PtrShadowmapObj = make_shared<ShadowmapObject>(); m_PtrShadowmapObj->m_MeshRes = MeshPtr; //描画データの行列をコピー m_PtrShadowmapObj->m_WorldMatrix = World; m_PtrShadowmapObj->m_Camera = GetStage<Stage>()->GetCamera(); }
//初期化 void BcBoxObject::OnCreate() { //Rigidbodyの初期化 auto PtrGameStage = GetStage<GameStage>(); Rigidbody body; body.m_Owner = GetThis<GameObject>(); body.m_Mass = 1.0f; body.m_Scale = m_Scale; body.m_Quat = m_Qt; body.m_Pos = m_Pos; body.m_CollType = CollType::typeOBB; body.m_IsFixed = true; // body.m_IsDrawActive = true; body.SetToBefore(); PtrGameStage->AddRigidbody(body); //行列の定義 Mat4x4 World; World.affineTransformation( body.m_Scale, Vec3(0, 0, 0), body.m_Quat, body.m_Pos ); auto TexPtr = App::GetApp()->GetResource<TextureResource>(m_TextureResName); auto NormTexPtr = App::GetApp()->GetResource<TextureResource>(m_NormalTextureResName); //メッシュの取得 auto MeshPtr = App::GetApp()->GetResource<MeshResource>(L"NORMAL_BOX"); //描画データの構築 m_PtrObj = make_shared<BcDrawObject>(); m_PtrObj->m_MeshRes = MeshPtr; m_PtrObj->m_TextureRes = TexPtr; m_PtrObj->m_NormalTextureRes = NormTexPtr; m_PtrObj->m_WorldMatrix = World; m_PtrObj->m_Camera = GetStage<Stage>()->GetCamera(); m_PtrObj->m_OwnShadowmapActive = true; m_PtrObj->m_ShadowmapUse = true; m_PtrObj->m_FogEnabled = true; //フォグは黄色っぽく m_PtrObj->m_FogColor = Col4(0.5f, 0.5f, 0.1f, 1.0f); m_PtrObj->m_FogStart = -10.0f; m_PtrObj->m_FogEnd = -30.0f; //シャドウマップ描画データの構築 m_PtrShadowmapObj = make_shared<ShadowmapObject>(); m_PtrShadowmapObj->m_MeshRes = MeshPtr; //描画データの行列をコピー m_PtrShadowmapObj->m_WorldMatrix = World; m_PtrShadowmapObj->m_Camera = GetStage<Stage>()->GetCamera(); }
void BcSphereObject::OnCreate() { CreateBuffers(); //Rigidbodyの初期化 auto PtrGameStage = GetStage<GameStage>(); Rigidbody body; body.m_Owner = GetThis<GameObject>(); body.m_Mass = 1.0f; body.m_Scale = m_Scale; body.m_Quat = m_Qt; body.m_Pos = m_Pos; body.m_CollType = CollType::typeSPHERE; body.m_IsFixed = true; // body.m_IsDrawActive = true; body.SetToBefore(); PtrGameStage->AddRigidbody(body); //行列の定義 Mat4x4 World; World.affineTransformation( m_Scale, Vec3(0, 0, 0), m_Qt, m_Pos ); auto TexPtr = App::GetApp()->GetResource<TextureResource>(m_TextureResName); //描画データの構築 m_PtrObj = make_shared<BcDrawObject>(); m_PtrObj->m_MeshRes = m_SphereMesh; m_PtrObj->m_TextureRes = TexPtr; m_PtrObj->m_WorldMatrix = World; m_PtrObj->m_Camera = GetStage<Stage>()->GetCamera(); m_PtrObj->m_OwnShadowmapActive = false; m_PtrObj->m_ShadowmapUse = true; m_PtrObj->m_FogEnabled = true; //フォグは青色っぽく m_PtrObj->m_FogColor = Col4(0.4f, 0.4f, 0.8f, 1.0f); m_PtrObj->m_FogStart = -10.0f; m_PtrObj->m_FogEnd = -30.0f; //シャドウマップ描画データの構築 m_PtrShadowmapObj = make_shared<ShadowmapObject>(); m_PtrShadowmapObj->m_MeshRes = m_SphereMesh; //描画データの行列をコピー m_PtrShadowmapObj->m_WorldMatrix = World; m_PtrShadowmapObj->m_Camera = GetStage<Stage>()->GetCamera(); }
int main() { // Initialization //-------------------------------------------------------------------------------------- int screenWidth = 800; int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [physics] example - basic rigidbody"); InitPhysics(3); // Initialize physics system with maximum physic objects // Object initialization Transform player = (Transform){(Vector2){(screenWidth - OBJECT_SIZE) / 2, (screenHeight - OBJECT_SIZE) / 2}, 0.0f, (Vector2){OBJECT_SIZE, OBJECT_SIZE}}; AddCollider(PLAYER_INDEX, (Collider){true, COLLIDER_RECTANGLE, (Rectangle){player.position.x, player.position.y, player.scale.x, player.scale.y}, 0}); AddRigidbody(PLAYER_INDEX, (Rigidbody){true, 1.0f, (Vector2){0, 0}, (Vector2){0, 0}, false, false, true, 0.5f, 1.0f}); // Floor initialization // NOTE: floor doesn't need a rigidbody because it's a static physic object, just a collider to collide with other dynamic colliders (with rigidbody) Transform floor = (Transform){(Vector2){0, screenHeight * 0.8f}, 0.0f, (Vector2){screenWidth, screenHeight * 0.2f}}; AddCollider(PLAYER_INDEX + 1, (Collider){true, COLLIDER_RECTANGLE, (Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, 0}); // Object properties initialization float moveSpeed = 6.0f; float jumpForce = 5.0f; bool physicsDebug = false; SetTargetFPS(60); //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // Update object physics // NOTE: all physics detections and reactions are calculated in ApplyPhysics() function (You will live happier :D) ApplyPhysics(PLAYER_INDEX, &player.position); // Check jump button input if (IsKeyDown(KEY_SPACE) && GetRigidbody(PLAYER_INDEX).isGrounded) { // Reset object Y velocity to avoid double jumping cases but keep the same X velocity that it already has SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){GetRigidbody(PLAYER_INDEX).velocity.x, 0}); // Add jumping force in Y axis AddRigidbodyForce(PLAYER_INDEX, (Vector2){0, jumpForce}); } // Check movement buttons input if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) { // Set rigidbody velocity in X based on moveSpeed value and apply the same Y velocity that it already has SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){moveSpeed, GetRigidbody(PLAYER_INDEX).velocity.y}); } else if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) { // Set rigidbody velocity in X based on moveSpeed negative value and apply the same Y velocity that it already has SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){-moveSpeed, GetRigidbody(PLAYER_INDEX).velocity.y}); } // Check debug mode toggle button input if (IsKeyPressed(KEY_P)) physicsDebug = !physicsDebug; //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); // Draw information DrawText("Use LEFT / RIGHT to MOVE and SPACE to JUMP", (screenWidth - MeasureText("Use LEFT / RIGHT to MOVE and SPACE to JUMP", 20)) / 2, screenHeight * 0.20f, 20, LIGHTGRAY); DrawText("Use P to switch DEBUG MODE", (screenWidth - MeasureText("Use P to switch DEBUG MODE", 20)) / 2, screenHeight * 0.3f, 20, LIGHTGRAY); // Check if debug mode is enabled if (physicsDebug) { // Draw every internal physics stored collider if it is active for (int i = 0; i < 2; i++) { if (GetCollider(i).enabled) { DrawRectangleLines(GetCollider(i).bounds.x, GetCollider(i).bounds.y, GetCollider(i).bounds.width, GetCollider(i).bounds.height, GREEN); } } } else { // Draw player and floor DrawRectangleRec((Rectangle){player.position.x, player.position.y, player.scale.x, player.scale.y}, GRAY); DrawRectangleRec((Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, BLACK); } EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadPhysics(); // Unload physic objects CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }