Jump to content

Third Person Camera Collision


Arkane
 Share

Recommended Posts

Hi,

 

I'm testing Thid Person Camera. I have searched for tutorial on the forum .

My camera is based on Aggror tutorial but I don't know how to handle camera collision to avoid seeing through walls.

 

I know there are differents solutions to handle that (camera sphere, raycast, ...) :

 

http://www.gamasutra.com/blogs/EricUndersander/20131001/201382/Accurate_Collision_Zoom_for_Cameras.php

 

or

 

http://books.google.fr/books?id=az_Oi9mSSiIC&pg=PA681&lpg=PA681&dq=third+person+camera+collision+sphere&source=bl&ots=CDTY7nJpfL&sig=sDnRf4DgJkJg39vSmNwI8vJlQ2U&hl=fr&sa=X&ei=gxAOVMOrAY2Xar_tgNAF&ved=0CCoQ6AEwATgK#v=onepage&q=third%20person%20camera%20collision%20sphere&f=false

 

What's the best method ?

 

Can someone help me posting code to show me how to handle that problem ?

 

Thanks !

Link to comment
Share on other sites

Here is a snippet from my third person camera code, pretty sure I derived it from Aggrors Tut (can't remember). Obviously a lot of the variables are declared somewhere else the section you are after is the Pick. Note you need to set the characters pick mode to 0 else the pick will hit the model and wont work.

 

GetCameraTarget() returns an entity that the camera is tracking

 

Let me know if you have any questions

 

void ChaseCamera::Update(void)
{
BaseCamera::Update();
if (GetCameraTarget())
{
Vec3 lastPosition;
Vec3 lastRotation;
BaseObject* object = (BaseObject*)GetCameraTarget()->GetUserData();
//Calculate the furthest TPS pivot position
tpsPivot->SetPosition(GetCameraTarget()->GetPosition());
Vec3 pickPos = GetCameraTarget()->GetPosition();
pickPos.y += GetHeight();
Vec3 rot = tpsPivot->GetRotation();
Vec3 pos = tpsPivot->GetPosition();
pos.y += GetHeight();
tpsPivot->SetPosition(pos);
rot.y = -characterYaw;
rot.x = -characterPitch;
tpsPivot->SetRotation(rot);
entity->Point(tpsPivot);
tpsPivot->Move(0.0, GetHeight(), -GetCamDistance(), false);
//Use a pick to determine where the camera should be
PickInfo pick;
GetCameraTarget()->SetPickMode(0);
if (World::GetCurrent()->Pick(pickPos, tpsPivot->GetPosition(), pick, 0.0F, true)) tpsPivot->SetPosition(pick.position);
GetCameraTarget()->SetPickMode(Entity::SpherePick);
//Apply position
lastPosition.x = Math::Curve(tpsPivot->GetPosition().x, entity->GetPosition().x, (GetSmoothness()) / Time::GetSpeed());
lastPosition.y = Math::Curve(tpsPivot->GetPosition().y, entity->GetPosition().y, (GetSmoothness()) / Time::GetSpeed());
lastPosition.z = Math::Curve(tpsPivot->GetPosition().z, entity->GetPosition().z, (GetSmoothness()) / Time::GetSpeed());
entity->SetPosition(lastPosition);
}
}

trindieprod.png?dl=0spacer.png?dl=0steam-icon.png?dl=0twitter-icon.png?dl=0spacer.png?dl=0
Link to comment
Share on other sites

So I found a workaround to handle my camera and not see through walls when I'm in a house for example.

I've changed the pick radius and it works. After I will try to improve my camera rotation around the player.

 

Based on Aggrors tutorial :

 

//Update camera position
Vec3 playerPosition = player->GetPosition();
Vec3 cameraPosition = fpsPivot->GetPosition();
float cameraHeight = crouching ? crouchHeight : playerHeight;
cameraPosition.y = Math::Curve(playerPosition.y + cameraHeight, cameraPosition.y, cameraSmoothing);
cameraPosition = Vec3(playerPosition.x, cameraPosition.y, playerPosition.z);
fpsPivot->SetPosition(cameraPosition);

//Position and rotation of FPS camera
camera->SetPosition(fpsPivot->GetPosition());

//Postion and rotation of TPS camera
tpsPivot->SetPosition(fpsPivot->GetPosition());
tpsPivot->Move(0, 1, maxCameraOffset);

//Checking if Space key is pressed
if (window->KeyHit(Key::Space))
{
isFpsCam = !isFpsCam;
}

// Checking camera mode
if (isFpsCam)
{
camera->SetPosition(fpsPivot->GetPosition());
camera->SetRotation(fpsPivot->GetRotation());
}
else
{
camera->SetRotation(fpsPivot->GetRotation());
camera->SetPosition(tpsPivot->GetPosition());
}

//Raycast to check TPS Camera obstacle
PickInfo pick;
if (world->Pick(fpsPivot->GetPosition(), tpsPivot->GetPosition(), pick, 0.5, true) && !isFpsCam)
{
camera->SetPosition(pick.position);
}

 

You can adjust pick radius a bit to have a better behaviour.

Link to comment
Share on other sites

I've found a good tutorial about TP camera collision/occlusion. It's for Unity but I think we can make the same in leadwerks.

 

http://www.3dbuzz.com/training/view/3rd-person-character-system/enhanced-character-system/11-occlusion-and-collision-overview

 

There is more videos about third person camera here :

 

http://www.3dbuzz.com/training/view/3rd-person-character-system/enhanced-character-system

 

Can someone tell me if it is possible to port it to leadwerks ?

 

Thanks!

Link to comment
Share on other sites

I saw in camera.h that there is localfrustumpoints and frustumpoints. What is the difference ? What if the order of each point in those arrays ?

Can I use them to get the near camera plane and the far camera plane of the view frustrum ?

 

Maybe is it easier to use Camera::GetRange() and Camera::GetFOV () then calculate the four points of the near camera plane ?

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...