Skip to content

Commit

Permalink
Traktor: Implemented SoundComponent and ListenerComponent in Spray, r…
Browse files Browse the repository at this point in the history
…emoved old "setListener" methods in WorldLayer. Fixed collision callback from physics.
  • Loading branch information
apistol78 committed Jan 19, 2024
1 parent 0c25474 commit bb542a7
Show file tree
Hide file tree
Showing 37 changed files with 822 additions and 281 deletions.
10 changes: 4 additions & 6 deletions code/Physics/Body.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -28,9 +28,7 @@ void Body::addCollisionListener(CollisionListener* collisionListener)

void Body::removeCollisionListener(CollisionListener* collisionListener)
{
RefArray< CollisionListener >::iterator i = std::find(m_collisionListeners.begin(), m_collisionListeners.end(), collisionListener);
if (i != m_collisionListeners.end())
m_collisionListeners.erase(i);
m_collisionListeners.remove(collisionListener);
}

void Body::removeAllCollisionListeners()
Expand All @@ -40,8 +38,8 @@ void Body::removeAllCollisionListeners()

void Body::notifyCollisionListeners(const CollisionInfo& collisionInfo)
{
for (RefArray< CollisionListener >::iterator i = m_collisionListeners.begin(); i != m_collisionListeners.end(); ++i)
(*i)->notify(collisionInfo);
for (auto listener : m_collisionListeners)
listener->notify(collisionInfo);
}

Body::Body(const wchar_t* const tag)
Expand Down
14 changes: 6 additions & 8 deletions code/Physics/Bullet/PhysicsManagerBullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1174,23 +1174,22 @@ void PhysicsManagerBullet::update(float simulationDeltaTime, bool issueCollision
const float dT = simulationDeltaTime * m_timeScale;
m_dynamicsWorld->stepSimulation(dT, 10, 1.0f / m_simulationFrequency);

/*
// Issue collision events.
if (issueCollisionEvents)
{
CollisionInfo info;

int32_t manifoldCount = m_dispatcher->getNumManifolds();
const int32_t manifoldCount = m_dispatcher->getNumManifolds();
for (int32_t i = 0; i < manifoldCount; ++i)
{
btPersistentManifold* manifold = m_dispatcher->getManifoldByIndexInternal(i);
T_ASSERT(manifold);

// Only call to listeners when a new manifold has been created.
if (!manifold->m_fresh)
if (manifold->getContactPoint(0).m_userPersistentData != nullptr)
continue;

int32_t contacts = manifold->getNumContacts();
const int32_t contacts = manifold->getNumContacts();
if (contacts <= 0)
continue;

Expand Down Expand Up @@ -1226,7 +1225,7 @@ void PhysicsManagerBullet::update(float simulationDeltaTime, bool issueCollision
if (!info.contacts.empty())
{
notifyCollisionListeners(info);
manifold->m_fresh = false;
manifold->getContactPoint(0).m_userPersistentData = (void*)1;

// Only issue one new collision per update; distribute over
// several updates to prevent CPU overload.
Expand All @@ -1236,16 +1235,15 @@ void PhysicsManagerBullet::update(float simulationDeltaTime, bool issueCollision
}
else
{
int32_t manifoldCount = m_dispatcher->getNumManifolds();
const int32_t manifoldCount = m_dispatcher->getNumManifolds();
for (int32_t i = 0; i < manifoldCount; ++i)
{
btPersistentManifold* manifold = m_dispatcher->getManifoldByIndexInternal(i);
T_ASSERT(manifold);

manifold->m_fresh = false;
manifold->getContactPoint(0).m_userPersistentData = nullptr;
}
}
*/

m_queryCountLast = m_queryCount;
m_queryCount = 0;
Expand Down
30 changes: 1 addition & 29 deletions code/Runtime/Engine/WorldLayer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -24,7 +24,6 @@
#include "Runtime/Engine/Stage.h"
#include "Runtime/Engine/WorldLayer.h"
#include "Scene/Scene.h"
#include "Sound/Filters/SurroundEnvironment.h"
#include "Spray/Feedback/FeedbackManager.h"
#include "World/IWorldRenderer.h"
#include "World/WorldRenderSettings.h"
Expand Down Expand Up @@ -92,7 +91,6 @@ void WorldLayer::destroy()
m_rootGroup = nullptr;
m_renderGroup = nullptr;
m_cameraEntity = nullptr;
m_listenerEntity = nullptr;

if (m_scene)
{
Expand Down Expand Up @@ -136,7 +134,6 @@ void WorldLayer::preUpdate(const UpdateInfo& info)

// Get initial camera.
m_cameraEntity = getEntity(L"Camera");
m_listenerEntity = m_cameraEntity;
}

// Re-create world renderer.
Expand Down Expand Up @@ -189,21 +186,6 @@ void WorldLayer::preUpdate(const UpdateInfo& info)
}
}
}

