loop omit a value

M

Michael

Hi
I'm setting a position for which I needed a function to return a number
from:
0 up to 180 then down to 0 again. The problem is to set lets say a
position at 32 but to have it exececuted only once.
Now I try something alike:
if (pPsi > 10 && pPsi < 40)
{
heading = 40;
}
which obviously doesn't work. It should only work one way but not both ways.
I need: first 32 do it, second 32 coming down, do nothing. Going back up
to 32 do it again and so on.
Many thanks
Michael
 
F

Francesco S. Carta

Hi
I'm setting a position for which I needed a function to return a number
from:
0 up to 180 then down to 0 again. The problem is to set lets say a
position at 32 but to have it exececuted only once.
Now I try something alike:
if (pPsi > 10 && pPsi < 40)
{
heading = 40;
}
which obviously doesn't work. It should only work one way but not both
ways.
I need: first 32 do it, second 32 coming down, do nothing. Going back up
to 32 do it again and so on.

What about using a boolean flag?
 
H

Haider

First, your problem is not clear. Please describe it bit more.
second, else will help you in coming down.. I guess...

Thanks,
-Haider
 
M

Michael

Imagine a car driving around randomly. Now you look at it (listener)
circling around. On the back of the car I want a different sound than on
the front. For sound attenuation i had to do 0 - 180 - 0 now the problem
is same sound at front and back. So i need to omit front. I tried with
boolean but don't get the right code.
if heading > 10 and < 30 always omits all but i only need to omit at front.
Many thanks
 
Ö

Öö Tiib

Imagine a car driving around randomly. Now you look at it (listener)
circling around. On the back of the car I want a different sound than on
the front. For sound attenuation i had to do 0 - 180 - 0 now the problem
is same sound at front and back. So i need to omit front. I tried with
boolean but don't get the right code.
if heading > 10 and < 30 always omits all but i only need to omit at front.
Many thanks

I understood you need function that replaces 0 to 360 with 0 to 180 to
0? If such algorithm feels too complicated to develop then first write
some tests. Lets say your function is called "attenuation":

#include <assert.h>
#include <iostream>

int attenuation( int input );

int main()
{
std::cout << std::endl << "test 1:"
<< (attenuation(0) == 0) ? "pass" : "fail"
<< std::endl << "test 2:"
<< (attenuation(90) == 90) ? "pass" : "fail"
<< std::endl << "test 3:"
<< (attenuation(180) == 180) ? "pass" : "fail"
<< std::endl << "test 4:"
<< (attenuation(270) == 90) ? "pass" : "fail"
<< std::endl << "test 5:"
<< (attenuation(360) == 0) ? "pass" : "fail"
<< std::endl << "Done!" << std::endl;

return 0;
}

That compiles but linker complains it can not find that "attenuation"
function anywhere.
So implement the function:

int attenuation( int input )
{
assert( 0 <= input && input <= 360 );
return 0; // not implemented
}

Now you can run it. See, it is not implemented but 2 of 5 tests
already run.

It is possibly not what you need ... because i did not understand the
real problem. All that mess of heading? position? pPsi? 180? 40? 32?
confused me. However it is the usual way how to solve complex
programming problems. Make clear to yourself what you want to achieve
and then achieve it and done.
 
Ö

Öö Tiib

 int main()
 {
    std::cout << std::endl << "test 1:"
              << (attenuation(0) == 0) ? "pass" : "fail"
              << std::endl << "test 2:"
              << (attenuation(90) == 90) ? "pass" : "fail"
              << std::endl << "test 3:"
              << (attenuation(180) == 180) ? "pass" : "fail"
              << std::endl << "test 4:"
              << (attenuation(270) == 90) ? "pass" : "fail"
              << std::endl << "test 5:"
              << (attenuation(360) == 0) ? "pass" : "fail"
              << std::endl << "Done!" << std::endl;

    return 0;
 }

Ups, Forgot parentheses, should be:

