AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Suggestions / Requests (https://forums.alliedmods.net/forumdisplay.php?f=12)
-   -   How to know which side is the sound coming from (https://forums.alliedmods.net/showthread.php?t=330917)

EFFx 02-26-2021 14:17

How to know which side is the sound coming from
 
When someone is firing at me, how can I know which side are the bullets coming from?

Like, if i'm aiming anywhere and someone shoot me, I wanna return LEFT, RIGHT, BEHIND or IN FRONT information as origin, how can I?

Natsheh 02-26-2021 14:34

Re: How to know which side are the sound coming from
 
You mean you want to show a HUD shooting indictor on the screen ?

EFFx 02-26-2021 14:37

Re: How to know which side are the sound coming from
 
This is pretty much what I've done so far, pretty hard coded but just an example:

PHP Code:

    new Float:fFireAngle[3]
    
pev(iPlayerpev_anglesfFireAngle)
    
    new 
iPlayers[32], iNum
    get_players
(iPlayersiNum"a")
    for(new 
iPlayerAroundFloat:fPlayerAngle[3], i;iNum;i++)
    {
        
iPlayerAround iPlayers[i// iPlayerAround is the ID of all players around me, I'm checking which side they are from my perspective.
        
        
pev(iPlayerAroundpev_anglesfPlayerAngle)
        
        if(
40.0 <= (fPlayerAngle[1] - fFireAngle[1]) <= 105.0)
        {
            
log_amx("Player is on my left"
        }
        else if(
120.0 <= (fPlayerAngle[1] - fFireAngle[1]) > -147.0)
        {
            
log_amx("Player is behind me")
        }
        else 
log_amx("Player is on my right")
        
console_print(iPlayer"%f %f %f"fPlayerAngle[0] - fFireAngle[0], fPlayerAngle[1] - fFireAngle[1], fPlayerAngle[2] - fFireAngle[2])
    } 


If I shot at you, I wanna know first if it's behind you, your left or right or in front of you, then create an entity based on that information.

If I shot at behind you, I wanna get an origin behind you and then spawn an entity there.

Natsheh 02-26-2021 14:45

Re: How to know which side are the sound coming from
 
let me get this correct you want to spawn an entity if you have shot behind the player the entity will spawn over there ? am i correct? and if you shot infront of the player the entity will spawn infront of him ?

EFFx 02-26-2021 14:46

Re: How to know which side are the sound coming from
 
Yes, the entity will spawn depending of the sound origin, not the end point of the bullet.
If the sound is coming from behind, the entity will spawn behind. If the sound is coming from my left side, it'll spawn on my left side.

Edit:

Took this code - https://forums.alliedmods.net/showpo...48&postcount=9
And this is pretty much the closest thing I got:

PHP Code:

#include <amxmodx>
#include <engine>
#include <fakemeta> 

#define PLUGIN "Distant Sounds Test"
#define VERSION "1.0"

#define DIST         50.0
#define SND_DIST     1230.0
#define SND_HEIGHT     15.0

new const Sound[] = "misc/killChicken.wav";
new 
SoundIndex;
new 
cvar_distsndxcvar_distsndycvar_distsndz;

#define clamp_byte(%1)     ( clamp( %1, 0, 255 ) ) 
#define write_coord_f(%1)  ( engfunc( EngFunc_WriteCoord, %1 ) )
#define null_vector        ( Float:{ 0.0, 0.0, 0.0 } )

public plugin_init()
{
    
register_plugin(PLUGINVERSION"Arkshine & hellmonja");
    
    
register_concmd("debug""Debug");
    
    
cvar_distsndx register_cvar("ds_x""0.0");
    
cvar_distsndy register_cvar("ds_y""0.0");
    
cvar_distsndz register_cvar("ds_z""0.0");
}

public 
plugin_precache()
{
    
SoundIndex precache_soundSound );
    
precache_model("models/chick.mdl");
}

public 
Debug(user)
{
    new 
Float:pl_origin[3];
    new 
Float:snd_origin[3];
    new 
ent
    
///////////////////////////// 1 ///////////////////////////////////////////////////    
    
pev(userpev_originpl_origin); 
    
    
snd_origin[0] = pl_origin[0] + (DIST 25.0);
    
snd_origin[1] = pl_origin[1];
    
snd_origin[2] = pl_origin[2] + SND_HEIGHT;
    
    
ent create_entity("info_target")
    
entity_set_origin(ent snd_origin)
    
entity_set_model(ent,"models/chick.mdl");
    
    
spawnStaticSound(0snd_originSoundIndexVOL_NORMATTN_NORMPITCH_NORM, .flags 0);

    
set_task(5.5"remove_ent"ent);
///////////////////////////////////////////////////////////////////////////////////

///////////////////////////// 2 ///////////////////////////////////////////////////    
    
pev(userpev_originpl_origin); 
    
    
snd_origin[0] = pl_origin[0] - (DIST 25.0);
    
snd_origin[1] = pl_origin[1];
    
snd_origin[2] = pl_origin[2] + SND_HEIGHT;
    
    
ent create_entity("info_target")
    
entity_set_origin(ent snd_origin)
    
entity_set_model(ent,"models/chick.mdl");
    
    
spawnStaticSound(0snd_originSoundIndexVOL_NORMATTN_NORMPITCH_NORM, .flags 0);

    
set_task(5.5"remove_ent"ent);
///////////////////////////////////////////////////////////////////////////////////

///////////////////////////// 3 ///////////////////////////////////////////////////    
    
pev(userpev_originpl_origin); 
    
    
snd_origin[0] = pl_origin[0];
    
snd_origin[1] = pl_origin[1] + (DIST 25.0);
    
snd_origin[2] = pl_origin[2] + SND_HEIGHT;
    
    
ent create_entity("info_target")
    
entity_set_origin(ent snd_origin)
    
entity_set_model(ent,"models/chick.mdl");
    
    
spawnStaticSound(0snd_originSoundIndexVOL_NORMATTN_NORMPITCH_NORM, .flags 0);

    
set_task(5.5"remove_ent"ent);
///////////////////////////////////////////////////////////////////////////////////

///////////////////////////// 4 ///////////////////////////////////////////////////    
    
pev(userpev_originpl_origin); 
    
    
snd_origin[0] = pl_origin[0];
    
snd_origin[1] = pl_origin[1] - (DIST 25.0);
    
snd_origin[2] = pl_origin[2] + SND_HEIGHT;
    
    
ent create_entity("info_target")
    
entity_set_origin(ent snd_origin)
    
entity_set_model(ent,"models/chick.mdl");
    
    
spawnStaticSound(0snd_originSoundIndexVOL_NORMATTN_NORMPITCH_NORM, .flags 0);

    
set_task(5.5"remove_ent"ent);
///////////////////////////////////////////////////////////////////////////////////
    
    
pev(userpev_originpl_origin); 
    
    
snd_origin[0] = pl_origin[0] + DIST;
    
snd_origin[1] = pl_origin[1] + DIST;
    
snd_origin[2] = pl_origin[2] + SND_HEIGHT;
    
    
ent create_entity("info_target")
    
entity_set_origin(ent snd_origin)
    
entity_set_model(ent,"models/chick.mdl");
    
    
spawnStaticSound(0snd_originSoundIndexVOL_NORMATTN_NORMPITCH_NORM, .flags 0);

    
set_task(5.5"remove_ent"ent);///////
    
       
pev(userpev_originpl_origin); 
    
    
snd_origin[0] = pl_origin[0] - DIST;
    
snd_origin[1] = pl_origin[1] - DIST;
    
snd_origin[2] = pl_origin[2] + SND_HEIGHT;
    
    
ent create_entity("info_target")
    
entity_set_origin(ent snd_origin)
    
entity_set_model(ent,"models/chick.mdl");
    
    
spawnStaticSound(0snd_originSoundIndexVOL_NORMATTN_NORMPITCH_NORM, .flags 0);

    
set_task(5.5"remove_ent"ent);
    
//
    
       
pev(userpev_originpl_origin); 
    
    
snd_origin[0] = pl_origin[0] - DIST;
    
snd_origin[1] = pl_origin[1] + DIST;
    
snd_origin[2] = pl_origin[2] + SND_HEIGHT;
    
    
ent create_entity("info_target")
    
entity_set_origin(ent snd_origin)
    
entity_set_model(ent,"models/chick.mdl");
    
    
spawnStaticSound(0snd_originSoundIndexVOL_NORMATTN_NORMPITCH_NORM, .flags 0);

    
set_task(5.5"remove_ent"ent);
    
////
       
pev(userpev_originpl_origin); 
    
    
snd_origin[0] = pl_origin[0] + DIST;
    
snd_origin[1] = pl_origin[1] - DIST;
    
snd_origin[2] = pl_origin[2] + SND_HEIGHT;
    
    
ent create_entity("info_target")
    
entity_set_origin(ent snd_origin)
    
entity_set_model(ent,"models/chick.mdl");
    
    
spawnStaticSound(0snd_originSoundIndexVOL_NORMATTN_NORMPITCH_NORM, .flags 0);

    
set_task(5.5"remove_ent"ent);
    
//    new Float:dist = entity_range(user, ent);
    //    client_print(user, print_chat, "DIST: %f", dist);

}

public 
remove_ent(ent)
{
    
remove_entity(ent);
}

stock spawnStaticSound( const index, const Float:origin[3], const soundIndex, const Float:vol, const Float:atten, const pitch, const flags 

    
message_beginindex MSG_ONE MSG_ALLSVC_SPAWNSTATICSOUND, .player index );
    {
        
write_coord_forigin[0] ); 
        
write_coord_forigin[1] ); 
        
write_coord_forigin[2] );
        
write_shortsoundIndex );
        
