Skip to content

Commit

Permalink
Make Velocity saved in a particle instance
Browse files Browse the repository at this point in the history
  • Loading branch information
durswd committed Aug 27, 2023
1 parent 28aabbc commit b0c5cdd
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
33 changes: 24 additions & 9 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ void Instance::Initialize(Instance* parent, float spawnDeltaFrame, int32_t insta
m_randObject.SetSeed(instanceGlobal->GetRandObject().GetRandInt());

prevPosition_ = SIMD::Vec3f(0, 0, 0);
prevLocalVelocity_ = SIMD::Vec3f(0, 0, 0);
}

void Instance::FirstUpdate()
Expand Down Expand Up @@ -375,6 +376,7 @@ void Instance::FirstUpdate()
}

prevGlobalPosition_ = SIMD::Vec3f::Transform(prevPosition_, m_ParentMatrix);
prevLocalVelocity_ = SIMD::Vec3f(0, 0, 0);

m_pEffectNode->InitializeRenderedInstance(*this, *ownGroup_, m_pManager);
}
Expand Down Expand Up @@ -638,7 +640,9 @@ void Instance::UpdateTransform(float deltaFrame)
}

SIMD::Vec3f localPosition = prevPosition_;
SIMD::Vec3f localVelocity;
SIMD::Vec3f localVelocity = prevLocalVelocity_;

SIMD::Vec3f localAcc = SIMD::Vec3f(0, 0, 0);

if (m_pEffectNode->CommonValues.TranslationBindType == TranslationParentBindType::NotBind_FollowParent ||
m_pEffectNode->CommonValues.TranslationBindType == TranslationParentBindType::WhenCreating_FollowParent)
Expand All @@ -652,6 +656,7 @@ void Instance::UpdateTransform(float deltaFrame)
toTarget *= followParentParam.maxFollowSpeed;
}

auto prevSteering = steeringVec_;
SIMD::Vec3f vSteering = toTarget - steeringVec_;
vSteering *= followParentParam.steeringSpeed;

Expand All @@ -663,34 +668,44 @@ void Instance::UpdateTransform(float deltaFrame)
steeringVec_ *= followParentParam.maxFollowSpeed;
}

localVelocity = steeringVec_ * deltaFrame * m_pEffectNode->m_effect->GetMaginification();
localAcc = (steeringVec_ - prevSteering) * deltaFrame * m_pEffectNode->m_effect->GetMaginification();
localVelocity = steeringVec_;
}
else
{
localVelocity = m_pEffectNode->TranslationParam.CalculateTranslationState(translation_values, m_randObject, m_pEffectNode->GetEffect(), m_pContainer->GetRootInstance(), m_LivingTime, m_LivedTime, m_pParent, coordinateSystem, m_pEffectNode->DynamicFactor);
localAcc = m_pEffectNode->TranslationParam.CalculateTranslationState(translation_values, m_randObject, m_pEffectNode->GetEffect(), m_pContainer->GetRootInstance(), m_LivingTime, m_LivedTime, m_pParent, coordinateSystem, m_pEffectNode->DynamicFactor);

if (m_pEffectNode->GenerationLocation.EffectsRotation)
{
// TODO : check rotation(It seems has bugs and it can optimize it)
localVelocity = SIMD::Vec3f::Transform(localVelocity, m_GenerationLocation.Get3x3SubMatrix());
localAcc = SIMD::Vec3f::Transform(localAcc, m_GenerationLocation.Get3x3SubMatrix());
}
}
localVelocity += localAcc;

localPosition += localVelocity;
}

auto matRot = RotationFunctions::CalculateRotation(rotation_values, m_pEffectNode->RotationParam, m_randObject, m_pEffectNode->GetEffect(), m_pContainer->GetRootInstance(), m_LivingTime, m_LivedTime, m_pParent, m_pEffectNode->DynamicFactor, m_pManager->GetLayerParameter(GetInstanceGlobal()->GetLayer()).ViewerPosition);
auto scaling = ScalingFunctions::UpdateScaling(scaling_values, m_pEffectNode->ScalingParam, m_randObject, m_pEffectNode->GetEffect(), m_pContainer->GetRootInstance(), m_LivingTime, m_LivedTime, m_pParent, m_pEffectNode->DynamicFactor);

// update local fields
if (m_pEffectNode->LocalForceField.HasValue)
{
forceField_.ExternalVelocity = localVelocity;
localPosition += forceField_.Update(m_pEffectNode->LocalForceField, localPosition, m_pEffectNode->GetEffect()->GetMaginification(), deltaFrame, m_pEffectNode->GetEffect()->GetSetting()->GetCoordinateSystem());
// for compatibiliy
auto new_local_position = localPosition + localVelocity;

localVelocity += forceField_.Update(
m_pEffectNode->LocalForceField,
new_local_position,
localVelocity,
m_pEffectNode->GetEffect()->GetMaginification(),
deltaFrame,
m_pEffectNode->GetEffect()->GetSetting()->GetCoordinateSystem());
}

localPosition += localVelocity;
prevPosition_ = localPosition;
prevLocalVelocity_ = localVelocity;

/* 描画部分の更新 */
m_pEffectNode->UpdateRenderedInstance(*this, *ownGroup_, m_pManager);

// Update matrix
Expand Down
3 changes: 3 additions & 0 deletions Dev/Cpp/Effekseer/Effekseer/Effekseer.Instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ class alignas(16) Instance : public IntrusiveList<Instance>::Node
SIMD::Vec3f prevPosition_;
SIMD::Vec3f prevGlobalPosition_;

SIMD::Vec3f prevLocalVelocity_;

