Skip to content

Commit

Permalink
restore drag in rotational motion, object mass distributed through pa…
Browse files Browse the repository at this point in the history
…rticles

mesh.lua correctly placing mesh points for concave polygons
  • Loading branch information
Jerboa-app committed Nov 24, 2023
1 parent ae58812 commit 2806c43
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 144 deletions.
35 changes: 20 additions & 15 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ DEMO=0
ANDROID_NDK=""
BENCHMARK=0
STANDALONE=0
CLEAN=1

while [[ $# -gt 0 ]]; do
case $1 in
Expand Down Expand Up @@ -109,6 +110,10 @@ while [[ $# -gt 0 ]]; do
BENCHMARK=1
shift
;;
-c|--continue)
CLEAN=0
shift
;;
-*|--*)
echo "Unknown option $1"
exit 1
Expand All @@ -120,24 +125,21 @@ while [[ $# -gt 0 ]]; do
esac
done

for file in build CMakeFiles cmake_install.cmake CMakeCache.txt Makefile Jerboa
do
if [ -d $file ];
then
rm -rf $file
fi
if [ -f $file ];
if [[ $CLEAN -eq 1 ]];
then
if [ -d build ];
then
rm $file
rm -rf build
fi
done
cmake -E make_directory build
fi

echo "release ${RELEASE}"

if [[ $WINDOWS -eq 0 ]];
then
cmake -E make_directory build
cmake -E chdir build cmake .. -D WINDOWS=ON -D STANDALONE=$STANDALONE -D BUILD_DEMOS=$DEMO -D RELEASE=$RELEASE -D BENCHMARK=$BENCHMARK -D TEST_SUITE=$TEST -D SYNTAX_ONLY=$SYNTAX -D SANITISE=$SANITISE -D CMAKE_TOOLCHAIN_FILE=./windows.cmake && make -j 8 -C build
cd build
cmake .. -D WINDOWS=ON -D STANDALONE=$STANDALONE -D BUILD_DEMOS=$DEMO -D RELEASE=$RELEASE -D BENCHMARK=$BENCHMARK -D TEST_SUITE=$TEST -D SYNTAX_ONLY=$SYNTAX -D SANITISE=$SANITISE -D CMAKE_TOOLCHAIN_FILE=./windows.cmake && make -j 8
# now copy dlls
PREFIX="x86_64-w64-mingw32"

Expand Down Expand Up @@ -179,10 +181,12 @@ then
cp demo/desktop/include/SFML-2.5.1-mingw64/bin/sfml-window-d-2.dll build/
cp demo/desktop/include/SFML-2.5.1-mingw64/bin/sfml-graphics-d-2.dll build/
fi
cd ..
elif [[ $OSX -eq 0 ]];
then
cmake -E make_directory build
cmake -E chdir build cmake .. -D OSX=ON -D STANDALONE=$STANDALONE -D BUILD_DEMOS=$DEMO -D RELEASE=$RELEASE -D BENCHMARK=$BENCHMARK -D TEST_SUITE=$TEST -D SYNTAX_ONLY=$SYNTAX -D SANITISE=$SANITISE -D CMAKE_TOOLCHAIN_FILE=./osx.cmake && make -j 8 -C build
cd build
cmake .. -D OSX=ON -D STANDALONE=$STANDALONE -D BUILD_DEMOS=$DEMO -D RELEASE=$RELEASE -D BENCHMARK=$BENCHMARK -D TEST_SUITE=$TEST -D SYNTAX_ONLY=$SYNTAX -D SANITISE=$SANITISE -D CMAKE_TOOLCHAIN_FILE=./osx.cmake && make -j 8
cd ..
elif [[ ! -z "$ANDROID_NDK" ]]
then
TOOL_CHAIN="$ANDROID_NDK/build/cmake/android.toolchain.cmake"
Expand All @@ -200,8 +204,9 @@ then
buildAndroid x86_64

else
cmake -E make_directory build
cmake -E chdir build cmake -D BUILD_DEMOS=$DEMO -D STANDALONE=$STANDALONE -D RELEASE=$RELEASE -D BENCHMARK=$BENCHMARK -D TEST_SUITE=$TEST -D SANITISE=$SANITISE -D SYNTAX_ONLY=$SYNTAX .. && make -j 8 -C build
cd build
cmake -D BUILD_DEMOS=$DEMO -D STANDALONE=$STANDALONE -D RELEASE=$RELEASE -D BENCHMARK=$BENCHMARK -D TEST_SUITE=$TEST -D SANITISE=$SANITISE -D SYNTAX_ONLY=$SYNTAX .. && make -j 8
cd ..
fi


Expand Down
2 changes: 1 addition & 1 deletion config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ config =
{
["timeStep"] = 1.0/1800.0,
["subSample"] = 2,
["cofr"] = 0.75
["cofr"] = 0.75
}

hop.configure(config);
4 changes: 2 additions & 2 deletions include/Debug/collisionMeshDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ namespace Hop::Debugging
"texCoord = pos;\n"
"parameters.xy = rot * a_parameters.xy;\n"
"parameters.zw = rot * a_parameters.zw;\n"
"oColour = vec4(0.0,0.0,1.0,1.0);\n"
"oColour = vec4(200.0/255.0,200.0/255.0,250.0/255.0,1.0);\n"
"th = a_thickness;\n"
"}";

Expand All @@ -173,7 +173,7 @@ namespace Hop::Debugging
"q = mat2(d.x,-d.y,d.y,d.x)*q;\n"
"q = abs(q)-vec2(l*0.5,th);\n"
"float sd = length(max(q,0.0)) + min(max(q.x,q.y),0.0);"
"float alpha = 0.1;"
"float alpha = 1.0;"
"if (sd <= 0.0) { colour = vec4(oColour.rgb,0.5); }"
//"if (sd <= 0.1){ alpha = 1.0-smoothstep(0.0,0.1,sd); colour = vec4(oColour.rgb,alpha); }"
"else { colour = vec4(oColour.rgb,alpha); }"
Expand Down
2 changes: 1 addition & 1 deletion src/Collision/collisionMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ namespace Hop::System::Physics
}
}

return m;
return m * (1.0/float(size()));
}

std::ostream & operator<<(std::ostream & o, Rectangle const & r)
Expand Down
24 changes: 12 additions & 12 deletions src/System/sPhysics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ namespace Hop::System::Physics
ComponentArray<cPhysics> & physics = m->getComponentArray<cPhysics>();
ComponentArray<cTransform> & transforms = m->getComponentArray<cTransform>();

double nx, ny, ntheta, at, bt, ct, sticktion, r, dx, dy, d;
double nx, ny, ntheta, at, bt, ct, sticktion, r, dx, dy, d, ar, br, cr;

double DT_OVER_TWICE_MASS = dt / (2.0*PARTICLE_MASS);

Expand All @@ -184,12 +184,12 @@ namespace Hop::System::Physics
bt = 1.0/(1.0+ct);
at = (1.0-ct)*bt;

if (collideables.hasComponent(*it))
{
cCollideable & data = collideables.get(*it);
r = data.mesh.getRadius();
dataP.tau -= dataP.phi * 50.0 * r*r;
}
// if (collideables.hasComponent(*it))
// {
// cCollideable & data = collideables.get(*it);
// r = data.mesh.getRadius();
// dataP.tau -= dataP.phi * 50.0 * r*r;
// }

sticktion = std::sqrt(dataP.fx*dataP.fx+dataP.fy*dataP.fy);

Expand Down Expand Up @@ -234,18 +234,18 @@ namespace Hop::System::Physics

dataP.omega += dataP.tau;

// cr = dt * dataP.rotationalDrag / (2.0*dataP.momentOfInertia);
// br = 1.0/(1.0+cr);
// ar = (1.0-cr)*br;
cr = dt * dataP.rotationalDrag / (2.0*dataP.momentOfInertia);
br = 1.0/(1.0+cr);
ar = (1.0-cr)*br;

// std::cout << dataP.omega << "\n";
// if (sticktion < 9.81){
// dataP.omega *= std::min((dataP.omega * dataP.omega), 1.0);
// }

ntheta = 2.0*dataT.theta - dataP.lastTheta + dtdt*dataP.omega/dataP.momentOfInertia;
// ntheta = 2.0*dataT.theta - dataP.lastTheta + dtdt*dataP.omega/dataP.momentOfInertia;

// ntheta = 2.0*br*dataT.theta-ar*dataP.lastTheta+br*dataP.omega*dtdt/dataP.momentOfInertia;
ntheta = 2.0*br*dataT.theta-ar*dataP.lastTheta+br*dataP.omega*dtdt/dataP.momentOfInertia;

dataP.phi = (ntheta-dataP.lastTheta)/(2.0*dt);

Expand Down
58 changes: 1 addition & 57 deletions tests/DEM.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,7 @@ D_vertices =
{0.08, -0.5}
}

