Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Listener rotation  (Read 11161 times)

0 Members and 2 Guests are viewing this topic.

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Listener rotation
« on: May 21, 2012, 11:38:51 pm »
Hi Laurent,

I am in the process of testing SFML 2 RC and its many features, (re)making a little game.
So far it is loads of fun and pretty simple to use, but I do have a question about the Listener class:

How would one go about rotating the listener around the z-axis, like sf::View does with setRotation?

Thank you in advance!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Listener rotation
« Reply #1 on: May 22, 2012, 08:15:23 am »
sf::Listener::setDirection(std::cos(angle), std::sin(angle), 0);
Laurent Gomila - SFML developer

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Re: Listener rotation
« Reply #2 on: May 22, 2012, 08:48:20 am »
But that implies that [ -1, 0, 0 ] is equal to [ 0, 0, -1 ], which is the default direction. Is that really true?
How about [ -1, 0, -1 ] then?

I thought about doing it like you suggest, but then some sounds would be "behind" the listener, and I can't figure out if it makes a difference.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Listener rotation
« Reply #3 on: May 22, 2012, 09:01:00 am »
Quote
But that implies that [ -1, 0, 0 ] is equal to [ 0, 0, -1 ], which is the default direction. Is that really true?
How about [ -1, 0, -1 ] then?
Sorry but I don't get it. What do you mean?

Quote
I thought about doing it like you suggest, but then some sounds would be "behind" the listener, and I can't figure out if it makes a difference.
It makes a difference if you have front and back speakers, it doesn't if you have simple stereo speakers.
Laurent Gomila - SFML developer

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Re: Listener rotation
« Reply #4 on: May 22, 2012, 09:13:11 am »
Quote
But that implies that [ -1, 0, 0 ] is equal to [ 0, 0, -1 ], which is the default direction. Is that really true?
How about [ -1, 0, -1 ] then?
Sorry but I don't get it. What do you mean?
What I meant was, that your solution implied that a listener direction of [ -1, 0, 0 ] would have the same effect as [ 0, 0, -1 ], but you already clarified that that was not the case. At least not if the user has surround sound.

But what all this means, if I understand you correctly, is that there is currently no way of aligning the listener with a view, if the view is rotated. I would say that is a pretty deep hole in the audio API, wouldn't you?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Listener rotation
« Reply #5 on: May 22, 2012, 09:27:30 am »
Quote
What I meant was, that your solution implied that a listener direction of [ -1, 0, 0 ] would have the same effect as [ 0, 0, -1 ]
I still don't get it. Why would a listener oriented along the X axis would be the same as one oriented along the Z axis?

Quote
But what all this means, if I understand you correctly, is that there is currently no way of aligning the listener with a view, if the view is rotated.
I don't get that too. Why wouldn't you be able to rotate the listener the same way as the view? You can use any position and orientation in 3D, there's no limitation.
Laurent Gomila - SFML developer

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Re: Listener rotation
« Reply #6 on: May 22, 2012, 08:19:30 pm »
Sorry for the break in our conversation - I had to leave the house.

Quote
What I meant was, that your solution implied that a listener direction of [ -1, 0, 0 ] would have the same effect as [ 0, 0, -1 ]
I still don't get it. Why would a listener oriented along the X axis would be the same as one oriented along the Z axis?
Because you said so. My original question was about rotating the listener around the z-axis, just like sf::View, not facing the listener in different directions in 3D space.

Quote
But what all this means, if I understand you correctly, is that there is currently no way of aligning the listener with a view, if the view is rotated.
I don't get that too. Why wouldn't you be able to rotate the listener the same way as the view? You can use any position and orientation in 3D, there's no limitation.
Yes, the limitation is that you can't rotate the listener. That makes it impossible to align the 3D listener to the 2D view.

Let's say that I have a rocket ship in my game world, which is visually represented with a sprite, and audibly represented with a looping sound.
Let's also say that the view makes it so that the rocket is rendered at the bottom of the screen.

Now, if the view isn't rotated, then I can easily align the listener to it, by setting the x and y components of its position to those of the view's center. No problem - all the sounds play through the front speakers. Everything makes sense.

But if I set the direction of the listener dependent on the rotation of the view, as you suggest, then the sound of the rocket will be playing through the back speakers, which doesn't make sense in a 2D world.

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Re: Listener rotation
« Reply #7 on: May 22, 2012, 08:30:26 pm »
My first statement in the previous post post
Quote
Because you said so.
is obviously wrong.
I meant [0, -1, 0], and not [-1, 0, 0]. So the question shoould have been if a listener facing the y-axis would be equal to one facing the z-axis.

My guess is "no", so the rest of the post is still valid.

