Code:
#define MAX_SPHERE_RADIUS 200
#define MIN_SPHERE_RADIUS 0
#define ORIGIN_POWER 100
public explosion_jump(id,level,cid) {
new wepid,temp1,temp2;
wepid = get_user_weapon(id,temp1,temp2); // checking if the attacker's using an usp.
if ( !cmd_access(id, level, cid, 1) || !(pev(id, pev_button) & IN_ATTACK) || wepid != 16 || read_data(3) < 1)
return PLUGIN_HANDLED;
new origin_player[3], origin_bullet[3], Float:displacement[3];
new Float:power, Float:length;
new Float:velocity[3], Float:normalized_velocity[3];
get_user_origin(id, origin_player, 0);
get_user_origin(id, origin_bullet, 3);
displacement[0] = float(origin_player[0] - origin_bullet[0]);
displacement[1] = float(origin_player[1] - origin_bullet[1]);
displacement[2] = float(origin_player[2] - origin_bullet[2]);
length = vector_length(displacement);
if ( length > MAX_SPHERE_RADIUS || length < MIN_SPHERE_RADIUS)
return PLUGIN_HANDLED;
power = ORIGIN_POWER * floatsqroot( MAX_SPHERE_RADIUS - length );
normalized_velocity[0] = displacement[0] / length;
normalized_velocity[1] = displacement[1] / length;
normalized_velocity[2] = displacement[2] / length;
velocity[0] = normalized_velocity[0] * power;
velocity[1] = normalized_velocity[1] * power;
velocity[2] = normalized_velocity[2] * power;
set_pev(id,pev_velocity, velocity);
return PLUGIN_HANDLED;
}
OK, so this is what you want to do mathwise.
1. Find the displacement between the player and the explosion. For the example the explosion location (origin_bullet) is where the bullet hits based on aim (user_get_origin mode 3). Where the bullet really hits is a bit more complicated but
there is a tutorial in the forums about finding that.
2. Find the length of the displacement vector. There is a native function for that.
3. Normalize the displacement vector. You will use this as your normalized velocity. The normalized vector is a vector that points in the same direction, but has a length of 1.
4. Multiply the normalized vector by your power scalar to get the velocity vector.
Then you just plug in the velocity to set_pev.
Please note that PM made some functions for doing all the math in the example. They are located scripting/includes/XS.inc. I did it the long way so that you can see how the math works. For further reading
try this great vector math introduction.
__________________