Re: openAl sound
Posted: Tue Apr 14, 2020 5:20 am
And if listener moving with some velocity should I account for that?
You set the position and velocity of your listener and set the position and velocity of the sources. You set these every frame, independently of the others. That's all you need to do, you don't need to calculate anything else, OpenAL will understand what the distances between the objects are.
Yes, exactly. OpenAL will calculate relative distance between listener and source for you.
AL_INVALID_NAME
only occurs in alSource3f
when the source ALuint
that you're providing is not valid. Looking at your code, I believe you might want to be using an auto&
in your initialisation loop. Otherwise you're assigning values to a copy of a Component::Vehicle
, not the Component::Vehicle
in the container.Code: Select all
auto vehicles = getEngine()->getSubSystem<EngineStore>()->getRoot().getComponentsOfType<Component::Vehicle>();
for (auto& vehicle : vehicles)
{
if (vehicle->type == Component::Vehicle::Type::Player) { continue; }
vehicle->aiSource = generateSource();
}
Code: Select all
auto vehicles = getEngine()->getSubSystem<EngineStore>()->getRoot().getComponentsOfType<Component::Vehicle>();
glm::vec3 playerPosition;
for (auto vehicle : vehicles)
{
if (vehicle->type == Component::Vehicle::Type::Player)
{
playerPosition = vehicle->position;
}
}
for (auto& vehicle : vehicles)
{
if (!vehicle->pxVehicle)
continue;
if (vehicle->type == Component::Vehicle::Type::Player) { continue; }
bool currentAcceleration;
bool currentBreaking;
glm::vec3 relativePosition = vehicle->position - playerPosition;
auto velocity = vehicle->pxVehicle->getRigidDynamicActor()->getLinearVelocity();
alCall(alSource3f, vehicle->aiSource, AL_POSITION, relativePosition.x, relativePosition.y, relativePosition.z);
alCall(alSource3f, vehicle->aiSource, AL_VELOCITY, velocity.x, velocity.y, velocity.z);
if (vehicle->pxVehicleInputData.getAnalogAccel() == 1)
{
currentAcceleration = true;
if (currentAcceleration != vehicle->initialAccelerate)
{
alCall(alSourcei, vehicle->aiSource, AL_LOOPING, AL_TRUE);
alCall(alSourcei, vehicle ->aiSource, AL_BUFFER, accelerationBuffer);
playSound(vehicle ->aiSource);
vehicle->initialAccelerate = true;
}
}
if (vehicle->pxVehicleInputData.getAnalogAccel() == 0)
{
currentAcceleration = false;
if (currentAcceleration != vehicle->initialAccelerate)
{
stopSound(vehicle->aiSource);
vehicle->initialAccelerate = false;
}
}
if (vehicle->pxVehicleInputData.getAnalogBrake() == 1)
{
currentBreaking = true;
if (currentBreaking != vehicle->initialBreak)
{
alCall(alSourcei, vehicle->aiSource, AL_LOOPING, AL_FALSE);
alCall(alSourcei, vehicle->aiSource, AL_BUFFER, breakingBuffer);
playSound(vehicle->aiSource);
vehicle->initialBreak = true;
}
}
if (vehicle->pxVehicleInputData.getAnalogBrake() == 0)
{
currentBreaking = false;
if (currentBreaking != vehicle->initialBreak)
{
stopSound(vehicle->aiSource);
vehicle->initialBreak = false;
}
}
}
}
AL_INVALID_NAME
error on the alSource3f
calls, the only way that can happen is with an invalid source. So with that in mind, have you tried doing something as simple as this:Code: Select all
auto vehicles = getEngine()->getSubSystem<EngineStore>()->getRoot().getComponentsOfType<Component::Vehicle>();
for (auto& vehicle : vehicles)
{
if (vehicle->type == Component::Vehicle::Type::Player) { continue; }
vehicle->aiSource = generateSource();
std::cout << "vehicle al source created: " << vehicle->aiSource << std::endl;
}
1
and then the next created is 2
etc etc).