Added collision meshes from files

This commit is contained in:
Dane Johnson 2021-01-30 19:39:25 -06:00
parent f8614f8199
commit d84681c23a
33 changed files with 156 additions and 36 deletions

View File

@ -1,3 +1,24 @@
/*
Dane Johnson <dane@danejohnson.org>
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" #include "CollisionShape.h"
CollisionShape::CollisionShape() { CollisionShape::CollisionShape() {
@ -15,3 +36,24 @@ BoxCollisionShape::BoxCollisionShape(float width, float height, float depth) {
CapsuleCollisionShape::CapsuleCollisionShape(float radius, float height) { CapsuleCollisionShape::CapsuleCollisionShape(float radius, float height) {
shape = new btCapsuleShape(radius, 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;
}

View File

@ -1,10 +1,36 @@
/**
@file
@author Dane Johnson <dane@danejohnson.org>
@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 #ifndef COLLISIONSHAPE_H
#define COLLISIONSHAPE_H #define COLLISIONSHAPE_H
#include <btBulletDynamicsCommon.h> #include <btBulletDynamicsCommon.h>
#include "types.h" #include "types.h"
#include "Mesh.h"
/**
Base collision shape, should not be instanced directly
*/
class CollisionShape { class CollisionShape {
public: public:
CollisionShape(); CollisionShape();
@ -16,17 +42,41 @@ private:
class SphereCollisionShape : public CollisionShape { class SphereCollisionShape : public CollisionShape {
public: public:
/**
Creates a collision sphere with a defined radius
@param radius The radius
*/
SphereCollisionShape(float radius); SphereCollisionShape(float radius);
}; };
class BoxCollisionShape : public CollisionShape { class BoxCollisionShape : public CollisionShape {
public: 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); BoxCollisionShape(float width, float height, float depth);
}; };
class CapsuleCollisionShape: public CollisionShape { class CapsuleCollisionShape: public CollisionShape {
public: 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); 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 */ #endif /* COLLISIONSHAPE_H */

View File

@ -105,6 +105,7 @@ public:
protected: protected:
SubMeshList submeshes; SubMeshList submeshes;
virtual void SetupMesh(); virtual void SetupMesh();
friend class MeshCollisionShape;
}; };
#endif /* MESH_H */ #endif /* MESH_H */

View File

