Selaa lähdekoodia

Working on physics

Panagiotis Christopoulos Charitos 12 vuotta sitten
vanhempi
sitoutus
84387f24ee

+ 5 - 0
include/anki/resource/Model.h

@@ -193,6 +193,11 @@ public:
 	{
 	{
 		return visibilityShape;
 		return visibilityShape;
 	}
 	}
+
+	const btCollisionShape* getCollisionShape() const
+	{
+		return collShape.get();
+	}
 	/// @}
 	/// @}
 
 
 	void load(const char* filename);
 	void load(const char* filename);

+ 3 - 0
src/physics/MotionState.cpp

@@ -38,6 +38,9 @@ void MotionState::setWorldTransform(const btTransform& worldTrans)
 {
 {
 	worldTransform = worldTrans;
 	worldTransform = worldTrans;
 	needsUpdate = true;
 	needsUpdate = true;
+
+	/// XXX
+	sync();
 }
 }
 
 
 //==============================================================================
 //==============================================================================

+ 1 - 1
src/renderer/Dbg.cpp

@@ -92,7 +92,7 @@ void Dbg::run()
 	}
 	}
 
 
 	// XXX
 	// XXX
-	if(1)
+	if(0)
 	{
 	{
 		Vec3 tri[3] = {
 		Vec3 tri[3] = {
 			Vec3{10.0 , 7.0, -2.0}, 
 			Vec3{10.0 , 7.0, -2.0}, 

+ 4 - 1
src/resource/Model.cpp

@@ -4,6 +4,7 @@
 #include "anki/resource/MeshLoader.h"
 #include "anki/resource/MeshLoader.h"
 #include "anki/resource/ShaderProgramResource.h"
 #include "anki/resource/ShaderProgramResource.h"
 #include "anki/misc/Xml.h"
 #include "anki/misc/Xml.h"
+#include "anki/physics/Converters.h"
 #include <btBulletCollisionCommon.h>
 #include <btBulletCollisionCommon.h>
 
 
 namespace anki {
 namespace anki {
@@ -260,7 +261,7 @@ void Model::load(const char* filename)
 		XmlElement rootEl = doc.getChildElement("model");
 		XmlElement rootEl = doc.getChildElement("model");
 
 
 		// <collisionShape>
 		// <collisionShape>
-		XmlElement collEl = doc.getChildElement("collisionShape");
+		XmlElement collEl = rootEl.getChildElementOptional("collisionShape");
 		if(collEl)
 		if(collEl)
 		{
 		{
 			std::string type = collEl.getChildElement("type").getText();
 			std::string type = collEl.getChildElement("type").getText();
@@ -272,6 +273,8 @@ void Model::load(const char* filename)
 			}
 			}
 			else if(type == "box")
 			else if(type == "box")
 			{
 			{
+				Vec3 extend = valEl.getVec3();
+				collShape.reset(new btBoxShape(toBt(extend)));
 			}
 			}
 			else if(type == "mesh")
 			else if(type == "mesh")
 			{
 			{

+ 25 - 0
src/scene/ModelNode.cpp

@@ -2,6 +2,8 @@
 #include "anki/scene/SceneGraph.h"
 #include "anki/scene/SceneGraph.h"
 #include "anki/resource/Model.h"
 #include "anki/resource/Model.h"
 #include "anki/resource/Skeleton.h"
 #include "anki/resource/Skeleton.h"
+#include "anki/physics/RigidBody.h"
+#include "anki/physics/PhysicsWorld.h"
 
 
 namespace anki {
 namespace anki {
 
 
@@ -192,6 +194,23 @@ ModelNode::ModelNode(
 		patches.push_back(mpn);
 		patches.push_back(mpn);
 		++i;
 		++i;
 	}
 	}
+
+	// Load rigid body
+	if(model->getCollisionShape() != nullptr)
+	{
+		RigidBody::Initializer init;
+		init.mass = 1.0;
+		init.shape = const_cast<btCollisionShape*>(model->getCollisionShape());
+		init.startTrf.getOrigin().y() = 20.0;
+		init.movable = this;
+
+		RigidBody* body;
+		
+		getSceneGraph().getPhysics().newPhysicsObject<RigidBody>(
+			body, init);
+
+		sceneNodeProtected.rigidBodyC = body;
+	}
 }
 }
 
 
 //==============================================================================
 //==============================================================================
@@ -201,6 +220,12 @@ ModelNode::~ModelNode()
 	{
 	{
 		getSceneGraph().deleteSceneNode(patch);
 		getSceneGraph().deleteSceneNode(patch);
 	}
 	}
+
+	if(sceneNodeProtected.rigidBodyC)
+	{
+		getSceneGraph().getPhysics().deletePhysicsObject(
+			sceneNodeProtected.rigidBodyC);
+	}
 }
 }
 
 
 //==============================================================================
 //==============================================================================

+ 11 - 5
testapp/Main.cpp

@@ -42,10 +42,6 @@ void initPhysics()
 {
 {
 	SceneGraph& scene = SceneGraphSingleton::get();
 	SceneGraph& scene = SceneGraphSingleton::get();
 
 
-	scene.getPhysics().setDebugDrawer(
-		new PhysicsDebugDrawer(
-			&MainRendererSingleton::get().getDbg().getDebugDrawer()));
-
 	btCollisionShape* groundShape = new btBoxShape(
 	btCollisionShape* groundShape = new btBoxShape(
 	    btVector3(btScalar(50.), btScalar(50.), btScalar(50.)));
 	    btVector3(btScalar(50.), btScalar(50.), btScalar(50.)));
 
 
@@ -243,6 +239,11 @@ void init()
 	horse->setLocalTransform(Transform(Vec3(-2, 0, 0), Mat3::getIdentity(),
 	horse->setLocalTransform(Transform(Vec3(-2, 0, 0), Mat3::getIdentity(),
 		0.7));
 		0.7));
 
 
+
+	scene.newSceneNode(horse, "crate", "models/crate0/crate0.ankimdl");
+	horse->setLocalTransform(Transform(Vec3(2, 10.0, 0), Mat3::getIdentity(),
+		1.0));
+
 	// barrel
 	// barrel
 	/*ModelNode* redBarrel = new ModelNode(
 	/*ModelNode* redBarrel = new ModelNode(
 		"red_barrel", &scene, nullptr, MoveComponent::MF_NONE, 
 		"red_barrel", &scene, nullptr, MoveComponent::MF_NONE, 
@@ -261,7 +262,12 @@ void init()
 #endif
 #endif
 	scene.load("maps/sponza/master.ankiscene");
 	scene.load("maps/sponza/master.ankiscene");
 
 
-	//initPhysics();
+	// Physics debug
+	scene.getPhysics().setDebugDrawer(
+		new PhysicsDebugDrawer(
+			&MainRendererSingleton::get().getDbg().getDebugDrawer()));
+
+	initPhysics();
 
 
 	// Sectors
 	// Sectors
 #if 0
 #if 0

+ 2 - 2
tools/texture/ankitexture.py

@@ -226,9 +226,9 @@ def identify_image(in_file):
 	stdout_str = proc.stdout.read()
 	stdout_str = proc.stdout.read()
 
 
 	# Make sure the colorspace is what we want
 	# Make sure the colorspace is what we want
-	reg = re.search(r"Colorspace: (.*)", stdout_str)
+	"""reg = re.search(r"Colorspace: (.*)", stdout_str)
 	if not reg or reg.group(1) != "RGB":
 	if not reg or reg.group(1) != "RGB":
-		raise Exception("Something is wrong with the colorspace")
+		raise Exception("Something is wrong with the colorspace")"""
 
 
 	# Get the size of the iamge
 	# Get the size of the iamge
 	reg = re.search(r"Geometry: ([0-9]*)x([0-9]*)\+", stdout_str)
 	reg = re.search(r"Geometry: ([0-9]*)x([0-9]*)\+", stdout_str)