Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Novi

3rd person camera from GameTutorials

This topic is 5560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In lesson "Camera part 4 (3rd person)" (GameTotorials) main object turns together with camera, but how to make, to always he was turn backwards to camera ?

Share this post


Link to post
Share on other sites
Advertisement
For a very basic camera system, just translate the camera x units back and y units up from the player avitar and comupte a look at transform from this position to the players position. This should work nicely for a simple 3dperson camera. But it would move very riggidly and things would get in the way of the camera. I''ll outline a more advance camera system below to give you some implementation ideas for a more advance system, but note that this is a pretty tedious task, but the results are well worth it.




For more of an advance camera system i look at the camera like any other object in the world (similar to the player, enemies, or any other dynamic object in the scene). So the camera will interact with the world like any other dynamic object; it must obey the collision detection rules (with some exceptions to help maintian constant visiblity of the player). This type of implementation would require a dynamic object-to-static world collision detection system, although dynamic-to-dynamic would also be helpful. Collision is used to check for visiblity between the player and camera as well as forcing the camera to abide by the restrictions of the world (this maintains a level of emersion for the player) .

Additionalliy, Like any other autonomous object in the world the camera has a set of objectives it tries to achive. These objectives will outline the way our camera behaves.

-most of the time, we want it to always look at the player (focused object)
-some times we want it to look a bit in front of the player (so that we can see what the player is looking at....this is helpful when the player is moving forward and turning at the same time)
-we want it to try to aways maintain a position behind the camera.
-the further away from this ''desired'' position the faster the camera will move to try to reach this point.
-if there is something that is between the camera and the ''focused object'' (obstruction) then we must try to adjust the cameras position so that their is nothing between the ''focused object'' and camera.

noticie that the above set of behaviors for our camera assume that there is no initial obstruction between the camera and the player and that there is will always be some point where the focused object is visible from the current cameras position.



Share this post


Link to post
Share on other sites
ok i''ll explain a simpler camera model
from the 3rd pers camera tutorial youll shall eliminate the move camera by arrow function. now lets the object ur lookin at has a vPos position vector and a vView viso make the camera''s view always vPos.
so do this: calculate the cameras movement (RotateViewByMouse func). set the objects view to the camers view and the cameras view 2 vPos. and when u render the object find the angle between the cameras view and it''s positon (acos(dot(...)) and rotate.

that''s it

/*ilici*/

Share this post


Link to post
Share on other sites
here is the code

camera update part

gCam.SetDist(100.0f); // set dist from obj
gCam.LookAt(vPos + CVector3(0, 32, 0));

gCam.Update();
gCam.Look();

if (!lockCam)
{
vCamView = gCam.View();
vCamPos = gCam.Position();
}

render obj part

CVector3 vView = CVector3(vCamView.x, 0, vCamView.z);
CVector3 vPosition = CVector3(vCamPos.x, 0, vCamPos.z);

CVector3 vRot = Normalize(vView - vPosition);

float prod = Dot(vRot, vLastRot);
CVector3 vCross = Cross(vRot, vLastRot);

float ang;
if (prod >= 1.0f || prod <= -1.0f) ang = 0;
else
{
if (vCross.y < 0) ang = acos(prod) * 180 / PI;
else ang = -acos(prod) * 180 / PI;
}

rot += ang;
if (rot > 360) rot = rot - 360;
if (rot < -360) rot = rot + 360;


//rotate by rot

moving obj part:

CVector3 vVector = Normalize(vCamView - vCamPos);

CVector3 vVect = CVector3(vCamView.x - vCamPos.x, 0, vCamView.z - vCamPos.z);

CVector3 vStrafe = Normalize(Cross(vVect, CVector3(0, 1, 0)) );

float dx, dz;

switch (dir)
{
case FORWARD:
dx = vVector.x * kSpeed;
dz = vVector.z * kSpeed;
break;
case BACKWARD:
dx = -vVector.x * kSpeed;
dz = -vVector.z * kSpeed;
break;
case LEFT:
dx = -vStrafe.x * kSpeed;
dz = -vStrafe.z * kSpeed;
break;
case RIGHT:
dx = vStrafe.x * kSpeed;
dz = vStrafe.z * kSpeed;
break;
case UPDATE:
dx = 0;
dz = 0;
break;
}


/*ilici*/

Share this post


Link to post
Share on other sites
here is the code

camera update part

gCam.SetDist(100.0f); // set dist from obj
gCam.LookAt(vPos + CVector3(0, 32, 0));

gCam.Update();
gCam.Look();

if (!lockCam)
{
vCamView = gCam.View();
vCamPos = gCam.Position();
}

render obj part

CVector3 vView = CVector3(vCamView.x, 0, vCamView.z);
CVector3 vPosition = CVector3(vCamPos.x, 0, vCamPos.z);

CVector3 vRot = Normalize(vView - vPosition);

float prod = Dot(vRot, vLastRot);
CVector3 vCross = Cross(vRot, vLastRot);

float ang;
if (prod >= 1.0f || prod <= -1.0f) ang = 0;
else
{
if (vCross.y < 0) ang = acos(prod) * 180 / PI;
else ang = -acos(prod) * 180 / PI;
}

rot += ang;
if (rot > 360) rot = rot - 360;
if (rot < -360) rot = rot + 360;


//rotate by rot

moving obj part:

CVector3 vVector = Normalize(vCamView - vCamPos);

CVector3 vVect = CVector3(vCamView.x - vCamPos.x, 0, vCamView.z - vCamPos.z);

CVector3 vStrafe = Normalize(Cross(vVect, CVector3(0, 1, 0)) );

float dx, dz;

switch (dir)
{
case FORWARD:
dx = vVector.x * kSpeed;
dz = vVector.z * kSpeed;
break;
case BACKWARD:
dx = -vVector.x * kSpeed;
dz = -vVector.z * kSpeed;
break;
case LEFT:
dx = -vStrafe.x * kSpeed;
dz = -vStrafe.z * kSpeed;
break;
case RIGHT:
dx = vStrafe.x * kSpeed;
dz = vStrafe.z * kSpeed;
break;
case UPDATE:
dx = 0;
dz = 0;
break;
}


/*ilici*/

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!