• 12
• 12
• 9
• 10
• 13

# 3D Sound

This topic is 4133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I am trying to make 3D sound effects with direct sound.But i have a problem about listener poisitions. I have a FirstPerson Camera in my engine then for activating the right speaker i have to know where is the sound and what direction i am looking for.It's hard to explain with words of my problem. I did a simple to graph. Let me explain rest on the code.
float Magnitude(D3DXVECTOR3 vNormal)
{
return (float)sqrt( (vNormal.x * vNormal.x) + (vNormal.z * vNormal.z) );
}

float Dot(D3DXVECTOR3 vVector1, D3DXVECTOR3 vVector2)
{
return ( (vVector1.x * vVector2.x) + (vVector1.z * vVector2.z) );
}

double AngleBetweenVectors(D3DXVECTOR3 Vector1, D3DXVECTOR3 Vector2)
{

float dotProduct = Dot(Vector1, Vector2);

float vectorsMagnitude = Magnitude(Vector1) * Magnitude(Vector2) ;

return( acos( dotProduct / vectorsMagnitude ) );
}

// AngleBetweenVectors func. returns the value of angle between LookatWay Vec
// and SoundWay Vec.

double SoundPos = AngleBetweenVectors( A, B );

/*

if(SoundPos < 0.6)
"Front Center"

if(SoundPos > 0.6 && SoundPos < 1.6 )
"Front LEFT and Right"

if(SoundPos > 1.6 )
"Rear LEFT and Right"

As you see the code above i can find
is sound in area " 1 "    Front Center Speaker Active
is sound in area " 2 or 5"  Front LEFT and Right Speakers Active
is sound in area " 3 or 4"  Rear LEFT and Right Speakers Active

Area 1 is ok but i have to separate area 2,5  and 3,4.
*/


Does anyone has any idea about how can i find the sound position is on LookatWay vec's right or on the left. Thanks for helps.

##### Share on other sites
You need more information to begin with, like an up vector for your camera would be standard. You can then cross this with your LookAt to get a right or left vector. Dot product this with the sound vector, and if it's positive the sound is on the side the vector points to. Done.

##### Share on other sites
You want to determine if the vector B is to the left or right of vector A? It's fairly straightforward, but you'll need an 'up' vector defined.

If you take the cross product of A and B, you'll have a new vector perpendicular to their plane. Moreover, this normal vector will point either up or down depending on which side B is of A.
N = Vec3Cross(A, B);if (Vec3Dot(N, Up) > 0) {    // Sound is to the right} else {    // Sound is to the left}

You may need to swap the left & right cases depending on the handedness of your coordinate system.
Also note that there is an exceptional case where B is parallel to A (i.e. the source is dead-ahead), whence N will be zero, and so will the resulting dot product. This case isn't worth accommodating, though if you use the abs(Vec3Dot(A, B)) and its complement to pan the sound across the two speakers.

Regards