int main()
{
std::cout << std::endl << "test 1:"
<< ((attenuation(0) == 0) ? "pass" : "fail")
<< std::endl << "test 2:"
<< ((attenuation(90) == 90) ? "pass" : "fail")
<< std::endl << "test 3:"
<< ((attenuation(180) == 180) ? "pass" : "fail")
<< std::endl << "test 4:"
<< ((attenuation(270) == 90) ? "pass" : "fail")
<< std::endl << "test 5:"
<< ((attenuation(360) == 0) ? "pass" : "fail")
<< std::endl << "Done!" << std::endl;

return 0;
}
 
M

Michael

Thanks but I don't understand how this would omit one value.


180

90 CAR 90

0


So for ex. 20 would be at the back of the car on both sides but it only
should be at one side. At least sound attenuation only for this position.
Many thanks
 
M

Michael

Maybe there's only a way to get smooth sound attenuation in a different
way? The problem was interrupted sound at position: ... 360 - 0 ...
 
A

Alf P. Steinbach /Usenet

* Michael, on 28.06.2010 09:27:
Maybe there's only a way to get smooth sound attenuation in a different
way? The problem was interrupted sound at position: ... 360 - 0 ...

As I recall it's not long since you (or someone very like you, name of Mike)
asked this very same question.

Öö Tib's answer this time, test driven development, is an excellent one.

For further ideas look at the answers you (or the other Mike) got in the
previous thread. As I recall I listed three functions to implement, in order,
guiding you step by tiny step towards a solution. That should not be very hard,
especially if you do this using Tib's approach.


Cheers & hth.,

- Alf
 
M

Michael

Hi Alf
sure i use the fmod solution but this is for omitting all either left or
right sided values. Not the same question.
Thanks
 
F

Francesco S. Carta

Michael said:
Hi Alf
sure i use the fmod solution but this is for omitting all either left or
right sided values. Not the same question.
Thanks

This message, for some reason, disappeared from my news server, but
it's still present here on GG... whatever.

I'm not sure if I got your question right, the first time I suggested
you to use a flag, so that you can process/ignore that value on an odd/
even basis.

Another choice could be to store the previously seen value and process
the current depending on it being higher or lower than the previous -
in other words, process it in the ascending part of the loop and
ignore it in the descending one, or the other way around.

I think it could come handy to know the granularity and the
homogeneity of the loop: how big are the steps? can the value jump up
and down of arbitrary steps?

Posting a simplified version of the loop, maybe along with eventual
input, achieved and requested outputs and so on will lead to further
help for sure.
 
M

Michael

Imagine a camera around a car. steps are float but of course the cam
rotates at user's will.

180

90 car 90

0

So values go from 0 - 180 - 0. How would you store the values?
Many thanks
 
F

Francesco S. Carta

Imagine a camera around a car. steps are float but of course the cam
rotates at user's will.

180

90 car 90

0

So values go from 0 - 180 - 0. How would you store the values?
Many thanks


I would store them either 0 to 360 or -180 to 180, the way that you use
or display those values afterwards makes no difference.

In such case, you would intercept, for example, values -32 and 32 (to
cite the value you mentioned in the OP), processing either the former or
the latter.

Hope that helps.
 
F

Francesco S. Carta

M

Michael

if (pPsi > 180)
{
heading = fmod(pPsi, 180);
}else{
heading = 180 - fmod(pPsi, 180);
}

Now two problems here. One still the same on how to avoid double
positions and the other new on how to set two positions. one in front
and one in the back of the car or whatever.
Many thanks
pPsi is a value in degrees between 0-360'
 
F

Francesco S. Carta

if (pPsi > 180)
{
heading = fmod(pPsi, 180);
}else{
heading = 180 - fmod(pPsi, 180);
}

Now two problems here. One still the same on how to avoid double
positions and the other new on how to set two positions. one in front
and one in the back of the car or whatever.
Many thanks
pPsi is a value in degrees between 0-360'