write_byteclamp_bytefloatroundvol 255 ) ) );
        
write_byteclamp_bytefloatroundatten 64 ) ) );
        
write_shortindex );        
        
write_bytepitch ); 
        
write_byteflags );   
    }
    
message_end();


This code creates 8 entities on each side of player's ear and gets which entity is the closest to the player that used the command. As soon as the closes entity ID is get, it emits a sound. Although it's not what I was reaching, it is good already.

Sadly, this module is not working for me, dunno why, otherwise it could be used and then only one player could hear the firing sound depending of it's position and angle.

Natsheh 02-27-2021 03:16

Re: How to know which side is the sound coming from
 
PHP Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <fakemeta>
#include <xs>

#define PLUGIN "Check players position"
#define VERSION "1.0"
#define AUTHOR "Natsheh"

#define FOV 45.0

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_clcmd("say /test""clcmd_test");
}


public 
clcmd_test(id)
{
    new 
Float:fPlayerView[3], Float:fPitchFloat:fStart[3];
    
pev(idpev_originfStart); fStart[2] = 0.0;
    
pev(idpev_v_anglefPlayerView);
    
    
// we only care about the pitch angle.
    
fPitch fPlayerView[1];
    
// fix the angle.
    
if(fPitch 0.0fPitch += 360.0;
    
    new 
players[32], pnumFloat:fOrigin[3];
    
get_players(playerspnum"a");
    
    for(new 
iszName[32], Float:fDirection[3], Float:fTargetPitchplayerpnumi++)
    {
        
player players[i];
        if(
player == id) continue;
        
pev(playerpev_originfOrigin);
        
fOrigin[2] = 0.0;
        
xs_vec_sub(fStart,fOrigin fDirection);
        
xs_vec_normalize(fDirectionfDirection);
        
vector_to_angle(fDirectionfDirection);
        
        
fTargetPitch fDirection[1];
        
// fix the angle.
        
if(fTargetPitch 0.0fTargetPitch += 360.0;
        
        if( 
isAngleBetween(fPitch FOVfPitch FOVfTargetPitch) )
        {
            
get_user_name(playerszName31);
            
client_print(idprint_chat"%s is behind!"szName);
        }
        else if( 
isAngleBetween(fPitch FOV 90.0fPitch FOV 90.0fTargetPitch) )
        {
            
get_user_name(playerszName31);
            
client_print(idprint_chat"%s is on your left!"szName);
        }
        else if( 
isAngleBetween(fPitch FOV 90.0fPitch FOV 90.0fTargetPitch) )
        {
            
get_user_name(playerszName31);
            
client_print(idprint_chat"%s is on your right!"szName);
        }
        else if( 
isAngleBetween(fPitch FOV 180.0fPitch FOV 180.0fTargetPitch) )
        {
            
get_user_name(playerszName31);
            
client_print(idprint_chat"%s is up ahead!"szName);
        }
    }
    
}

bool:isAngleBetween(Float:startFloat:endFloat:mid)
{
    if(
start 0.0start += 360.0;
    if(
end 0.0end += 360.0;
    
end = (end start) < 0.0 end start 360.0 end start;
    
mid = (mid start) < 0.0 mid start 360.0 mid start;
    return (
mid end); 


here i just made out this, it will tell you where're the players are depending on their position, hopefully it will help in somehow, i'm sure this is what you've wanted.

EFFx 03-01-2021 16:09

Re: How to know which side is the sound coming from
 
Yeah it is, the main point is to create an entity where the information is coming (left, right, etc) and then emit an sound with it. Your code gets the information, all what is left is getting an origin to spawn an entity and then emit the sound there, so the player will know which side the sound is coming from, instead of just the message, y'know?


All times are GMT -4. The time now is 12:31.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.