Jump to content
Sign in to follow this  
grant85

Teleport player in front of another player (or other ent) ?

Recommended Posts

I am trying to design a function to teleport player A to directly in front of player B and make him face player B. However, I am not familiar enough with vectors and the vector functions to achieve this. How can I determine an origin x units from where player B is facing? And how can I ensure that the player spawns on the ground, not in the ground or in the air?


Share this post


Link to post

Subtract the viewangles from the origin to get the direction. Multiply the offset in units by the direction to get the change in units. Add the change in units to the origin to get the final destination.

 

So if this is a mathematical equation:

a - o = d

d * f = c

c + o = s

Where a = viewangles, o = origin, d = direction, f = offset, c = change from origin and s = solution.

That could probably be simplified somehow.

 

There are functions VectorMA, VectorAdd, VectorSubtract which do this. I don't know the parameters of each off the top of my head, but the last one is always the result. Also there is VectorCopy.

 

EDIT: VectorMA is "Multiply + Add", meaning you can do the last two parts of that equation in one line of code.


Share this post


Link to post

I updated my original post to clarify that I am trying to teleport player A directly in front of player B and make player A face player B.

 

Subtract the viewangles from the origin to get the direction. Multiply the offset in units by the direction to get the change in units. Add the change in units to the origin to get the final destination.

So if this is a mathematical equation:
a - o = d
d * f = c
c + o = s
Where a = viewangles, o = origin, d = direction, f = offset, c = change from origin and s = solution.
That could probably be simplified somehow.

There are functions VectorMA, VectorAdd, VectorSubtract which do this. I don't know the parameters of each off the top of my head, but the last one is always the result. Also there is VectorCopy.

EDIT: VectorMA is "Multiply + Add", meaning you can do the last two parts of that equation in one line of code.

 

Okay, I think I understand the first part, angles - origin = direction, but I'm lost on the rest of it. What is offset, a vector? How do you determine the values to put in the offset? If I wanted to teleport the person 45 units in front of the other, would that be {45.0f, 45.0f, 0}?

 

 

 

As far as the parameters go:

void _VectorAdd( const vec3_t veca, const vec3_t vecb, vec3_t out ) {
out[0] = veca[0]+vecb[0];
out[1] = veca[1]+vecb[1];
out[2] = veca[2]+vecb[2];
}
 
void _VectorSubtract( const vec3_t veca, const vec3_t vecb, vec3_t out ) {
out[0] = veca[0]-vecb[0];
out[1] = veca[1]-vecb[1];
out[2] = veca[2]-vecb[2];
}
 
void _VectorMA( const vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc) {
vecc[0] = veca[0] + scale*vecb[0];
vecc[1] = veca[1] + scale*vecb[1];
vecc[2] = veca[2] + scale*vecb[2];
}

Share this post


Link to post

Offset is how far you want the other person to be away from you.

 

So your code should look like the following:

 

 

void Cmd_TeleMe_f(gentity_t* ent) {
    float offset = 32.0f;
    vec3_t dir, pos;
    _VectorSubtract(ent->client->ps.origin, ent->client->ps.viewAngles, dir);
    _VectorMA(ent->client->ps.origin, offset, dir, pos);
    // pos now contains the position you want that person to teleport to
}

The first two args of VectorSubtract might have to be flipped around. I can't remember which way they go.

For getting the other player to face you, I know it's possible. @@Scooper is great with vector math though. Much better than me. I'm pretty sure.


Share this post


Link to post
void Cmd_TeleMe_f(gentity_t* ent)
{
    float offset = 32.0f;
    vec3_t dir, pos, newAngle;
    char buffer[MAX_TOKEN_CHARS];
    gentity_t* target = NULL;
    int playerIndex = -1;
    
    // Find the target player
    trap_Argv( 1, buffer, sizeof( buffer ) );
    playerIndex = ClientNumberFromString(ent, buffer, qfalse);

    if( playerIndex == -1 )
    {
        return;
    }
    target = g_entities[playerIndex];
    
    // Find the forward vector from the target npc's view angles.
    AngleVectors(target->client->ps.viewangles, dir, NULL, NULL);
    
    // Find the point that is offset infront of the target player.
    VectorMA(target->client->ps.origin, offset, dir, pos);
    
    // Flip the vector so it's pointing towards the target player
    VectorScale(dir, -1, dir);
    
    // Use the flipped vector to get new facing angle for yourself
    vectoangles(dir, newAngle);
    
    // Teleport yourself(ent) to the position right infront of target(pos) with the new angle facing the target(newAngle)
    TeleportPlayer(ent, pos, newAngle);
}

Something like this should work.


grant85 likes this

Share this post


Link to post
void Cmd_TeleMe_f(gentity_t* ent)
{
    float offset = 32.0f;
    vec3_t dir, pos, newAngle;
    char buffer[MAX_TOKEN_CHARS];
    gentity_t* target = NULL;
    int playerIndex = -1;
    
    // Find the target player
    trap_Argv( 1, buffer, sizeof( buffer ) );
    playerIndex = ClientNumberFromString(ent, buffer, qfalse);

    if( playerIndex == -1 )
    {
        return;
    }
    target = g_entities[playerIndex];
    
    // Find the forward vector from the target npc's view angles.
    AngleVectors(target->client->ps.viewangles, dir, NULL, NULL);
    
    // Find the point that is offset infront of the target player.
    VectorMA(target->client->ps.origin, offset, dir, pos);
    
    // Flip the vector so it's pointing towards the target player
    VectorScale(dir, -1, dir);
    
    // Use the flipped vector to get new facing angle for yourself
    vectoangles(dir, newAngle);
    
    // Teleport yourself(ent) to the position right infront of target(pos) with the new angle facing the target(newAngle)
    TeleportPlayer(ent, pos, newAngle);
}

Something like this should work.

 

 

That's what I needed and the comments help too. Thanks!


Share this post


Link to post

... I am thinking about a SP force power not, that allow to the jedi to teleport directly into the face of an opponent for surprise and slay him :o:


Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...