SIMD::Vec3f parentPosition_;

SIMD::Vec3f steeringVec_;

public:
Expand Down
18 changes: 10 additions & 8 deletions Dev/Cpp/Effekseer/Effekseer/ForceField/ForceFields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,17 @@ void LocalForceFieldParameter::MaintainAttractiveForceCompatibility(const float
LocalForceFields[3].IsGlobal = true;
}

SIMD::Vec3f LocalForceFieldInstance::Update(const LocalForceFieldParameter& parameter, const SIMD::Vec3f& location, float magnification, float deltaFrame, CoordinateSystem coordinateSystem)
SIMD::Vec3f LocalForceFieldInstance::Update(const LocalForceFieldParameter& parameter, const SIMD::Vec3f& location, const SIMD::Vec3f& local_velocity_per_update, float magnification, float deltaFrame, CoordinateSystem coordinateSystem)
{
if (deltaFrame == 0.0f)
{
return SIMD::Vec3f{0, 0, 0};
}

std::array<SIMD::Vec3f, LocalFieldSlotMax> accs;
accs.fill(SIMD::Vec3f(0, 0, 0));


for (size_t i = 0; i < parameter.LocalForceFields.size(); i++)
{
auto& field = parameter.LocalForceFields[i];
Expand All @@ -376,7 +380,7 @@ SIMD::Vec3f LocalForceFieldInstance::Update(const LocalForceFieldParameter& para
ForceFieldCommonParameter ffcp;
ffcp.FieldCenter = parameter.LocalForceFields[i].Position;
ffcp.Position = location / magnification;
ffcp.PreviousSumVelocity = (VelocitySum + ExternalVelocity / deltaFrame) / magnification;
ffcp.PreviousSumVelocity = (local_velocity_per_update / deltaFrame) / magnification;
ffcp.PreviousVelocity = Velocities[i] / magnification;
ffcp.DeltaFrame = deltaFrame;
ffcp.IsFieldRotated = field.IsRotated;
Expand Down Expand Up @@ -455,19 +459,19 @@ SIMD::Vec3f LocalForceFieldInstance::Update(const LocalForceFieldParameter& para
}

Velocities[i] += acc;
accs[i] = acc;
}

VelocitySum = SIMD::Vec3f(0, 0, 0);
SIMD::Vec3f acc_sum = SIMD::Vec3f(0, 0, 0);

for (size_t i = 0; i < parameter.LocalForceFields.size(); i++)
{
if (parameter.LocalForceFields[i].IsGlobal)
continue;

VelocitySum += Velocities[i];
acc_sum += accs[i];
}

return VelocitySum * deltaFrame;
return acc_sum * deltaFrame;
}

void LocalForceFieldInstance::UpdateGlobal(const LocalForceFieldParameter& parameter, const SIMD::Vec3f& location, float magnification, const SIMD::Vec3f& targetPosition, float deltaFrame, CoordinateSystem coordinateSystem)
Expand Down Expand Up @@ -576,10 +580,8 @@ void LocalForceFieldInstance::UpdateGlobal(const LocalForceFieldParameter& param
void LocalForceFieldInstance::Reset()
{
Velocities.fill(SIMD::Vec3f(0, 0, 0));
VelocitySum = SIMD::Vec3f(0, 0, 0);
GlobalVelocitySum = SIMD::Vec3f(0, 0, 0);
GlobalModifyLocation = SIMD::Vec3f(0, 0, 0);
ExternalVelocity = SIMD::Vec3f(0, 0, 0);
}

} // namespace Effekseer
5 changes: 1 addition & 4 deletions Dev/Cpp/Effekseer/Effekseer/ForceField/ForceFields.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,10 @@ struct LocalForceFieldInstance
{
std::array<SIMD::Vec3f, LocalFieldSlotMax> Velocities;

SIMD::Vec3f ExternalVelocity;
SIMD::Vec3f VelocitySum;

SIMD::Vec3f GlobalVelocitySum;
SIMD::Vec3f GlobalModifyLocation;

SIMD::Vec3f Update(const LocalForceFieldParameter& parameter, const SIMD::Vec3f& location, float magnification, float deltaFrame, CoordinateSystem coordinateSystem);
SIMD::Vec3f Update(const LocalForceFieldParameter& parameter, const SIMD::Vec3f& location, const SIMD::Vec3f& local_velocity_per_update, float magnification, float deltaFrame, CoordinateSystem coordinateSystem);

void UpdateGlobal(const LocalForceFieldParameter& parameter, const SIMD::Vec3f& location, float magnification, const SIMD::Vec3f& targetPosition, float deltaTime, CoordinateSystem coordinateSystem);

Expand Down
6 changes: 5 additions & 1 deletion Dev/Cpp/Effekseer/Effekseer/Parameter/Translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ struct InstanceTranslationState
};

SIMD::Vec3f prevLocation;
SIMD::Vec3f prevVelocity;
};

class TranslationParameter
Expand Down Expand Up @@ -383,6 +384,7 @@ class TranslationParameter
}

instanceState.prevLocation = position;
instanceState.prevVelocity = SIMD::Vec3f(0,0,0);
}

SIMD::Vec3f CalculateTranslationState(
Expand Down Expand Up @@ -470,8 +472,10 @@ class TranslationParameter
}

const auto vel = localPosition - instanceState.prevLocation;
const auto acc = vel - instanceState.prevVelocity;
instanceState.prevLocation = localPosition;
return vel;
instanceState.prevVelocity = vel;
return acc;
}
};

Expand Down

0 comments on commit b0c5cdd

Please sign in to comment.