About Store Forum Documentation Contact



Post Reply 
Make Character turn to Mouse Direction w/o Running
Author Message
Chris Offline
Member

Post: #1
Make Character turn to Mouse Direction w/o Running
Hi,

In the modified Camera Modes tutorial, I want to add the functionality of having a character anglei rotate in the direction of the mouse away from the character, without running in that direction, for all three view types. I can get the character to run without stopping in that direction in FPS views with the following code:

Code:
if (Ms.b(0))
{
    Vec pos,dir;
    ScreenToPosDir(Ms.pos,pos,dir);
    actionMoveDir(dir);
}

Is there a way to make it so the character just turns with angeli=-1 or 1 accordingly in the direction of the mouse, which works in all FPS, Third & Isometric views? (and not continue running with the actionMove?)

Sorry if this was a confused explanation,
Chris
12-04-2009 04:36 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: Make Character turn to Mouse Direction w/o Running
player.angle
12-04-2009 05:07 AM
Find all posts by this user Quote this message in a reply
Chris Offline
Member

Post: #3
RE: Make Character turn to Mouse Direction w/o Running
Thanks for your quick response Esenthel, i'm still having problems:
Code:
if (Ms.b(0))
{
    Flt  max=DegToRad(900)*Tm.d(),
    dx =Ms.dir_ds.x*1.7,
    dy =Ms.dir_ds.y*1.7*Ms.inv();
    angle.x-=Mid(dx,-max,max);
    angle.y+=Mid(dy,-max,max);
    input.diri.z=1; // Go forward in that direction while mouse is held
}

This works exactly how I want it in First Person and Third Person camera modes, but in Isometric mode, the player doesn't run in the direction of the mouse. Instead he seems to only run in what looks like the correct position from the viewing frustum in the previous two camera modes.

In a previous game I had just set the directional vector to (target - viewpoint). But again, I can't seem to get this to work in the isometric mode.

Chris
12-04-2009 12:45 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #4
RE: Make Character turn to Mouse Direction w/o Running
I think you must be making calculations somewhere incorrectly.
calculations of the desired direction.

you should consider treating as if the player is standing on a flat plane.
cast a ray from the screen to the plane, and calculate the delta between contact point and player position.
12-04-2009 05:05 PM
Find all posts by this user Quote this message in a reply
Chris Offline
Member

Post: #5
RE: Make Character turn to Mouse Direction w/o Running
Thanks for your response, I'm still having problems getting this to behave properly despite several hours of trying now. This is my current code, which isn't working correctly:

Code:
if (View==VIEW_ISO && Ms.b(0))
{
    Vec     pos,dir ;
    ScreenToPosDir(Ms.pos,pos,dir); // convert screen mouse position
    PhysHit phys_hit;
    if (Physics.ray(pos,dir*ViewportActive.range,&phys_hit))
    {
        Vec facing =  phys_hit.plane.p - Players[0].pos();
        if (facing.x < 0)
            input.anglei.x = 1;
        else
            input.anglei.x = -1;    
    }
}

I also spent a while playing with AngleDelta(), and normalising the angles but just couldn't do it. (And couldn't draw text to print the output from this location in this mode ~ nothing would appear). I think i'm having problems with not using UP/Cross functions the same way as i'm used to.
12-04-2009 08:46 PM
Find all posts by this user Quote this message in a reply
Chris Offline
Member

Post: #6
RE: Make Character turn to Mouse Direction w/o Running
I finally managed to make it work after a whole day & night of reading up on vectors, quaternions, cross products, normalisation and uhh whatever else.

If anyone else wants to do this, in the end it was just a case of adding !Ms.br(0) into the actionBreak() chain:
Code:
if (action)
{
    if (Kb.b(KB_W) || Kb.b(KB_S) || Kb.b(KB_A) || Kb.b(KB_D) || Kb.b(KB_Q) || Kb.b(KB_E) || !Ms.br(0)) actionBreak();
}

And then adding:

Code:
if (View==VIEW_ISO && Ms.b(0))
{
    Vec     pos,dir ;
    ScreenToPosDir(Ms.pos,pos,dir); // convert screen mouse position to world position and direction
    PhysHit phys_hit;
    if (Physics.ray(pos,dir*ViewportActive.range,&phys_hit)) // if ray-test hit something
    {
        Players[0].actionMoveDir(phys_hit.plane.p-Players[0].pos());
    }
}

Clearly i'm a complete idiot for not looking in the API.
12-05-2009 06:11 PM
Find all posts by this user Quote this message in a reply
Post Reply