@ -21,6 +21,7 @@
They can be instanced on the scene tree as an anchor for some other nodes. They can be instanced on the scene tree as an anchor for some other nodes.
*/ */
#include "Spatial.h" #include "Spatial.h"
#include "Util.h"
Name Spatial::GetType() const {return "Spatial";} Name Spatial::GetType() const {return "Spatial";}
@ -32,6 +33,18 @@ void Spatial::SetTransform(Transform transform) {
this->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() { Spatial *Spatial::Create() {
return new Spatial; return new Spatial;
} }

View File

@ -46,6 +46,16 @@ public:
@param transform The transform property @param transform The transform property
*/ */
void SetTransform(Transform transform); 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 Directly translates the spatial by offset

View File

@ -45,29 +45,29 @@ function init()
couch.Node.GetRoot():AddChild(light:Instance()) couch.Node.GetRoot():AddChild(light:Instance())
local skybox = couch.Skybox.FromFiles( local skybox = couch.Skybox.FromFiles(
"skybox/px.png", "../resources/skybox/px.png",
"skybox/nx.png", "../resources/skybox/nx.png",
"skybox/py.png", "../resources/skybox/py.png",
"skybox/ny.png", "../resources/skybox/ny.png",
"skybox/pz.png", "../resources/skybox/pz.png",
"skybox/nz.png" "../resources/skybox/nz.png"
) )
couch.Node.GetRoot():AddChild(skybox:Instance()) couch.Node.GetRoot():AddChild(skybox:Instance())
local physics_ball_prefab = couch.Rigidbody() 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 = physics_ball_mesh:GetMaterial(0)
material.ambient = BLUE material.ambient = BLUE
material.diffuse = BLUE material.diffuse = BLUE
physics_ball_mesh:SetMaterial(0, material) physics_ball_mesh:SetMaterial(0, material)
physics_ball_prefab:AddChild(physics_ball_mesh); 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() physics_ball = physics_ball_prefab:Instance()
couch.Node.GetRoot():AddChild(physics_ball) couch.Node.GetRoot():AddChild(physics_ball)
make_ground() 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 = character_prefab:GetMaterial(0)
material.ambient = BLUE material.ambient = BLUE
material.diffuse = BLUE material.diffuse = BLUE
@ -78,16 +78,16 @@ function init()
character_body:SetCollisionShape(couch.CapsuleCollisionShape(1.0, 1.0)) character_body:SetCollisionShape(couch.CapsuleCollisionShape(1.0, 1.0))
character_body:SetCharacter(true) character_body:SetCharacter(true)
character_body:AddChild(character_prefab) 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() character = character_body:Instance()
couch.Node.GetRoot():AddChild(character) 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 = cube_prefab:GetMaterial(0)
material.ambient = RED material.ambient = RED
material.diffuse = RED material.diffuse = RED
cube_prefab:SetMaterial(0, material) 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:UniformScale(0.25);
orbiter:Translate(couch.Vector3(1.0, 0.0, 0.0)) orbiter:Translate(couch.Vector3(1.0, 0.0, 0.0))
cube_prefab:AddChild(orbiter) cube_prefab:AddChild(orbiter)
@ -97,7 +97,7 @@ function init()
couch.Node.GetRoot():AddChild(die_cube) couch.Node.GetRoot():AddChild(die_cube)
die_cube:Translate(couch.Vector3(0.0, 0.0, 3.0)) 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) material = ball_prefab:GetMaterial(0)
ball_prefab:SetMaterial(0, material) ball_prefab:SetMaterial(0, material)
ball = ball_prefab:Instance() ball = ball_prefab:Instance()
@ -105,12 +105,12 @@ function init()
ball:Translate(couch.Vector3(0.0, 3.0, 0.0)) 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() trough = trough_prefab:Instance()
couch.Node.GetRoot():AddChild(trough) couch.Node.GetRoot():AddChild(trough)
trough:Translate(couch.Vector3(10.0, 0.0, 0.0)) 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() local scaffold = scaffold_prefab:Instance()
material = scaffold:GetMaterial(0) material = scaffold:GetMaterial(0)
material.alphaScissor = 0.9 material.alphaScissor = 0.9
@ -124,14 +124,18 @@ function init()
couch.Node.GetRoot():AddChild(scaffold) couch.Node.GetRoot():AddChild(scaffold)
scaffold:Translate(couch.Vector3(-3.0, 3.0, 0.0)) 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_prefab = couch.TexturedMesh("../resources/barn.obj", "../resources/paintedwood.jpg", "../resources/barnroof_lowres.png", "../resources/wood_lowres.png")
local barn = barn_prefab:Instance() material = barn_prefab:GetMaterial(0)
material = barn:GetMaterial(0)
material.cullBack = false material.cullBack = false
barn:SetMaterial(0, material) barn_prefab:SetMaterial(0, material)
material = barn:GetMaterial(1) material = barn_prefab:GetMaterial(1)
material.cullBack = false 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) couch.Node.GetRoot():AddChild(barn)
barn:Translate(couch.Vector3(-15.0, 0.0, 0.0)) barn:Translate(couch.Vector3(-15.0, 0.0, 0.0))
end end
@ -202,7 +206,7 @@ function onmousemotion(_, _, relx, rely)
end end
function make_ground() 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() local ground_prefab_transform = ground_prefab:GetTransform()
ground_prefab_transform.position = couch.Vector3(0.0, -2.0, 0.0) ground_prefab_transform.position = couch.Vector3(0.0, -2.0, 0.0)

View File

Before

Width:  |  Height:  |  Size: 149 KiB

After

Width:  |  Height:  |  Size: 149 KiB

View File

Before

Width:  |  Height:  |  Size: 413 KiB

After

Width:  |  Height:  |  Size: 413 KiB

View File

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 62 KiB

View File

Before

Width:  |  Height:  |  Size: 48 KiB

After

Width:  |  Height:  |  Size: 48 KiB

View File

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 28 KiB

View File

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View File

Before

Width:  |  Height:  |  Size: 106 KiB

After

Width:  |  Height:  |  Size: 106 KiB

View File

Before

Width:  |  Height:  |  Size: 92 KiB

After

Width:  |  Height:  |  Size: 92 KiB

View File

Before

Width:  |  Height:  |  Size: 106 KiB

After

Width:  |  Height:  |  Size: 106 KiB

View File

Before

Width:  |  Height:  |  Size: 106 KiB

After

Width:  |  Height:  |  Size: 106 KiB

View File

Before

Width:  |  Height:  |  Size: 107 KiB

After

Width:  |  Height:  |  Size: 107 KiB

View File

Before

Width:  |  Height:  |  Size: 112 KiB

After

Width:  |  Height:  |  Size: 112 KiB

View File

Before

Width:  |  Height:  |  Size: 71 KiB

After

Width:  |  Height:  |  Size: 71 KiB