• Advertisement
Sign in to follow this  

Character Move Forward after Camera Perspective

This topic is 1230 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

Hi Guys,

 

when I hold the forward key down my game character does not move directly forward it moves diagonally.

 

This Is happening because I say the forward direction is the +ve Z axis but when the camera perspective is applied to my scene the perspective makes my character look like it is moving diagonal.

 

How do I make my character move straight when camera persective is applied?

 

cheers

Share this post


Link to post
Share on other sites
Advertisement

When an object is moved in direction of the line of sight of the camera, but the object has a sideway offset to the line of sight, then the perspective depth-shortening not only makes the object appear smaller the farer it is moved, but also makes the offset smaller. This is expected behavior, and changing it will confuse the players. Assume that the scenery has edges parallel to the line of sight. They naturally seek to the vanishing point of perspective. But the player's avatar, although simply being moved forward, does not follow the same rule?!

 

For completeness, and because I may have misunderstood your question ... if you actually want to move along a projector line, you have to use not the line of sight but those projector line that passes through the object. I.e. compute the difference vector from the camera to the object of interest, if needed then project it onto the floor, and normalize the result. Use that vector as movement direction.

Share this post


Link to post
Share on other sites

Hi haegarr,

 

cheers for the reply.

 

I  want to calculate in world space the vector that will move my character straight forward after the perspective is applied. I think your 2nd suggestion is along the correct lines but I have tried it before & again now but it does not work. It won't work because those positions are calculated in world space before perspective is applied. I'm created a virtual joystick with my mouse & moving along the z axis when forward direction is applied does not feel right,

 

any other suggestions?

 

cheers

Share this post


Link to post
Share on other sites

A projector line denotes all locations that are mapped onto the same pixel on screen. Before perspective projection the projector line has an angle with the line of sight (if not coincident), and after perspective projection the projector line is parallel to the line of sight. This is presumably exactly what you want: The object of interest is projected to the same location on screen (letting the extent of the object aside). Therefore, moving along the projector line will do the job.

 

The projector line of interest is, in your case, chosen as those passing through the origin of the object of interest. A projector line ever passes through the origin of the camera in case of perspective projection. Now, having the camera position given in world space and having the object position given in world space, the difference vector between them can be calculated and is also given in world space. That's again what you presumably want.

 

The difference vector will usually point into the ground, so projecting it onto the ground makes is more or less a 2D vector; in fact it will be parallelized with the ground, what is fine because the character should not sink into the ground.

 

I don't see a flaw in this reasoning. If it does not work for you, I currently assume that your code is wrong somewhere. Perhaps you can show us the relevant code snippets...

 

BTW: A test would be to render the difference vector as a line in world space. It should appear as a point on screen.

Share this post


Link to post
Share on other sites

Are you just trying to implement a first person view/camera? If so, you either need to simply add the camera's look vector to position (for a floating camera); or need to cross multiply look with up (0,1,0), then the cross result with up, to get forward (for a character that can move and look in somewhat different directions).

Share this post


Link to post
Share on other sites

Hi haegarr,

 

I agree with what you are saying about the line of projection but when I use this my charater now moves diagonally outwards towards the window edge as opposed to innwards when I use the z axis as the direction, I want it to move on the window y axis, any thoughts?

 

 

Hi beans222,

 

sorry it is not a first person view I am moving characters on a sports pitch

 

 

cheers

Edited by Cacks

Share this post


Link to post
Share on other sites

If I understand your question correctly, this may work.

1) Optionally: Update your character's rotation:

 

mRotation.y += mRotationSpeed * GAMETIMER->DeltaTime();

2) Optionally: Update your character's position
 

mPosition.x += sin(mRotation.y) * mMovementSpeed * GAMETIMER->DeltaTime();
mPosition.z += cos(mRotation.y) * mMovementSpeed * GAMETIMER->DeltaTime();

3) Set the position and angle of your camera, according to your character's position and rotation.

Position:
x = mPosition.x - sin(-mRotation.y) * mZoom
y = mPosition.y - sin(your camera's roll (rotation around x axis)) * mZoom
z = mPosition.z + cos(-mRotation.y)* mZoom

Rotation: That's up to you. Good luck ;)

Depending on your logic, you may adjust the sign, add/substract pi and change cos/sin.

Edit: Don't forget about possible overflow. Make sure your angles stay under 2 * pi.
Edited by Nick C.

Share this post


Link to post
Share on other sites

If you're having trouble with the more elegant solution, I would be inclined to make another variable (as an angle relative to North), which then controls both the camera angle (relative to North) and the character movement direction.  Then, however you want to control your camera, just have it adjust that variable instead.  

 

This way, the character is always running at that angle relative to North, and the camera is always pointing in that direction too.  Then you'll just have to update your camera position based on that angle and the character position (assuming you're using a follow camera), and whatever distance and angle to the ground you wanted.

 

I'm a pretty hacky programmer though, so take my advice with a grain of salt.  I just like to make things work, and it's not necessarily how you should ideally do it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement