Browse Source

Player controller work in scene

Panagiotis Christopoulos Charitos 11 years ago
parent
commit
3e963f6ec3

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

@@ -21,6 +21,7 @@ struct PhysicsPlayerControllerInitializer
 	F32 m_outerRadius = 0.50;
 	F32 m_height = 1.9;
 	F32 m_stepHeight = 1.9 * 0.33;
+	Vec4 m_position = Vec4(0.0);
 };
 
 /// A player controller that walks the world.

+ 2 - 1
include/anki/scene/PlayerNode.h

@@ -7,6 +7,7 @@
 #define ANKI_SCENE_PLAYER_NODE_H
 
 #include "anki/scene/SceneNode.h"
+#include "anki/Math.h"
 
 namespace anki {
 
@@ -26,7 +27,7 @@ public:
 
 	~PlayerNode();
 
-	ANKI_USE_RESULT Error create(const CString& name);
+	ANKI_USE_RESULT Error create(const CString& name, const Vec4& position);
 
 private:
 	PhysicsPlayerController* m_player = nullptr;

+ 2 - 1
shaders/Pps.frag.glsl

@@ -106,7 +106,8 @@ void main()
 	outColor += hdr;
 #endif
 
-#if GAMMA_CORRECTION_ENABLED
+//#if GAMMA_CORRECTION_ENABLED
+#if 0
 	//outColor = BlendHardLight(vec3(0.7, 0.72, 0.4), outColor);
 	outColor = gammaCorrectionRgb(vec3(0.9, 0.92, 0.75), outColor);
 #endif

+ 1 - 1
src/physics/PhysicsCollisionShape.cpp

@@ -101,7 +101,7 @@ Error PhysicsTriangleSoup::create(Initializer& init,
 		NewtonTreeCollisionAddFace(m_shape, 3, &facePos[0][0], sizeof(Vec3), 0);
 	}
 
-	const I optimize = 1;
+	const I optimize = !ANKI_DEBUG;
 	NewtonTreeCollisionEndBuild(m_shape, optimize);
 
 	return ErrorCode::NONE;

+ 1 - 0
src/physics/PhysicsPlayerController.cpp

@@ -223,6 +223,7 @@ Error PhysicsPlayerController::create(const Initializer& init)
 
 	// Create the kinematic body
 	Mat4 locationMatrix(Mat4::getIdentity());
+	locationMatrix.setTranslationPart(init.m_position.xyz1());
 	m_body = NewtonCreateKinematicBody(world, playerShape, 
 		&toNewton(locationMatrix)[0]);
 	if(m_body == nullptr)

+ 33 - 26
src/scene/PlayerNode.cpp

@@ -31,12 +31,34 @@ public:
 
 		PlayerControllerComponent& playerc = 
 			node.getComponent<PlayerControllerComponent>();
+		const Input& in = node.getSceneGraph().getInput();
+		const F32 ang = toRad(7.0);
 
-		// Process physics move
-		if(playerc.getTimestamp() == getGlobTimestamp())
+		F32 y = in.getMousePosition().y();
+		F32 x = in.getMousePosition().x();
+		if(playerc.getTimestamp() == getGlobTimestamp() 
+			|| y != 0.0 
+			|| x != 0.0)
 		{
 			MoveComponent& move = node.getComponent<MoveComponent>();
-			move.setLocalTransform(playerc.getTransform());
+
+			// Set origin
+			Vec4 origin = playerc.getTransform().getOrigin();
+			origin.y() += 1.9;
+
+			// Set rotation
+			Mat3x4 rot(Euler(ang * y * -11.25, ang * x * -20.0, 0.0));
+
+			rot = move.getLocalRotation().combineTransformations(rot);
+
+			Vec3 newz = rot.getColumn(2).getNormalized();
+			Vec3 newx = Vec3(0.0, 1.0, 0.0).cross(newz);
+			Vec3 newy = newz.cross(newx);
+			rot.setColumns(newx, newy, newz, Vec3(0.0));
+			rot.reorthogonalize();
+
+			// Update move
+			move.setLocalTransform(Transform(origin, rot, 1.0));
 		}
 
 		return ErrorCode::NONE;
@@ -62,11 +84,11 @@ public:
 		PlayerControllerComponent& playerc = 
 			node.getComponent<PlayerControllerComponent>();
 		MoveComponent& move = node.getComponent<MoveComponent>();
-
-		// Process input
 		const Input& in = node.getSceneGraph().getInput();
-		Vec4 moveVec(0.0);
 
+		const F32 speed = 3.5;
+		
+		Vec4 moveVec(0.0);
 		if(in.getKey(KeyCode::W))
 		{
 			moveVec.z() += 1.0;
@@ -87,26 +109,10 @@ public:
 			moveVec.x() += 1.0;
 		}
 
-		const Transform& trf = playerc.getTransform();
-		Vec4 dir = trf.getRotation().getColumn(2).xyz0();
-
-		if(in.getKey(KeyCode::LEFT))
-		{
-			Mat3x4 rot = trf.getRotation();
-			rot.rotateYAxis(toRad(10.0));
-
-			dir = rot.getColumn(2).xyz0();
-		}
-
-		if(in.getKey(KeyCode::RIGHT))
-		{
-			Mat3x4 rot = trf.getRotation();
-			rot.rotateYAxis(toRad(-10.0));
-
-			dir = rot.getColumn(2).xyz0();
-		}
+		Vec4 dir = move.getLocalRotation().getColumn(2).xyz0();
+		dir.y() = 0.0;
+		dir.normalize();
 
-		const F32 speed = 2.0;
 		playerc.setVelocity(
 			moveVec.z() * speed, 
 			moveVec.x() * speed,
@@ -136,7 +142,7 @@ PlayerNode::~PlayerNode()
 }
 
 //==============================================================================
-Error PlayerNode::create(const CString& name)
+Error PlayerNode::create(const CString& name, const Vec4& position)
 {
 	Error err = SceneNode::create(name);
 	if(err)
@@ -146,6 +152,7 @@ Error PlayerNode::create(const CString& name)
 
 	// Create physics object
 	PhysicsPlayerController::Initializer init;
+	init.m_position = position;
 	m_player = getSceneGraph()._getPhysicsWorld().newPlayerController(init);
 
 	SceneComponent* comp;

+ 17 - 9
testapp/Main.cpp

@@ -32,6 +32,7 @@
 #include "anki/physics/PhysicsWorld.h"
 #include "anki/scene/LensFlareComponent.h"
 #include "anki/scene/PlayerNode.h"
+#include "anki/scene/PlayerControllerComponent.h"
 
 using namespace anki;
 
@@ -56,7 +57,7 @@ Error init()
 	MainRenderer& renderer = app->getMainRenderer();
 	ResourceManager& resources = app->getResourceManager();
 
-	scene.setAmbientColor(Vec4(0.1, 0.05, 0.05, 0.0) * 2.1);
+	scene.setAmbientColor(Vec4(0.1, 0.05, 0.05, 0.0) * 0.1);
 
 	if(getenv("PROFILE"))
 	{
@@ -66,13 +67,13 @@ Error init()
 	// camera
 	err = scene.newSceneNode<PerspectiveCamera>("main-camera", cam);
 	if(err) return err;
-	const F32 ang = 45.0;
+	const F32 ang = 55.0;
 	cam->setAll(
 		renderer.getAspectRatio() * toRad(ang),
 		toRad(ang), 0.5, 500.0);
 	cam->getComponent<MoveComponent>().
-		setLocalTransform(Transform(Vec4(0.0, 3.0, -6.0, 0.0),
-		Mat3x4(Euler(toRad(-20.0), toRad(180.0), toRad(0.0))),
+		setLocalTransform(Transform(Vec4(0.0),
+		Mat3x4(Euler(toRad(0.0), toRad(180.0), toRad(0.0))),
 		1.0));
 	scene.setActiveCamera(cam);
 
@@ -269,7 +270,7 @@ Error init()
 	player->setVelocity(0.0, 0.0, 0.0, Vec4(0.0, 0.0, -1.0, 0.0));*/
 
 	PlayerNode* pnode;
-	scene.newSceneNode<PlayerNode>("player", pnode);
+	scene.newSceneNode<PlayerNode>("player", pnode, Vec4(1.0, 3.0, 0.0, 0.0));
 
 	pnode->addChild(cam);
 
@@ -428,7 +429,14 @@ Error mainLoopExtra(App& app, void*, Bool& quit)
 				Transform(pos, Mat3x4::getIdentity(), 1.0));
 		}*/
 
-		SceneNode& l = scene.findSceneNode("player");
+		scene.newSceneNode<ModelNode>(CString(), horse, 
+			"models/crate0/crate0.ankimdl");
+		BodyComponent* bodyc = horse->tryGetComponent<BodyComponent>();
+
+		Vec4 pos(randRange(3, 15), 10, randRange(-6, 8), 0);
+
+		bodyc->setTransform(
+			Transform(pos, Mat3x4::getIdentity(), 1.0));
 	}
 
 	if(in.getKey(KeyCode::F1) == 1)
@@ -518,14 +526,14 @@ end)");
 	}
 #endif
 
-	if(in.getMousePosition() != Vec2(0.0))
+	/*if(in.getMousePosition() != Vec2(0.0))
 	{
 		F32 angY = -ang * in.getMousePosition().x() * mouseSensivity *
 			renderer.getAspectRatio();
 
 		mover->rotateLocalY(angY);
 		mover->rotateLocalX(ang * in.getMousePosition().y() * mouseSensivity);
-	}
+	}*/
 
 	//execStdinScpripts();
 
@@ -582,7 +590,7 @@ Error initSubsystems(int argc, char* argv[])
 
 	//config.set("maxTextureSize", 256);
 
-	config.set("fullscreenDesktopResolution", false);
+	config.set("fullscreenDesktopResolution", true);
 	config.set("debugContext", false);
 
 	app = new App;