E_vertices =
{
{0.1, -0.5},
{-0.5, -0.5},
{-0.5, 0.5},
{0.25, 0.5},
{},
{-0.5, 0.0},
{0.25, 0.0},
{}
}

M_vertices =
{
{-0.5, -0.5},
{-0.5, 0.5},
{0.0, 0.0},
{0.5, 0.5},
{0.5, -0.5},
{}
}

E_mesh = createMesh(E_vertices, 0.1)
-- table.remove(E_mesh, 1)
D_mesh = createMesh(D_vertices, 0.1)
M_mesh = createMesh(M_vertices, 0.1)

s = 6.8*hop.maxCollisionPrimitiveSize()

Expand All @@ -57,7 +32,7 @@ for i = 1, 8 do
o = {

["transform"] = {x,y,0.0,s},
-- ["colour"] = {200/255,200/255,250/255,1.0},
["colour"] = {200/255,200/255,250/255,1.0},
-- ["shader"] = "circleObjectShader",
-- ["shader"] = "lineSegmentObjectShader",
["moveable"] = true,
Expand All @@ -68,35 +43,4 @@ for i = 1, 8 do

hop.loadObject(o)

x = x + s

o = {

["transform"] = {x,y,0.0,s},
-- ["colour"] = {200/255,200/255,250/255,1.0},
-- ["shader"] = "circleObjectShader",
-- ["shader"] = "lineSegmentObjectShader",
["moveable"] = true,
["collisionMesh"] = E_mesh,
["name"] = ""

}

hop.loadObject(o)

x = x + s*0.75

o = {

["transform"] = {x,y,0.0,s},
-- ["colour"] = {200/255,200/255,250/255,1.0},
-- ["shader"] = "circleObjectShader",
-- ["shader"] = "lineSegmentObjectShader",
["moveable"] = true,
["collisionMesh"] = M_mesh,
["name"] = ""

}

hop.loadObject(o)
end
112 changes: 58 additions & 54 deletions tests/mesh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ function createMesh(vertices, radius)
v = {}
N = #vertices

place = {0.0, 0.0}
place[1] = vertices[1][1]
place[2] = vertices[1][2]
lastPlace = {0.0, 0.0}
lastPlace[1] = place[1]
lastPlace[2] = place[2]
table.insert(v, {place[1], place[2], radius})
postCorner = false
cornerDist = 0.0

for i = 1, N do

if (#vertices[i] > 0 and #vertices[next(i, N)] > 0) then
Expand All @@ -25,68 +35,62 @@ function createMesh(vertices, radius)
n[2] = vertices[next(i, N)][2] - vertices[i][2]

d = norm(n)
diff = {place[1]-lastPlace[1], place[2]-lastPlace[2]}

if (d > 0) then
dm = math.floor(d/(2.0*radius))
dm = math.max(dm, 2)
m = 0.0
for j = 0, dm do
c = {0.0, 0.0, radius}
c[1] = vertices[i][1] + n[1] * m/d
c[2] = vertices[i][2] + n[2] * m/d
table.insert(v, c)
m = m + 2.0*radius

l = 0.0

while l <= d do

if postCorner then
l = cornerDist
postCorner = false
end
nextPlace = {vertices[i][1]+l*n[1]/d, vertices[i][2]+l*n[2]/d}
if (l <= d) then
if (i == N) then
diff = {nextPlace[1]-v[1][1], nextPlace[2]-v[1][2]}
if (norm(diff)<radius) then
break
end
end
-- can place, not off end
place[1] = nextPlace[1]
place[2] = nextPlace[2]
print(place[1], place[2])
table.insert(v, {place[1], place[2], radius})
end

l = l + radius*2.0
end

end

end

end

n = {0.0, 0.0}

n[1] = vertices[next(1, N)][1] - vertices[1][1]
n[2] = vertices[next(1, N)][2] - vertices[1][2]

nt = {n[2], -n[1]}
dnt = norm(nt)
nt[1] = nt[1] / dnt
nt[2] = nt[2] / dnt

d = norm(n)

t = {0.0, 0.0}
t[1] = vertices[1][1] + n[1] * 0.5
t[2] = vertices[1][2] + n[2] * 0.5

c = {0.0, 0.0}
c[1] = t[1] + nt[1]*radius
c[2] = t[2] + nt[2]*radius

com = {0.0, 0.0}

for i = 1, N do
if (#vertices[i] > 0) then
com[1] = com[1] + vertices[i][1]
com[2] = com[2] + vertices[i][2]
lastPlace[1] = place[1]
lastPlace[2] = place[2]

postCorner = true
l = norm({vertices[next(i, N)][1]-lastPlace[1], vertices[next(i, N)][2]-lastPlace[2]})
ai = i
bi = next(i,N)
ci = next(next(i,N),N)

s = {vertices[ai][1] - vertices[bi][1], vertices[ai][2] - vertices[bi][2]}
t = {vertices[ci][1] - vertices[bi][1], vertices[ci][2] - vertices[bi][2]}

ctheta = (s[1]*t[1]+s[2]*t[2])/(norm(s)*norm(t))
if (math.abs(ctheta) < 1e-3) then
cornerDist = math.sqrt(l*l+4.0*radius*radius)
elseif ((ctheta*ctheta-1.0)*l*l+4.0*radius*radius < 0.0) then
cornerDist = math.sqrt(l*l+4.0*radius*radius)
else
s1 = l*ctheta + math.sqrt((ctheta*ctheta-1.0)*l*l+4.0*radius*radius)
s2 = l*ctheta - math.sqrt((ctheta*ctheta-1.0)*l*l+4.0*radius*radius)
cornerDist = math.max(s1,s2)
end
end
com[1] = com[1] / N
com[2] = com[2] / N

a = {0.0, 0.0}
b = {0.0, 0.0}

a[1] = c[1]-com[1]
a[2] = c[2]-com[2]
b[1] = t[1]-com[1]
b[2] = t[2]-com[2]

s = norm(a) / norm(b)

for i = 1, #v do
v[i][1] = s*(v[i][1]-com[1])+com[1]
v[i][2] = s*(v[i][2]-com[2])+com[2]
end

return v
Expand Down
2 changes: 1 addition & 1 deletion tests/rectangles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ s = 0.9*hop.maxCollisionPrimitiveSize()

math.randomseed(1)

for i = 1,100 do
for i = 1,2000 do

-- x = 0.897; y = 0.6;
x = 0.85+math.random(); y = math.random()+0.6;
Expand Down
2 changes: 1 addition & 1 deletion tests/runtest.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require("tests/mix")
require("tests/DEM")
Loading

0 comments on commit 2806c43

Please sign in to comment.