Browse Source

Player controller work in scene

Panagiotis Christopoulos Charitos 11 years ago
parent
commit
871532cecb

+ 1 - 1
include/anki/physics/PhysicsPlayerController.h

@@ -91,7 +91,7 @@ private:
 
 	// Motion state
 	Bool8 m_updated = true;
-	Transform m_trf;
+	Transform m_trf = {Transform::getIdentity()};
 	Mat4 m_prevTrf = {Mat4::getIdentity()};
 
 	static constexpr F32 MIN_RESTRAINING_DISTANCE = 1.0e-2;

+ 1 - 1
include/anki/scene/Light.h

@@ -26,7 +26,7 @@ class Light: public SceneNode
 public:
 	Light(SceneGraph* scene);
 
-	virtual ~Light();
+	~Light();
 
 	ANKI_USE_RESULT Error create(
 		const CString& name, 

+ 12 - 0
include/anki/scene/PlayerNode.h

@@ -10,14 +10,26 @@
 
 namespace anki {
 
+// Forward
+class PhysicsPlayerController;
+
 /// @addtogroup scene
 /// @{
 
 /// Player scene node. It uses input and physics to move inside the world.
 class PlayerNode: public SceneNode
 {
+	friend class PlayerNodeFeedbackComponent;
+
 public:
 	PlayerNode(SceneGraph* scene);
+
+	~PlayerNode();
+
+	ANKI_USE_RESULT Error create(const CString& name);
+
+private:
+	PhysicsPlayerController* m_player = nullptr;
 };
 /// @}
 

+ 4 - 8
src/scene/Light.cpp

@@ -68,33 +68,29 @@ Error Light::create(const CString& name,
 	// Move component
 	comp = getSceneAllocator().newInstance<MoveComponent>(this);
 	if(comp == nullptr) return ErrorCode::OUT_OF_MEMORY; 
-	comp->setAutomaticCleanup(true);
 
-	err = addComponent(comp);
+	err = addComponent(comp, true);
 	if(err) return err;
 
 	// Light component
 	comp = getSceneAllocator().newInstance<LightComponent>(this, type);
 	if(comp == nullptr) return ErrorCode::OUT_OF_MEMORY; 
-	comp->setAutomaticCleanup(true);
 
-	err = addComponent(comp);
+	err = addComponent(comp, true);
 	if(err) return err;
 
 	// Feedback component
 	comp = getSceneAllocator().newInstance<LightFeedbackComponent>(this);
 	if(comp == nullptr) return ErrorCode::OUT_OF_MEMORY; 
-	comp->setAutomaticCleanup(true);
 
-	err = addComponent(comp);
+	err = addComponent(comp, true);
 	if(err) return err;
 
 	// Spatial component
 	comp = getSceneAllocator().newInstance<SpatialComponent>(this, shape);
 	if(comp == nullptr) return ErrorCode::OUT_OF_MEMORY; 
-	comp->setAutomaticCleanup(true);
 
-	err = addComponent(comp);
+	err = addComponent(comp, true);
 	if(err) return err;
 
 	return err;

+ 140 - 0
src/scene/PlayerNode.cpp

@@ -0,0 +1,140 @@
+// Copyright (C) 2014, Panagiotis Christopoulos Charitos.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#include "anki/scene/PlayerNode.h"
+#include "anki/scene/SceneGraph.h"
+#include "anki/scene/MoveComponent.h"
+#include "anki/physics/PhysicsPlayerController.h"
+#include "anki/physics/PhysicsWorld.h"
+#include "anki/input/Input.h"
+
+namespace anki {
+
+//==============================================================================
+// PlayerNodeFeedbackComponent                                                 =
+//==============================================================================
+
+/// Feedback component.
+class PlayerNodeFeedbackComponent final: public SceneComponent
+{
+public:
+	PlayerNodeFeedbackComponent(SceneNode* node)
+	:	SceneComponent(SceneComponent::Type::NONE, node)
+	{}
+
+	Error update(SceneNode& node, F32, F32, Bool& updated) override
+	{
+		PlayerNode& pnode = static_cast<PlayerNode&>(node);
+
+		// Process physics move
+		const Transform& trf = pnode.m_player->getTransform(updated);
+		if(updated)
+		{
+			MoveComponent& move = node.getComponent<MoveComponent>();
+			move.setLocalTransform(trf);
+		}
+
+		// Process input
+		const Input& in = node.getSceneGraph().getInput();
+		Vec4 moveVec(0.0);
+
+		if(in.getKey(KeyCode::W))
+		{
+			moveVec.z() += -1.0;
+		}
+
+		if(in.getKey(KeyCode::A))
+		{
+			moveVec.x() += -1.0;
+		}
+
+		if(in.getKey(KeyCode::S))
+		{
+			moveVec.z() += 1.0;
+		}
+
+		if(in.getKey(KeyCode::D))
+		{
+			moveVec.x() += 1.0;
+		}
+
+		//if(moveVec != Vec4(0.0))
+		{
+			const F32 speed = 2.0;
+			pnode.m_player->setVelocity(
+				moveVec.x() * speed, 
+				moveVec.z() * speed,
+				0.0, 
+				Vec4(0.0, 0.0, -1.0, 0.0));
+		}
+
+		return ErrorCode::NONE;
+	}
+};
+
+//==============================================================================
+// PlayerNode                                                                  =
+//==============================================================================
+
+//==============================================================================
+PlayerNode::PlayerNode(SceneGraph* scene)
+:	SceneNode(scene)
+{}
+
+//==============================================================================
+PlayerNode::~PlayerNode()
+{
+	if(m_player)
+	{
+		m_player->setMarkedForDeletion();	
+	}
+}
+
+//==============================================================================
+Error PlayerNode::create(const CString& name)
+{
+	Error err = SceneNode::create(name);
+	if(err)
+	{
+		return err;
+	}
+
+	// Create physics object
+	PhysicsPlayerController::Initializer init;
+	m_player = getSceneGraph()._getPhysicsWorld().newPlayerController(init);
+
+	SceneComponent* comp;
+
+	// Feedback component
+	comp = getSceneAllocator().newInstance<PlayerNodeFeedbackComponent>(this);
+	if(comp == nullptr)
+	{
+		return ErrorCode::OUT_OF_MEMORY; 
+	}
+
+	err = addComponent(comp, true);
+	if(err)
+	{
+		return err;
+	}
+
+	// Move component
+	comp = getSceneAllocator().newInstance<MoveComponent>(this);
+	if(comp == nullptr)
+	{
+		return ErrorCode::OUT_OF_MEMORY; 
+	}
+
+	err = addComponent(comp, true);
+	if(err)
+	{
+		return err;
+	}
+
+	return err;
+}
+
+} // end namespace anki
+

+ 6 - 2
testapp/Main.cpp

@@ -31,6 +31,7 @@
 #include "anki/core/Config.h"
 #include "anki/physics/PhysicsWorld.h"
 #include "anki/scene/LensFlareComponent.h"
+#include "anki/scene/PlayerNode.h"
 
 using namespace anki;
 
@@ -262,10 +263,13 @@ Error init()
 	}
 #endif
 
-	PhysicsPlayerController::Initializer initp;
+	/*PhysicsPlayerController::Initializer initp;
 	auto player = scene._getPhysicsWorld().newPlayerController(initp);
 	player->moveToPosition(Vec4(5.0, 2.0, 0.0, 0.0));
-	player->setVelocity(0.0, 0.0, 0.0, Vec4(0.0, 0.0, -1.0, 0.0));
+	player->setVelocity(0.0, 0.0, 0.0, Vec4(0.0, 0.0, -1.0, 0.0));*/
+
+	PlayerNode* pnode;
+	scene.newSceneNode<PlayerNode>("player", pnode);
 
 	PhysicsCollisionShape::Initializer initc;
 	auto box =