// Update sound listener transform.
if (m_environment->getAudio())
{
sound::SurroundEnvironment* surroundEnvironment = m_environment->getAudio()->getSurroundEnvironment();
if (surroundEnvironment && (m_listenerEntity || m_cameraEntity))
{
Transform listenerTransform;
if (m_listenerEntity)
listenerTransform = m_listenerEntity->getTransform();
else
listenerTransform = m_cameraEntity->getTransform();
surroundEnvironment->setListenerTransform(listenerTransform);
}
}
}

void WorldLayer::update(const UpdateInfo& info)
Expand Down Expand Up @@ -618,16 +600,6 @@ const world::Entity* WorldLayer::getCamera() const
return m_cameraEntity;
}

void WorldLayer::setListener(const world::Entity* listenerEntity)
{
m_listenerEntity = listenerEntity;
}

const world::Entity* WorldLayer::getListener() const
{
return m_listenerEntity;
}

void WorldLayer::feedbackValues(spray::FeedbackType type, const float* values, int32_t count)
{
if (type == spray::FbtCamera)
Expand Down
5 changes: 0 additions & 5 deletions code/Runtime/Engine/WorldLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ class T_DLLCLASS WorldLayer

const world::Entity* getCamera() const;

void setListener(const world::Entity* listenerEntity);

const world::Entity* getListener() const;

protected:
virtual void feedbackValues(spray::FeedbackType type, const float* values, int32_t count) override final;

Expand All @@ -164,7 +160,6 @@ class T_DLLCLASS WorldLayer
Ref< world::Entity > m_renderGroup;
Ref< world::Entity > m_dynamicEntities;
Ref< const world::Entity > m_cameraEntity;
Ref< const world::Entity > m_listenerEntity;
IntervalTransform m_cameraTransform;
Transform m_cameraOffset;
double m_alternateTime = 0.0;
Expand Down
1 change: 0 additions & 1 deletion code/Runtime/GameClassFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ void GameClassFactory::createClasses(IRuntimeClassRegistrar* registrar) const
classWorldLayer->addProperty< double >("alternateTime", &WorldLayer::setAlternateTime, &WorldLayer::getAlternateTime);
classWorldLayer->addProperty< float >("feedbackScale", &WorldLayer::setFeedbackScale, &WorldLayer::getFeedbackScale);
classWorldLayer->addProperty< const world::Entity* >("camera", &WorldLayer::setCamera, &WorldLayer::getCamera);
classWorldLayer->addProperty< const world::Entity* >("listener", &WorldLayer::setListener, &WorldLayer::getListener);
classWorldLayer->addMethod("getEntity", &WorldLayer_getEntity_1);
classWorldLayer->addMethod("getEntity", &WorldLayer_getEntity_2);
classWorldLayer->addMethod("getEntities", &WorldLayer::getEntities);
Expand Down
14 changes: 7 additions & 7 deletions code/Runtime/Impl/AudioServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ bool AudioServer::create(const PropertyGroup* settings, const SystemApplication&
}

// Create surround environment.
const float surroundMaxDistance = settings->getProperty< float >(L"Audio.Surround/MaxDistance", 10.0f);
const float surroundInnerRadius = settings->getProperty< float >(L"Audio.Surround/InnerRadius", 1.0f);
const float surroundFallOffExponent = settings->getProperty< float >(L"Audio.Surround/FallOffExponent", 4.0f);
const float surroundMaxDistance = 50.0f; // settings->getProperty< float >(L"Audio.Surround/MaxDistance", 50.0f);
const float surroundInnerRadius = 5.0f; // settings->getProperty< float >(L"Audio.Surround/InnerRadius", 5.0f);
const float surroundFallOffExponent = 4.0f; // settings->getProperty< float >(L"Audio.Surround/FallOffExponent", 4.0f);
m_surroundEnvironment = new sound::SurroundEnvironment(
surroundMaxDistance,
surroundInnerRadius,
Expand Down Expand Up @@ -232,10 +232,10 @@ int32_t AudioServer::reconfigure(const PropertyGroup* settings)
}

// Configure surround environment distances.
const float surroundMaxDistance = settings->getProperty< float >(L"Audio.Surround/MaxDistance", 10.0f);
const float surroundInnerRadius = settings->getProperty< float >(L"Audio.Surround/InnerRadius", 1.0f);
m_surroundEnvironment->setMaxDistance(surroundMaxDistance);
m_surroundEnvironment->setInnerRadius(surroundInnerRadius);
//const float surroundMaxDistance = settings->getProperty< float >(L"Audio.Surround/MaxDistance", 10.0f);
//const float surroundInnerRadius = settings->getProperty< float >(L"Audio.Surround/InnerRadius", 1.0f);
//m_surroundEnvironment->setMaxDistance(surroundMaxDistance);
//m_surroundEnvironment->setInnerRadius(surroundInnerRadius);
return CrAccepted;
}

