diff --git a/core/CollisionShape.cpp b/core/CollisionShape.cpp index 3195f68..38e08c2 100644 --- a/core/CollisionShape.cpp +++ b/core/CollisionShape.cpp @@ -1,3 +1,24 @@ +/* + Dane Johnson + + LICENSE + + Couch Copyright (C) 2021 Dane Johnson + + This program comes with ABSOLUTELY NO WARRANTY; without event the + implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for details at + https://www.gnu.org/licenses/gpl-3.0.html + + This is free software, and you are welcome to redistribute it + under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + DESCRIPTION + + Defines various collision shapes for rigidbodies and trigger areas +*/ #include "CollisionShape.h" CollisionShape::CollisionShape() { @@ -15,3 +36,24 @@ BoxCollisionShape::BoxCollisionShape(float width, float height, float depth) { CapsuleCollisionShape::CapsuleCollisionShape(float radius, float height) { shape = new btCapsuleShape(radius, height); } + +MeshCollisionShape::MeshCollisionShape(Mesh *mesh) { + btCompoundShape *compoundShape = new btCompoundShape(); + + for (SubMesh *submesh : mesh->submeshes) { + btTriangleIndexVertexArray *indexVertexArray = new btTriangleIndexVertexArray( + submesh->indices.size(), + (int*) &submesh->indices[0], + sizeof(Index), + submesh->vertices.size(), + (float*) &submesh->vertices[0], + sizeof(Vertex) + ); + compoundShape->addChildShape( + btTransform::getIdentity(), + new btBvhTriangleMeshShape(indexVertexArray, true) + ); + } + + shape = compoundShape; +} diff --git a/core/CollisionShape.h b/core/CollisionShape.h index 4089b8d..a50c7a2 100644 --- a/core/CollisionShape.h +++ b/core/CollisionShape.h @@ -1,10 +1,36 @@ +/** + @file + @author Dane Johnson + + @section LICENSE + + Couch Copyright (C) 2021 Dane Johnson + + This program comes with ABSOLUTELY NO WARRANTY; without event the + implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for details at + https://www.gnu.org/licenses/gpl-3.0.html + + This is free software, and you are welcome to redistribute it + under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + @section DESCRIPTION + + Defines various collision shapes for rigidbodies and trigger areas +*/ #ifndef COLLISIONSHAPE_H #define COLLISIONSHAPE_H #include #include "types.h" +#include "Mesh.h" +/** + Base collision shape, should not be instanced directly +*/ class CollisionShape { public: CollisionShape(); @@ -16,17 +42,41 @@ private: class SphereCollisionShape : public CollisionShape { public: + /** + Creates a collision sphere with a defined radius + @param radius The radius + */ SphereCollisionShape(float radius); }; class BoxCollisionShape : public CollisionShape { public: + /** + Creates a collision cube with the desired width, height, and depth + @param width The desired width + @param height The desired height + @param depth The desired depth + */ BoxCollisionShape(float width, float height, float depth); }; class CapsuleCollisionShape: public CollisionShape { public: + /** + Creates a collision shape that is a cylinder with height, capped with a hemisphere of radius + @param radius The hemisphere radius + @param height The cylinder height + */ CapsuleCollisionShape(float radius, float height); }; +class MeshCollisionShape: public CollisionShape { +public: + /** + Creates a concave mesh with the same faces as mesh + @param mesh The mesh who's faces to use + */ + MeshCollisionShape(Mesh *mesh); +}; + #endif /* COLLISIONSHAPE_H */ diff --git a/core/Mesh.h b/core/Mesh.h index 8e8e0bb..3c5d494 100644 --- a/core/Mesh.h +++ b/core/Mesh.h @@ -105,6 +105,7 @@ public: protected: SubMeshList submeshes; virtual void SetupMesh(); + friend class MeshCollisionShape; }; #endif /* MESH_H */ diff --git a/core/Spatial.cpp b/core/Spatial.cpp index 4ff9142..c1a2f6a 100644 --- a/core/Spatial.cpp +++ b/core/Spatial.cpp @@ -1,26 +1,27 @@ /* - Dane Johnson + Dane Johnson - LICENSE + LICENSE - Couch Copyright (C) 2021 Dane Johnson + Couch Copyright (C) 2021 Dane Johnson - This program comes with ABSOLUTELY NO WARRANTY; without event the - implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - See the GNU General Public License for details at - https://www.gnu.org/licenses/gpl-3.0.html + This program comes with ABSOLUTELY NO WARRANTY; without event the + implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for details at + https://www.gnu.org/licenses/gpl-3.0.html - This is free software, and you are welcome to redistribute it - under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. + This is free software, and you are welcome to redistribute it + under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. - DESCRIPTION + DESCRIPTION - A spatial is a node with a transform property, i.e. position, scale or rotation. - They can be instanced on the scene tree as an anchor for some other nodes. + A spatial is a node with a transform property, i.e. position, scale or rotation. + They can be instanced on the scene tree as an anchor for some other nodes. */ #include "Spatial.h" +#include "Util.h" Name Spatial::GetType() const {return "Spatial";} @@ -32,6 +33,18 @@ void Spatial::SetTransform(Transform transform) { this->transform = transform; } +Transform Spatial::GetGlobalTransform() { + Util::Die("Global transform is not implemented yet.\n" + "I don't fully understand the maths yet!"); + return Transform(); +} + +void Spatial::SetGlobalTransform(Transform globalTransform) { + // TODO + Util::Die("Global transform is not implemented yet.\n" + "I don't fully understand the maths yet!"); +} + Spatial *Spatial::Create() { return new Spatial; } diff --git a/core/Spatial.h b/core/Spatial.h index 4b8ae00..59a4b45 100644 --- a/core/Spatial.h +++ b/core/Spatial.h @@ -46,6 +46,16 @@ public: @param transform The transform property */ void SetTransform(Transform transform); + /** + Gets the translation with relation to the world origin + @returns the global transform + */ + Transform GetGlobalTransform(); + /** + Sets the transform with relation to the world origin + @param globalTransform The global transform property + */ + void SetGlobalTransform(Transform globalTransform); /** Directly translates the spatial by offset diff --git a/demo/exampleworld/main.lua b/demo/exampleworld/main.lua index d2f34dc..e9da61f 100644 --- a/demo/exampleworld/main.lua +++ b/demo/exampleworld/main.lua @@ -45,29 +45,29 @@ function init() couch.Node.GetRoot():AddChild(light:Instance()) local skybox = couch.Skybox.FromFiles( - "skybox/px.png", - "skybox/nx.png", - "skybox/py.png", - "skybox/ny.png", - "skybox/pz.png", - "skybox/nz.png" + "../resources/skybox/px.png", + "../resources/skybox/nx.png", + "../resources/skybox/py.png", + "../resources/skybox/ny.png", + "../resources/skybox/pz.png", + "../resources/skybox/nz.png" ) couch.Node.GetRoot():AddChild(skybox:Instance()) local physics_ball_prefab = couch.Rigidbody() - local physics_ball_mesh = couch.Mesh.FromFile("ball.obj") + local physics_ball_mesh = couch.Mesh.FromFile("../resources/ball.obj") material = physics_ball_mesh:GetMaterial(0) material.ambient = BLUE material.diffuse = BLUE physics_ball_mesh:SetMaterial(0, material) physics_ball_prefab:AddChild(physics_ball_mesh); - physics_ball_prefab:Translate(couch.Vector3(0.0, 30.0, -10.0)) + physics_ball_prefab:Translate(couch.Vector3(-14.0, 30.0, -5.0)) physics_ball = physics_ball_prefab:Instance() couch.Node.GetRoot():AddChild(physics_ball) make_ground() - local character_prefab = couch.Mesh.FromFile("capsule.obj") + local character_prefab = couch.Mesh.FromFile("../resources/capsule.obj") material = character_prefab:GetMaterial(0) material.ambient = BLUE material.diffuse = BLUE @@ -78,16 +78,16 @@ function init() character_body:SetCollisionShape(couch.CapsuleCollisionShape(1.0, 1.0)) character_body:SetCharacter(true) character_body:AddChild(character_prefab) - character_body:Translate(couch.Vector3(0.0, 3.0, 0.0)) + character_body:Translate(couch.Vector3(-15.0, 3.0, 0.0)) character = character_body:Instance() couch.Node.GetRoot():AddChild(character) - local cube_prefab = couch.Mesh.FromFile("cube.obj") + local cube_prefab = couch.Mesh.FromFile("../resources/cube.obj") material = cube_prefab:GetMaterial(0) material.ambient = RED material.diffuse = RED cube_prefab:SetMaterial(0, material) - local orbiter = couch.Mesh.FromFile("ball.obj") + local orbiter = couch.Mesh.FromFile("../resources/ball.obj") orbiter:UniformScale(0.25); orbiter:Translate(couch.Vector3(1.0, 0.0, 0.0)) cube_prefab:AddChild(orbiter) @@ -97,7 +97,7 @@ function init() couch.Node.GetRoot():AddChild(die_cube) die_cube:Translate(couch.Vector3(0.0, 0.0, 3.0)) - local ball_prefab = couch.Mesh.FromFile("ball.obj") + local ball_prefab = couch.Mesh.FromFile("../resources/ball.obj") material = ball_prefab:GetMaterial(0) ball_prefab:SetMaterial(0, material) ball = ball_prefab:Instance() @@ -105,12 +105,12 @@ function init() ball:Translate(couch.Vector3(0.0, 3.0, 0.0)) - local trough_prefab = couch.TexturedMesh("trough.obj", "wood_lowres.png") + local trough_prefab = couch.TexturedMesh("../resources/trough.obj", "../resources/wood_lowres.png") trough = trough_prefab:Instance() couch.Node.GetRoot():AddChild(trough) trough:Translate(couch.Vector3(10.0, 0.0, 0.0)) - local scaffold_prefab = couch.TexturedMesh("scaffold.obj", "grate_floor_lowres.png", "railing.png") + local scaffold_prefab = couch.TexturedMesh("../resources/scaffold.obj", "../resources/grate_floor_lowres.png", "../resources/railing.png") local scaffold = scaffold_prefab:Instance() material = scaffold:GetMaterial(0) material.alphaScissor = 0.9 @@ -124,14 +124,18 @@ function init() couch.Node.GetRoot():AddChild(scaffold) scaffold:Translate(couch.Vector3(-3.0, 3.0, 0.0)) - local barn_prefab = couch.TexturedMesh("barn.obj", "paintedwood.jpg", "barnroof_lowres.png", "wood_lowres.png") - local barn = barn_prefab:Instance() - material = barn:GetMaterial(0) + local barn_prefab = couch.TexturedMesh("../resources/barn.obj", "../resources/paintedwood.jpg", "../resources/barnroof_lowres.png", "../resources/wood_lowres.png") + material = barn_prefab:GetMaterial(0) material.cullBack = false - barn:SetMaterial(0, material) - material = barn:GetMaterial(1) + barn_prefab:SetMaterial(0, material) + material = barn_prefab:GetMaterial(1) material.cullBack = false - barn:SetMaterial(1, material) + barn_prefab:SetMaterial(1, material) + local barn_body = couch.Rigidbody() + barn_body:SetCollisionShape(couch.MeshCollisionShape(barn_prefab)) + barn_body:SetMass(0.0) + barn_body:AddChild(barn_prefab) + local barn = barn_body:Instance() couch.Node.GetRoot():AddChild(barn) barn:Translate(couch.Vector3(-15.0, 0.0, 0.0)) end @@ -202,7 +206,7 @@ function onmousemotion(_, _, relx, rely) end function make_ground() - local ground_prefab = couch.TexturedMesh("ground.obj", "grass_lowres.png") + local ground_prefab = couch.TexturedMesh("../resources/ground.obj", "../resources/grass_lowres.png") local ground_prefab_transform = ground_prefab:GetTransform() ground_prefab_transform.position = couch.Vector3(0.0, -2.0, 0.0) diff --git a/demo/exampleworld/ball.mtl b/demo/resources/ball.mtl similarity index 100% rename from demo/exampleworld/ball.mtl rename to demo/resources/ball.mtl diff --git a/demo/exampleworld/ball.obj b/demo/resources/ball.obj similarity index 100% rename from demo/exampleworld/ball.obj rename to demo/resources/ball.obj diff --git a/demo/exampleworld/barn.mtl b/demo/resources/barn.mtl similarity index 100% rename from demo/exampleworld/barn.mtl rename to demo/resources/barn.mtl diff --git a/demo/exampleworld/barn.obj b/demo/resources/barn.obj similarity index 100% rename from demo/exampleworld/barn.obj rename to demo/resources/barn.obj diff --git a/demo/exampleworld/barnroof_lowres.png b/demo/resources/barnroof_lowres.png similarity index 100% rename from demo/exampleworld/barnroof_lowres.png rename to demo/resources/barnroof_lowres.png diff --git a/demo/exampleworld/capsule.mtl b/demo/resources/capsule.mtl similarity index 100% rename from demo/exampleworld/capsule.mtl rename to demo/resources/capsule.mtl diff --git a/demo/exampleworld/capsule.obj b/demo/resources/capsule.obj similarity index 100% rename from demo/exampleworld/capsule.obj rename to demo/resources/capsule.obj diff --git a/demo/exampleworld/container.png b/demo/resources/container.png similarity index 100% rename from demo/exampleworld/container.png rename to demo/resources/container.png diff --git a/demo/exampleworld/cube.mtl b/demo/resources/cube.mtl similarity index 100% rename from demo/exampleworld/cube.mtl rename to demo/resources/cube.mtl diff --git a/demo/exampleworld/cube.obj b/demo/resources/cube.obj similarity index 100% rename from demo/exampleworld/cube.obj rename to demo/resources/cube.obj diff --git a/demo/exampleworld/grass_lowres.png b/demo/resources/grass_lowres.png similarity index 100% rename from demo/exampleworld/grass_lowres.png rename to demo/resources/grass_lowres.png diff --git a/demo/exampleworld/grate_floor_lowres.png b/demo/resources/grate_floor_lowres.png similarity index 100% rename from demo/exampleworld/grate_floor_lowres.png rename to demo/resources/grate_floor_lowres.png diff --git a/demo/exampleworld/ground.mtl b/demo/resources/ground.mtl similarity index 100% rename from demo/exampleworld/ground.mtl rename to demo/resources/ground.mtl diff --git a/demo/exampleworld/ground.obj b/demo/resources/ground.obj similarity index 100% rename from demo/exampleworld/ground.obj rename to demo/resources/ground.obj diff --git a/demo/exampleworld/paintedwood.jpg b/demo/resources/paintedwood.jpg similarity index 100% rename from demo/exampleworld/paintedwood.jpg rename to demo/resources/paintedwood.jpg diff --git a/demo/exampleworld/railing.png b/demo/resources/railing.png similarity index 100% rename from demo/exampleworld/railing.png rename to demo/resources/railing.png diff --git a/demo/exampleworld/scaffold.mtl b/demo/resources/scaffold.mtl similarity index 100% rename from demo/exampleworld/scaffold.mtl rename to demo/resources/scaffold.mtl diff --git a/demo/exampleworld/scaffold.obj b/demo/resources/scaffold.obj similarity index 100% rename from demo/exampleworld/scaffold.obj rename to demo/resources/scaffold.obj diff --git a/demo/exampleworld/skybox/nx.png b/demo/resources/skybox/nx.png similarity index 100% rename from demo/exampleworld/skybox/nx.png rename to demo/resources/skybox/nx.png diff --git a/demo/exampleworld/skybox/ny.png b/demo/resources/skybox/ny.png similarity index 100% rename from demo/exampleworld/skybox/ny.png rename to demo/resources/skybox/ny.png diff --git a/demo/exampleworld/skybox/nz.png b/demo/resources/skybox/nz.png similarity index 100% rename from demo/exampleworld/skybox/nz.png rename to demo/resources/skybox/nz.png diff --git a/demo/exampleworld/skybox/px.png b/demo/resources/skybox/px.png similarity index 100% rename from demo/exampleworld/skybox/px.png rename to demo/resources/skybox/px.png diff --git a/demo/exampleworld/skybox/py.png b/demo/resources/skybox/py.png similarity index 100% rename from demo/exampleworld/skybox/py.png rename to demo/resources/skybox/py.png diff --git a/demo/exampleworld/skybox/pz.png b/demo/resources/skybox/pz.png similarity index 100% rename from demo/exampleworld/skybox/pz.png rename to demo/resources/skybox/pz.png diff --git a/demo/exampleworld/trough.mtl b/demo/resources/trough.mtl similarity index 100% rename from demo/exampleworld/trough.mtl rename to demo/resources/trough.mtl diff --git a/demo/exampleworld/trough.obj b/demo/resources/trough.obj similarity index 100% rename from demo/exampleworld/trough.obj rename to demo/resources/trough.obj diff --git a/demo/exampleworld/wood_lowres.png b/demo/resources/wood_lowres.png similarity index 100% rename from demo/exampleworld/wood_lowres.png rename to demo/resources/wood_lowres.png