About Store Forum Documentation Contact



Post Reply 
Character turning in the camera direction
Author Message
Kiekos Offline
Member

Post: #1
Character turning in the camera direction
I want to improve the standard camera but it doesn't seem to be as easy as I thought.

When I turn the camera (so that I'm facing the character) and press W, the character goes forward despite the camera facing him.

I want the character to turn around and start going forward in the oposite direction, so the effect would be that pressing W and turning the camera would affect the direction of character movement.

Basically, is something like this already included in the engine? I couldn't find anything on the forums.
(This post was last modified: 05-05-2013 08:00 PM by Kiekos.)
05-05-2013 07:47 PM
Find all posts by this user Quote this message in a reply
Ozmodian Offline
Member

Post: #2
RE: Character turning in the camera direction
Does the tutorial 14 - Game Basics - 14 - Camera Modes not show you what you are looking for? The default "VIEW_TPP" seems to be what you are looking for.
05-05-2013 08:00 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #3
RE: Character turning in the camera direction
No, not really. I don't want the camera to be 'fixed'.

I want to be able to rotate it any angle I want and if I rotate so that it faces the player (I'm looking at his face) and I press W, the character turns around (I'm looking at his back now) and starts going forward the direction the camera is facing.

Just like in most of the mmo games.
(This post was last modified: 05-05-2013 08:14 PM by Kiekos.)
05-05-2013 08:11 PM
Find all posts by this user Quote this message in a reply
Ozmodian Offline
Member

Post: #4
RE: Character turning in the camera direction
Hi Kiekos,

Got it, so you basically want the ISO mode but when you press W or S, you want the character to move based on where the camera is. That is pretty simple.

In that same 14 - Game Basics - 14 - Camera Modes, in the Class Player just after if(!action), if you add the following code, that should do it for you.


Code:
if(View==VIEW_ISO &&  (Kb.b(KB_W) || Kb.b(KB_S)))
         {
            angle.x = Cam.yaw;
         }

Make sure when you are trying that tutorial, press TAB once to get to ISO mode.
(This post was last modified: 05-06-2013 02:38 AM by Ozmodian.)
05-06-2013 02:36 AM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #5
RE: Character turning in the camera direction
Thanks, that's exactly what I wanted! So simple smile

I still have a few questions tho. Is there a way to include that 'turning around' animation which is default to Q and E? It looks kind of weird when he instantly switches and starts going the direction of camera, if you know what I mean. It should be smooth.

Code looks like this right now:
Code:
if(!action)
    {
       input.move.z=Kb.b(KB_W)+Kb.b(KB_A)+Kb.b(KB_D)+Kb.b(KB_S);
    }

   if (Kb.b(KB_W))
       angle.x = Cam.yaw;

   if (Kb.b(KB_S))
       angle.x = Cam.yaw + PI;

   if (Kb.b(KB_A))
       angle.x = Cam.yaw + PI_2;

   if (Kb.b(KB_D))
       angle.x = Cam.yaw - PI_2;

   if (Kb.b(KB_W) && Kb.b(KB_A))
       angle.x = Cam.yaw + PI_4;

   if (Kb.b(KB_W) && Kb.b(KB_D))
       angle.x = Cam.yaw - PI_4;

   if (Kb.b(KB_S) && Kb.b(KB_A))
       angle.x = Cam.yaw + PI - PI_4;

   if (Kb.b(KB_S) && Kb.b(KB_D))
       angle.x = Cam.yaw + PI + PI_4;
(This post was last modified: 05-06-2013 11:21 PM by Kiekos.)
05-06-2013 12:34 PM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #6
RE: Character turning in the camera direction
You could do something similar to
angle.x += (Cam.yaw calculations) * (turning speed)
where instead of setting it, you could increment it each frame. (it would be a little more complex than that)
05-06-2013 11:23 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #7
RE: Character turning in the camera direction
I thought of something like that but I failed every time. I just can't get this working. I've tried modifying this:

Code:
if (Kb.b(KB_W))
       angle.x = Cam.yaw;

to something like this:

Code:
if (Kb.b(KB_W))
    if (angle.x != Cam.yaw)
        input.turn.x = *something here*;

I'd really like to get it working. Any hints? :3

EDIT: Would it work if I checked the angle difference in between the angle.x and Cam.yaw and increment it until they're even?
(This post was last modified: 05-06-2013 11:31 PM by Kiekos.)
05-06-2013 11:26 PM
Find all posts by this user Quote this message in a reply
fatcoder Offline
Member

Post: #8
RE: Character turning in the camera direction
Kiekos Wrote:Would it work if I checked the angle difference in between the angle.x and Cam.yaw and increment it until they're even?

Yes, do something like angle.x = Lerp(angle.x, Cam.yaw, step) where step is a Flt you increment each frame using Time.d().

Oh, and you may need to store angle.x so it doesn't change each frame in your Lerp.
(This post was last modified: 05-07-2013 01:43 AM by fatcoder.)
05-07-2013 01:40 AM
Find all posts by this user Quote this message in a reply
Ozmodian Offline
Member

Post: #9
RE: Character turning in the camera direction
Yeah, what fatcoder said. Instead of setting the angle.x just use Linear Interpolation (I LOVE all the awesome macros Esenthel has).

Just replace the 0.05 with however fast you want the character to turn. Anywhere between 0.1 and 0.05 seems about right to me.


Code:
if(View==VIEW_ISO &&  (Kb.b(KB_W) || Kb.b(KB_S)))
         {
            //angle.x = Cam.yaw;
            angle.x = Lerp(angle.x, Cam.yaw, 0.05);
         }
05-07-2013 03:46 AM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #10
RE: Character turning in the camera direction
Yep, that's what I needed, thanks guys! Now there's no need for those +/- PI_4 parts as the movements are mixed by the engine. There's just one more problem.

Code:
if (Kb.b(KB_W))
       angle.x = Lerp(angle.x, Cam.yaw, 0.1f);

   if (Kb.b(KB_S))      
       angle.x = Lerp(angle.x, Cam.yaw + PI, 0.1f);

   if (Kb.b(KB_A))
       angle.x = Lerp(angle.x, Cam.yaw + PI_2, 0.1f);

   if (Kb.b(KB_D))
       angle.x = Lerp(angle.x, Cam.yaw - PI_2, 0.1f);

While pressing S & D button the effect is the same as W & A. Singular buttons work perfectly but combination of S & D doesn't. That's probably because of the way Lerp works.

EDIT: Yep, when pressing two buttons it goes to the average angle between those two and average for PI and -PI_2 is the same as for 0 and PI_2... Any ideas?
(This post was last modified: 05-07-2013 10:14 AM by Kiekos.)
05-07-2013 10:05 AM
Find all posts by this user Quote this message in a reply
treavor09 Offline
Member

Post: #11
RE: Character turning in the camera direction
is there anyway to have a multipress event like

if(kb.b(KB_W)+(KB_A)) just a concept idea not sure if you can implement it so easily
05-07-2013 03:51 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #12
RE: Character turning in the camera direction
I've tried using:

Code:
if (Kb.b(KB_W) && Kb.b(KB_A)) *something*

but it doesn't seem to do the trick.

EDIT: Issue fixed and it's working almost perfectly. The only thing I don't like about it is the fact that the Lerp allows the character to turn only one way and there's a certain point where instead of turning 45°, the character turns 315° (the other way). If I add PI to the angle - character turns left, if I substract PI - the character turns right. But still there's that certain point where he will do a barrel roll, lol.

Is there anything like Reversed Lerp or some sort of different feature I could use?

Code:
if (Kb.b(KB_W))
       angle.x = Lerp(angle.x, Cam.yaw, 0.1f);
  
   if (Kb.b(KB_A))
       angle.x = Lerp(angle.x, Cam.yaw + PI_2, 0.1f);

   if (Kb.b(KB_S) && Kb.b(KB_D))
       angle.x = Lerp(angle.x, Cam.yaw - PI_2 - PI_4, 0.1f);
   else
   {
       if (Kb.b(KB_S))      
          angle.x = Lerp(angle.x, Cam.yaw + PI, 0.1f);

       if (Kb.b(KB_D))
          angle.x = Lerp(angle.x, Cam.yaw - PI_2, 0.1f);
   }

In this set up the point is when switching from S to S & D.

I mean, I could do 8 IFs (for every direction) but I don't think that will fix the issue.
(This post was last modified: 05-07-2013 04:47 PM by Kiekos.)
05-07-2013 04:10 PM
Find all posts by this user Quote this message in a reply
SamNainocard Offline
Member

Post: #13
RE: Character turning in the camera direction
If you want to prevent character to spin too much to turn, instead of 350' 349' 348'.. 0 to 350' 351' 352... 359' 0', don't know what it called, hope you get the idea.

Try
PHP Code:
Flt    angle_x=angle.x;
{
    
Flt  diff=angle_x-Cam.yaw;
    
angle_x=Cam.yaw;

    while (
diff<-PI)    {diff+=PI2angle_x-=PI2;}
    while (
diffPI)    {diff-=PI2angle_x+=PI2;}
    
AdjustValTarget(angle.xangle_x, <turn_rate>); // or Lerp

(This post was last modified: 05-08-2013 07:22 AM by SamNainocard.)
05-08-2013 07:19 AM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #14
RE: Character turning in the camera direction
@SamNainocard, that's a nice solution, thank you! All W,S,A,D directions work perfectly now and the character doesn't turn like crazy anymore but I can't seem to get mixed movements working (W&A, W&D, etc).
05-08-2013 11:12 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #15
RE: Character turning in the camera direction
instead of using above code I recommend something like this:
Code:
flt angle, target_angle, turn_speed; // initial values

flt delta=AngleDelta(angle, target_angle);
flt target_angle=angle+delta;
AdjustValTarget(angle, target_angle, Time.d()*turn_speed);
05-12-2013 05:16 PM
Find all posts by this user Quote this message in a reply
Post Reply