Expand Down
3 changes: 0 additions & 3 deletions code/Sound/AudioClassFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ void AudioClassFactory::createClasses(IRuntimeClassRegistrar* registrar) const
classSurroundEnvironment->addProperty("maxDistance", &SurroundEnvironment::setMaxDistance, &SurroundEnvironment::getMaxDistance);
classSurroundEnvironment->addProperty("innerRadius", &SurroundEnvironment::setInnerRadius, &SurroundEnvironment::getInnerRadius);
classSurroundEnvironment->addProperty("fullSurround", &SurroundEnvironment::setFullSurround, &SurroundEnvironment::getFullSurround);
classSurroundEnvironment->addProperty("listenerTransform", &SurroundEnvironment::setListenerTransform, &SurroundEnvironment::getListenerTransform);
classSurroundEnvironment->addProperty("listenerTransformInv", &SurroundEnvironment::getListenerTransformInv);
registrar->registerClass(classSurroundEnvironment);

auto classCombFilter = new AutoRuntimeClass< CombFilter >();
Expand Down Expand Up @@ -175,7 +173,6 @@ void AudioClassFactory::createClasses(IRuntimeClassRegistrar* registrar) const
registrar->registerClass(classISoundHandle);

auto classISoundPlayer = new AutoRuntimeClass< ISoundPlayer >();
classISoundPlayer->addProperty("listenerTransform", &ISoundPlayer::setListenerTransform, &ISoundPlayer::getListenerTransform);
classISoundPlayer->addMethod< Ref< ISoundHandle >, const Sound*, uint32_t >("play", &ISoundPlayer::play);
classISoundPlayer->addMethod< Ref< ISoundHandle >, const Sound*, const Vector4&, uint32_t, bool >("play", &ISoundPlayer::play);
registrar->registerClass(classISoundPlayer);
Expand Down
14 changes: 4 additions & 10 deletions code/Sound/Filters/SurroundEnvironment.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Sound/Filters/SurroundEnvironment.h"

namespace traktor
namespace traktor::sound
{
namespace sound
{

T_IMPLEMENT_RTTI_CLASS(L"traktor.sound.SurroundEnvironment", SurroundEnvironment, Object)

Expand All @@ -25,8 +23,6 @@ SurroundEnvironment::SurroundEnvironment(
, m_innerRadius(innerRadius)
, m_fallOffExponent(fallOffExponent)
, m_fullSurround(fullSurround)
, m_listenerTransform(Transform::identity())
, m_listenerTransformInv(Transform::identity())
{
}

Expand All @@ -50,11 +46,9 @@ void SurroundEnvironment::setFullSurround(bool fullSurround)
m_fullSurround = fullSurround;
}

void SurroundEnvironment::setListenerTransform(const Transform& listenerTransform)
void SurroundEnvironment::setListenerTransforms(const AlignedVector< Transform >& listenerTransforms)
{
m_listenerTransform = listenerTransform;
m_listenerTransformInv = listenerTransform.inverse();
m_listenerTransforms = listenerTransforms;
}

}
}
20 changes: 7 additions & 13 deletions code/Sound/Filters/SurroundEnvironment.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -9,6 +9,7 @@
#pragma once

#include "Core/Object.h"
#include "Core/Containers/AlignedVector.h"
#include "Core/Math/Transform.h"

// import/export mechanism.
Expand All @@ -19,17 +20,15 @@
# define T_DLLCLASS T_DLLIMPORT
#endif

namespace traktor
namespace traktor::sound
{
namespace sound
{

class T_DLLCLASS SurroundEnvironment : public Object
{
T_RTTI_CLASS;

public:
SurroundEnvironment(
explicit SurroundEnvironment(
float maxDistance,
float innerRadius,
float fallOffExponent,
Expand All @@ -52,21 +51,16 @@ class T_DLLCLASS SurroundEnvironment : public Object

bool getFullSurround() const { return m_fullSurround; }

void setListenerTransform(const Transform& listenerTransform);
void setListenerTransforms(const AlignedVector< Transform >& listenerTransforms);

const Transform& getListenerTransform() const { return m_listenerTransform; }

const Transform& getListenerTransformInv() const { return m_listenerTransformInv; }
const AlignedVector< Transform >& getListenerTransforms() const { return m_listenerTransforms; }

private:
Scalar m_maxDistance;
Scalar m_innerRadius;
Scalar m_fallOffExponent;
bool m_fullSurround;
Transform m_listenerTransform;
Transform m_listenerTransformInv;
AlignedVector< Transform > m_listenerTransforms;
};

}
}

Loading

0 comments on commit bb542a7

Please sign in to comment.