Sorry for the confusion.  :-[

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Listener rotation
« Reply #8 on: May 22, 2012, 11:39:46 pm »
There's obviously something that you don't understand, but I don't know what ;D

To be clear: you can set any position and any rotation for the listener. So you're not forced to have it in front of or behind something -- if it's the case then adjust the coordinates.
Laurent Gomila - SFML developer

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Re: Listener rotation
« Reply #9 on: May 23, 2012, 01:50:48 am »
There's obviously something that you don't understand, but I don't know what ;D
That is likely, but there must also be something in my argumentation that you don't understand. Otherwise you would be able to point out my error more clearly. :)

I am sorry about not being clear before, but the nature of the problem wasn't completely clear in my head. Our little conversation has forced me to think harder about it, which is one reason I enjoy discussing things with you.

If you don't mind, I would like to continue:

Quote from: sf::Listener Class Reference
If sf::View is the eyes of the user, then sf::Listener is his ears (by the way, they are often linked together -- same position, orientation, etc.).
My argument is that linking the orientation of the two is not possible because they are conceptually different. Here is why:
From a 3D perspective, orientation In 2D space can be described as rotation around an imaginary z-axis. On the other hand, you cannot describe 3D orientation from a 2D perspective. There simply is no equivalent.

To be clear: you can set any position and any rotation for the listener. So you're not forced to have it in front of or behind something -- if it's the case then adjust the coordinates.
This is not true. You can't set rotation for the listener, only orientation. Therefore it is not possible to truly link the 3D listener to a 2D view, and a compromise must be made.

Furthermore, I postulate that the compromise you suggest, will inevitably result in positioning some sounds behind the listener.

I hope that explains my position more clearly, so you can easily point out any misconceptions I may have.  :D
« Last Edit: May 23, 2012, 03:15:16 am by model76 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Listener rotation
« Reply #10 on: May 23, 2012, 08:01:16 am »
Quote
On the other hand, you cannot describe 3D orientation from a 2D perspective. There simply is no equivalent.
Ok but we don't mind, here we want to link the listener to the view, not the other way round :)

Quote
This is not true. You can't set rotation for the listener, only orientation.
What's the difference? An axis/angle can always be converted to a direction.

Quote
Therefore it is not possible to truly link the 3D listener to a 2D view, and a compromise must be made.

Furthermore, I postulate that the compromise you suggest, will inevitably result in positioning some sounds behind the listener.
It's not a compromise, a 2D rotation is equivalent to the vector that I gave in my first post: (cos(a), sin(a), 0). That is, if you consider the 2D plane being XY. You can use other planes (XZ or YZ) with the same results.

Quote
I hope that explains my position more clearly, so you can easily point out any misconceptions I may have.
I think you don't realize that you can define any 2D rotation with a 3D vector -- 3D is a superset of 2D so why would there be limitations and compromises to do? ???

If you still don't get my point, maybe you can write an example of a 2D angle that cannot be defined with a 3D vector, and what kind of compromises it would require.
« Last Edit: May 23, 2012, 08:04:48 am by Laurent »
Laurent Gomila - SFML developer

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Re: Listener rotation
« Reply #11 on: May 23, 2012, 11:04:32 am »
Hi Laurent

I'm not the question author, but I think I understand what he meant.

Lets say the view is rotated 90 degrees clockwise. That means that what is on negative Y (relative to the listener position) should come out from the right speaker, and what is on positive Y should come out from the left speaker.

The direction of the listener remains the same as the default, but it is rotated. An angle can be used to make a direction, but the other way around misses some data.

Or, of course, I may have missunderstood the whole cenario, and all I'm saying is non-sense.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Listener rotation
« Reply #12 on: May 23, 2012, 12:13:05 pm »
I think I understand.

To solve the problem that you describe, one would have to change the "up" vector of the listener (the listener's coordinate system is defined by the following 3 axes: front (direction), up, and right (up x front)).

The "up" vector is currently fixed to (0, 1, 0) by SFML. So in fact one should map the XZ plane of the listener to the XY plane of the 2D graphics. This way, there should be no limitation.
Laurent Gomila - SFML developer

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Re: Listener rotation
« Reply #13 on: May 23, 2012, 01:41:25 pm »
Eureka!
Tex nailed it. Thank you Tex!  :)

Just so you know, Laurent, I spend hours trying to understand your point, and coming up with explanations for mine. I regularly mess up, but I do try hard. Here is how I was going to try to clarify my point next, just because I can't stand letting it go to waste:

Quote
What's the difference? An axis/angle can always be converted to a direction.
Let's say we have a friend named Magnus. All we know about him is that he is looking at the sun. From that information, how can we tell if he is up side down? We can't. That information is not contained in direction. Magnus is rotated around an axis, but we have no way of knowing, since all we know is his orientation.

Anyway, I don't quite understand your solution yet, so I will have to do some more thinking and experimentation.

However, I have to leave the house soon and drive 100 km to a different location. So hopefully I will be able to continue our conversation later tonight, and post some code.

Thank you for your patience!  8)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Listener rotation
« Reply #14 on: May 23, 2012, 02:01:37 pm »
Your last example is very good too, and it proves that our brains are properly sync'd now ;D

But this example doesn't reflect the problem that we're trying to solve. Here we need two vectors (up and front) to clearly define the coordinate system of Magnus, because Magnus lives in a 3D world. Our use case is a 2D world, with only 2 axes -- therefore we need only a single vector to define a coordinate system.

Imagine that your 2D world is the XZ plane of a bigger 3D world, and thus rotates around the Y axis. Since it's a 2D world, it is only allowed to translate in the XZ plane or rotate around the Y axis. Nothing more. What I suggest is simply to apply the same rules to the listener: define its position in the XZ plane, rotate it around the Y axis, and it will be able to exactly match your 2D graphics view :)
Laurent Gomila - SFML developer

 

anything