3D 3d 3rd person projection

phillipPbor

Member
im using Tristan Batchler's 3d tutioriels to exparament on projections
3rd person
far away while projecting at the player.
I wanted to make a klonoa project,


(I mean that how can I make the comra focus at the player while moving?)

var cx = obj_player.x;
var cy = obj_player.y;
var cz = obj_player.z + obj_player.height;
var cdir = obj_player.direction;
var cpitch = obj_player.pitch;
d3d_set_projection(cx,cy,cz,
cx + lengthdir_x(1,cdir),
cy + lengthdir_y(1,cdir),
cz + lengthdir_y(1,cpitch),
0,0,1);
 
Last edited:
The first triplet of arguments is the point that the camera is looking from. In other words, it's the position of the camera. This point should be equal to the player's position plus the camera's look direction scaled by some amount (likely a negative number).
If your camera's look direction is based on a yaw (obj_player.direction) and a pitch (obj_player.pitch), then the look direction can be computed as (x,y,z):

dcos(yaw)*dcos(pitch)
-dsin(yaw)*dcos(pitch)
dsin(pitch)

Remember, you want to scale that vector by some amount, probably a negative number ( and most likely a number with an absolute value much greater than 1, which is what you've used as the length argument in your lengthdir functions).

The second triplet of arguments is the point that the camera is looking at. This point should be equal to the character's position.

The third triplet of arguments is the camera's up vector. As long as the camera's pitch stays within the range -90 to 90 (not including -90 or 90), then using an up vector of (0,0,1) should be okay. However if your camera can point direction up or down, or turn upside down, then you can compute the up vector, given the camera's pitch and yaw, like this:

-dcos(yaw)*dsin(pitch)
dsin(yaw)*dsin(pitch)
dcos(pitch)

EDIT: If the camera is located behind the player, and you want to be able to see what is in front of the player without the player blocking the view, then the camera's position and look at position should be offset by some amount. A good idea could be make that offset amount equal to some multiple of the camera's up vector.

Problems that are not addressed here are a) the camera clipping through walls or floor/ceiling, and if this is pertinent to your game, b) drawing a reticle that indicates what direction the player is pointing toward.
 
Last edited:

phillipPbor

Member
The first triplet of arguments is the point that the camera is looking from. In other words, it's the position of the camera. This point should be equal to the player's position plus the camera's look direction scaled by some amount (likely a negative number).
If your camera's look direction is based on a yaw (obj_player.direction) and a pitch (obj_player.pitch), then the look direction can be computed as (x,y,z):

dcos(yaw)*dcos(pitch)
-dsin(yaw)*dcos(pitch)
dsin(pitch)

Remember, you want to scale that vector by some amount, probably a negative number ( and most likely a number with an absolute value much greater than 1, which is what you've used as the length argument in your lengthdir functions).

The second triplet of arguments is the point that the camera is looking at. This point should be equal to the character's position.

The third triplet of arguments is the camera's up vector. As long as the camera's pitch stays within the range -90 to 90 (not including -90 or 90), then using an up vector of (0,0,1) should be okay. However if your camera can point direction up or down, or turn upside down, then you can compute the up vector, given the camera's pitch and yaw, like this:

-dcos(yaw)*dsin(pitch)
dsin(yaw)*dsin(pitch)
dcos(pitch)

EDIT: If the camera is located behind the player, and you want to be able to see what is in front of the player without the player blocking the view, then the camera's position and look at position should be offset by some amount. A good idea could be make that offset amount equal to some multiple of the camera's up vector.

Problems that are not addressed here are a) the camera clipping through walls or floor/ceiling, and if this is pertinent to your game, b) drawing a reticle that indicates what direction the player is pointing toward.

in code version please.
 
No problem. Seeing an example in code can often be illuminating.

But do be sure to try to understand what is happening in the code.

For now, I'm just going to cover the most basic version of the various setups I described above. Not bothering to offset the camera and look position to see around the player. Not going into details about avoiding camera clipping with the world, and not worrying about any visual indication of the player's facing.
Code:
d3d_set_projection(
    cx + dist *  dcos(yaw)*dcos(pitch),
    cy + dist * -dsin(yaw)*dcos(pitch),
    cz + dist *  dsin(pitch),
    cx,
    cy,
    cz,
   -dcos(yaw)*dsin(pitch),
    dsin(yaw)*dsin(pitch),
    dcos(pitch)
);
Make sure all the following variables have appropriate values. So, (cx,cy, and cz) is the point that your camera is looking at. "dist" is the distance at which the camera orbits the player. This should be a negative number if you want the camera to be behind the player. "yaw" is rotationg around the z axis, and "pitch" is rotation around the y axis (how much the camera tilts up and down).

If your camera can't point straight up or down, or turn upside down, you can leave the up vector as (0,0,1).
 
Last edited:

phillipPbor

Member
No problem. Seeing an example in code can often be illuminating.

But do be sure to try to understand what is happening in the code.

For now, I'm just going to cover the most basic version of the various setups I described above. Not bothering to offset the camera and look position to see around the player. Not going into details about avoiding camera clipping with the world, and not worrying about any visual indication of the player's facing.
Code:
d3d_set_projection(
    cx + dist *  dcos(yaw)*dcos(pitch),
    cy + dist * -dsin(yaw)*dcos(pitch),
    cz + dist *  dsin(pitch),
    cx,
    cy,
    cz,
   -dcos(yaw)*dsin(pitch),
    dsin(yaw)*dsin(pitch),
    dcos(pitch)
);
Make sure all the following variables have appropriate values. So, (cx,cy, and cz) is the point that your camera is looking at. "dist" is the distance at which the camera orbits the player. This should be a negative number if you want the camera to be behind the player. "yaw" is rotationg around the z axis, and "pitch" is rotation around the y axis (how much the camera tilts up and down).

If your camera can't point straight up or down, or turn upside down, you can leave the up vector as (0,0,1).
i don't know how to make those variables, don't know where to code at.
is it okay if you can putt those down for me?

I mean the examples in case I haven't learn how to.
 
Ok. But we are getting close to the point where I won't know how to explain this any clearer.

You can pretty much start the same way your code was originally structured.
Code:
var cx = obj_player.x;
var cy = obj_player.y;
var cz = obj_player.z + obj_player.height;
var yaw = obj_player.direction;
var pitch = obj_player.pitch;
var dist = -100;  //<--  -100 is an arbitrary choice by me. 
d3d_set_projection(
   cx + dist *  dcos(yaw)*dcos(pitch),
   cy + dist * -dsin(yaw)*dcos(pitch),
   cz + dist *  dsin(pitch),
   cx,
   cy,
   cz,
   -dcos(yaw)*dsin(pitch),
   dsin(yaw)*dsin(pitch),
   dcos(pitch)
);
 

phillipPbor

Member
Ok. But we are getting close to the point where I won't know how to explain this any clearer.

You can pretty much start the same way your code was originally structured.
Code:
var cx = obj_player.x;
var cy = obj_player.y;
var cz = obj_player.z + obj_player.height;
var yaw = obj_player.direction;
var pitch = obj_player.pitch;
var dist = -100;  //<--  -100 is an arbitrary choice by me.
d3d_set_projection(
   cx + dist *  dcos(yaw)*dcos(pitch),
   cy + dist * -dsin(yaw)*dcos(pitch),
   cz + dist *  dsin(pitch),
   cx,
   cy,
   cz,
   -dcos(yaw)*dsin(pitch),
   dsin(yaw)*dsin(pitch),
   dcos(pitch)
);

thank you.. I will be forever gratful
 
Top