I'm not at all sure I understand what you're asking for, does the
following help you?

//-------
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
const double step = 360.0 / 8;

for(double angle = 0; angle < 360.0; angle += step) {
cout << "Front cam: " << angle << endl;
cout << "Right cam: " << fmod(angle + 45.0, 360.0) << endl;
cout << "Back cam: " << fmod(angle + 180.0, 360.0) << endl;
cout << "Left cam: " << fmod(angle + 225.0, 360.0) << endl;
cout << endl;
}

return 0;
}
//-------

Of course, the correctness of concepts such as "left" and "right" depend
on the direction being clockwise or not, and whether we're speaking
about the angle of the camera related to the car, the angle of the car
related to the camera, or both of them related to the world.

Such things involve lots of conversions, translations (does that mean
"movements" in English too?), you might want to express positions and
directions in Cartesian or Polar coordinates or whatever... all of this
is really interesting but it is also way off topic here ;-)
 
M

Michael

Many thanks Francesco
This should have solved the sound placement. Now still I've difficulties
on how to omit one side.

//car heading = heading ; camera heading = heading2
now = fmod(pPsi + 180.0, 360.0);

if (pPsi > 180)
{
heading = fmod(pPsi, 180);
}else{
heading = 180 - fmod(pPsi, 180);
}
if ((now - heading2) < 40){
heading = 180 - fmod(pPsi, 180);

I need to omit also the sound attenuation. So if heading between f.ex.
10-40 set sound position to the other side or so.
Still some faults above...maybe somone has a better view for such than me.
Thanks again
Michael
 
F

Francesco S. Carta

Many thanks Francesco
This should have solved the sound placement. Now still I've difficulties
on how to omit one side.

//car heading = heading ; camera heading = heading2
now = fmod(pPsi + 180.0, 360.0);

if (pPsi > 180)
{
heading = fmod(pPsi, 180);
}else{
heading = 180 - fmod(pPsi, 180);
}
if ((now - heading2) < 40){
heading = 180 - fmod(pPsi, 180);

I need to omit also the sound attenuation. So if heading between f.ex.
10-40 set sound position to the other side or so.
Still some faults above...maybe somone has a better view for such than me.

I repeat that all of this stuff isn't topical here, please bring it to a
group were you can find a wider and more experienced audience on these
subjects.

I'm still having hard time understanding what you're asking for, also
because I am not experienced in these matters.

Look out for Computer Graphics groups and eventually for groups about
the CG libraries you're using - assuming you're using any.

Good luck!
 
R

Ron Francis

Michael said:
Many thanks Francesco
This should have solved the sound placement. Now still I've difficulties on how to omit one side.

//car heading = heading ; camera heading = heading2
now = fmod(pPsi + 180.0, 360.0);

if (pPsi > 180)
{
heading = fmod(pPsi, 180);
}else{
heading = 180 - fmod(pPsi, 180);
}
if ((now - heading2) < 40){
heading = 180 - fmod(pPsi, 180);

I need to omit also the sound attenuation. So if heading between f.ex. 10-40 set sound position to
the other side or so.
Still some faults above...maybe somone has a better view for such than me.
Thanks again
Michael

I also have difficulty understanding your problem.
From what I can see, you have a camera and a car, and you want to determine what proportion of the
sound to play in each speaker (stereo) and how loud it should be depending on the distance the car
is from the camera (attenuation)?
I would attack the problem this way.
Your camera should have a position and a direction vector and an up vector.
That is enough information to create a plane.
Your car has a position.
Use a dot product to find out what side of your plane the car is, and the distance from it.
This should determine what proportion of the sound is divided into each speaker.
Calculate the distance from the camera to the car and use that to determine the volume of the sound.
If you want to include a Doppler effect, you could store the previous distance the car was from the
camera, and change the sound based on whether it's getting closer or further away.

Keep in mind that I am not at all familiar with OpenGL or 3D sound.

Regards
Ron Francis
www.RonaldFrancis.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top