Fixed shadow pass and physics system a little bit

This commit is contained in:
Dynamitos committed 2026-02-25 07:13:54 +01:00
1 parent 2e63b3cb83
commit 61dcb0430a
2 files changed
+46 -25

No files matched your search

+36 -20
View File
@@ -1,6 +1,5 @@
#include "PhysicsSystem.h"
#include <iostream>
#include <random>
using namespace Seele;
@@ -466,27 +465,44 @@ void PhysicsSystem::computeBVector(const Array<Contact>& contacts, Array<float>&
}
}
void PhysicsSystem::solveQP(const Array<Array<float>>& CI, const Array<float>& ci0, Array<float>& sol) const {
static std::mt19937_64 generator;
static std::uniform_real_distribution<float> dist(0.01f, 0.1f);
sol.resize(CI.size());
bool solved = false;
while (!solved) {
for (size_t i = 0; i < sol.size(); ++i) {
sol[i] = dist(generator);
}
solved = true;
for (size_t i = 0; i < sol.size(); ++i) {
float res = 0;
for (size_t j = 0; j < sol.size(); ++j) {
res += CI[i][j] * sol[j] + ci0[i];
void PhysicsSystem::solveQP(const Array<Array<float>>& A, const Array<float>& b, Array<float>& f) const {
// Solves the LCP: find f >= 0 such that Af + b >= 0 and f^T(Af + b) = 0
// using Projected Gauss-Seidel (PGS) iteration
const size_t n = A.size();
f.resize(n);
for (size_t i = 0; i < n; ++i) {
f[i] = 0.0f;
}
constexpr size_t maxIterations = 200;
constexpr float tolerance = 1e-6f;
for (size_t iter = 0; iter < maxIterations; ++iter) {
float maxDelta = 0.0f;
for (size_t i = 0; i < n; ++i) {
float sigma = b[i];
for (size_t j = 0; j < n; ++j) {
if (j != i) {
sigma += A[i][j] * f[j];
}
}
if (res < 0) {
solved = false;
std::cout << "failed to solve QP" << std::endl;
continue;
float diagonal = A[i][i];
float newF;
if (std::abs(diagonal) > 1e-10f) {
newF = std::max(0.0f, -sigma / diagonal);
} else {
newF = 0.0f;
}
maxDelta = std::max(maxDelta, std::abs(newF - f[i]));
f[i] = newF;
}
if (maxDelta < tolerance) {
return;
}
return;
}
}