Jump to content

[Help - C++] 3rd person camera


mike323
 Share

Recommended Posts

I followed Aggror's 3rd person camera tutorial (c++) and simply replaced :

 

playerMesh = Model::Cylinder(16, player);

with

playerMesh = Model::Load("Models/Characters/Barbarian/barbarian.mdl");

playerMesh->SetParent(player);

 

It seems to work as expected in terms of movement but for some reason the camera gets the barbarian model attached to it depending on the cams position or rotation.I also noticed the barbarian is elevated a bit.

 

I have attached my App.cpp (txt format)

 

Any help is greatly appreciated! lol i dont feel like i can continue with the tutorials until i get this solved tongue.png

App.txt

Link to comment
Share on other sites

Im still having an issue,Before i changed SetPosition,It seemed like the player pivot ('player') is loading the same model as 'playerMesh' even though 'player' is the parent of PlayerMesh.Also it seemed like the camera was just inside the models armor thus sort of blocking the camera.When i changed the set position you can no longer see parts of the "cloned" armor,But i still think its there...Can someone help me make sense of this?

 

Here is my code :

#include "App.h"
using namespace Leadwerks;
App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}
App::~App() { delete world; delete window; }
//Camera storage
Vec3 camRotation;
Vec2 centerMouse;
Vec2 mouseDifference;
float mouseSensitivity;
//Player
Model* playerMesh;
Entity* player;
Model* tpsSphere;
//Speeds
Vec3 playerMovement;
float moveSpeed;
float strafeSpeed;
//jump and crouch
float jumpForce;
float tempJumpForce;
bool crouched;
float playerHeight;
float playerCrouchHeight;
//angles
float cameraTopAngle;
float cameraBottomAngle;
float camSmoothing;
//TPS controls
Pivot* fpsPivot;
Pivot* tpsPivot;
float maxCamOffset;
float minCamOffset;
Vec3 oldCamPos;
bool App::Start()
{
//Create a window
window = Window::Create("_9_TPSController", 200, 0, 1024,768);
//Create a context
context = Context::Create(window);
//Create a world
world = World::Create();
//Create a camera
camera = Camera::Create();
camera->Move(0,0,0);
//Hide the mouse cursor
window->HideMouse();
Map::Load("Maps/3P-Test.map");
//Move the mouse to the center of the screen
centerMouse = Vec2(context->GetWidth()/2,context->GetHeight()/2 );
window->SetMousePosition(centerMouse.x, centerMouse.y);
mouseSensitivity =  15;
//Create the player
player = Pivot::Create();
player->SetPosition(0,4,0);
player->SetMass(5);
player->SetPhysicsMode(Entity::CharacterPhysics);

//Create camera pivot
fpsPivot = Pivot::Create();
tpsPivot = Pivot::Create();
//Create a visible mesh
//playerMesh = Model::Cylinder(16, player);
playerMesh = Model::Load("Models/Characters/Barbarian/barbarian.mdl");
//
playerMesh->SetParent(player);
playerMesh->SetPosition(0,0,0);
//playerMesh->SetScale(1,2,1);
//Set some variables
moveSpeed   = 6;
strafeSpeed   = 4;
crouched   = false;
playerHeight  = 1.8;
playerCrouchHeight = 0.8;
jumpForce   = 6;
cameraTopAngle  = -45;
cameraBottomAngle = 80;
camSmoothing  = 8.0;
//Tps camera
maxCamOffset = -8.0;
minCamOffset = 1.5;
return true;
}
bool App::Loop()
{
//Close the window to end the program
if (window->Closed()||window->KeyHit(Key::Escape)) return false;
//Get the mouse movement
Vec3 currentMousePos = window->GetMousePosition();
mouseDifference.x = currentMousePos.x - centerMouse.x;
mouseDifference.y = currentMousePos.y - centerMouse.y;
//Adjust and set the camera rotation
float tempX = camRotation.x + (mouseDifference.y / mouseSensitivity);
if(tempX > cameraTopAngle && tempX < cameraBottomAngle )
camRotation.x = tempX;
camRotation.y += mouseDifference.x / mouseSensitivity;
fpsPivot->SetRotation(camRotation);
window->SetMousePosition(centerMouse.x, centerMouse.y);
//Player Movement
playerMovement.x = (window->KeyDown(Key:) - window->KeyDown(Key::A)) * Time::GetSpeed() * strafeSpeed;
playerMovement.z = (window->KeyDown(Key::W) - window->KeyDown(Key::S)) * Time::GetSpeed() * moveSpeed;
// Check for jumping
tempJumpForce = 0;
if(window->KeyHit(Key::Space) && !(player->GetAirborne()) )
tempJumpForce = jumpForce;
// Check for crouching
if(window->KeyHit(Key::C))
crouched = !crouched;
//Position camera at correct height and playerPosition
player->SetInput(camRotation.y, playerMovement.z, playerMovement.x, tempJumpForce * Time::GetSpeed(), crouched, 1);
//Store player some information
Vec3 tempFpsPos = fpsPivot->GetPosition();
Vec3 playerPos = player->GetPosition();
playerPos.y += (crouched ? playerCrouchHeight : playerHeight);
tempFpsPos.y = Math::Curve(playerPos.y, tempFpsPos.y, camSmoothing * Time::GetSpeed());
tempFpsPos = Vec3(playerPos.x, tempFpsPos.y ,playerPos.z);
fpsPivot->SetPosition(tempFpsPos);
//Position and Rotate the camera to FPS pivot
camera->SetPosition(fpsPivot->GetPosition());
camera->SetRotation(fpsPivot->GetRotation());
//Calculate the furthest TPS pivot position
tpsPivot->SetPosition(fpsPivot->GetPosition());
tpsPivot->SetRotation(fpsPivot->GetRotation());
tpsPivot->Move(0, 0, maxCamOffset, false);
camera->SetPosition(tpsPivot->GetPosition());
//Use a pick to determine where the camera should be
PickInfo pick;
if(world->Pick(fpsPivot->GetPosition(), tpsPivot->GetPosition(), pick, 0, true ))
{
//Store distance
float distance = fpsPivot->GetPosition().DistanceToPoint(pick.position);
printf((String(distance) + "\n").c_str());
//If the tps distance is to small, we switch to FPS view
if(distance < minCamOffset)
{
camera->SetPosition(fpsPivot->GetPosition());
}
else
{
camera->SetPosition(pick.position);
}
}
else
{
camera->SetPosition(tpsPivot->GetPosition());
}
Time::Update();
world->Update();
world->Render();
context->Sync(true);
return true;
}

Link to comment
Share on other sites

I used aggrors c++ tutorial too , but I did not use parenting. Instead I just placed the character(player) in front od camera in my main loop.


jane.model->SetPosition(player->GetPosition());

jane.model->Move(0, 1.5, 0);

jane.model->SetRotation(0, camRotation.y, camrotation.z)

Edited by cassius
  • Upvote 1

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...