fix the collision bugs

This commit is contained in:
RandomityGuy 2023-12-24 13:01:50 +05:30
parent 472f868e64
commit 351a8d34d9
5 changed files with 141 additions and 155 deletions

View file

@ -135,156 +135,137 @@ class DifBuilder {
var edges = [];
var colliderSurfaces = [];
for (i in 0...hulls.length) {
var hullTris = [];
var hull = hulls[i];
for (j in hull.surfaceStart...(hull.surfaceStart + hull.surfaceCount)) {
var surfaceindex = geo.hullSurfaceIndices[j];
var surface = geo.surfaces[surfaceindex];
if (surface == null)
continue;
var planeindex = surface.planeIndex;
var planeFlipped = (planeindex & 0x8000) == 0x8000;
if (planeFlipped)
planeindex &= ~0x8000;
var plane = geo.planes[planeindex];
var normal = geo.normals[plane.normalIndex];
if (planeFlipped)
normal = normal.scalar(-1);
var texture = geo.materialList[surface.textureIndex];
if (!textures.contains(texture))
textures.push(texture);
var points = geo.points;
var colliderSurface = new CollisionSurface();
colliderSurface.points = [];
colliderSurface.normals = [];
colliderSurface.indices = [];
colliderSurface.transformKeys = [];
colliderSurface.originalIndices = [];
colliderSurface.originalSurfaceIndex = surfaceindex;
for (k in (surface.windingStart + 2)...(surface.windingStart + surface.windingCount)) {
var p1, p2, p3;
if ((k - (surface.windingStart + 2)) % 2 == 0) {
p1 = points[geo.windings[k]];
p2 = points[geo.windings[k - 1]];
p3 = points[geo.windings[k - 2]];
colliderSurface.originalIndices.push(geo.windings[k]);
colliderSurface.originalIndices.push(geo.windings[k - 1]);
colliderSurface.originalIndices.push(geo.windings[k - 2]);
} else {
p1 = points[geo.windings[k - 2]];
p2 = points[geo.windings[k - 1]];
p3 = points[geo.windings[k]];
colliderSurface.originalIndices.push(geo.windings[k - 2]);
colliderSurface.originalIndices.push(geo.windings[k - 1]);
colliderSurface.originalIndices.push(geo.windings[k]);
}
var e1 = new TriangleEdge(geo.windings[k], geo.windings[k - 1], geo.windings[k - 2], surfaceindex);
var e2 = new TriangleEdge(geo.windings[k - 1], geo.windings[k - 2], geo.windings[k], surfaceindex);
var e3 = new TriangleEdge(geo.windings[k], geo.windings[k - 2], geo.windings[k - 1], surfaceindex);
edges.push(e1);
edges.push(e2);
edges.push(e3);
var texgen = geo.texGenEQs[surface.texGenIndex];
var uv1 = new Point2F(p1.x * texgen.planeX.x
+ p1.y * texgen.planeX.y
+ p1.z * texgen.planeX.z
+ texgen.planeX.d,
p1.x * texgen.planeY.x
+ p1.y * texgen.planeY.y
+ p1.z * texgen.planeY.z
+ texgen.planeY.d);
var uv2 = new Point2F(p2.x * texgen.planeX.x
+ p2.y * texgen.planeX.y
+ p2.z * texgen.planeX.z
+ texgen.planeX.d,
p2.x * texgen.planeY.x
+ p2.y * texgen.planeY.y
+ p2.z * texgen.planeY.z
+ texgen.planeY.d);
var uv3 = new Point2F(p3.x * texgen.planeX.x
+ p3.y * texgen.planeX.y
+ p3.z * texgen.planeX.z
+ texgen.planeX.d,
p3.x * texgen.planeY.x
+ p3.y * texgen.planeY.y
+ p3.z * texgen.planeY.z
+ texgen.planeY.d);
var tri = new DifBuilderTriangle();
tri.texture = texture;
tri.normal1 = normal;
tri.normal2 = normal;
tri.normal3 = normal;
tri.p1 = p1;
tri.p2 = p2;
tri.p3 = p3;
tri.uv1 = uv1;
tri.uv2 = uv2;
tri.uv3 = uv3;
triangles.push(tri);
hullTris.push(tri);
var materialName = stripTexName(texture);
var hasMaterialInfo = materialDict.exists(materialName);
if (hasMaterialInfo) {
var minfo = materialDict.get(materialName);
colliderSurface.friction = minfo.friction;
colliderSurface.restitution = minfo.restitution;
}
colliderSurface.addPoint(-p1.x, p1.y, p1.z);
colliderSurface.addPoint(-p2.x, p2.y, p2.z);
colliderSurface.addPoint(-p3.x, p3.y, p3.z);
colliderSurface.addNormal(-normal.x, normal.y, normal.z);
colliderSurface.addNormal(-normal.x, normal.y, normal.z);
colliderSurface.addNormal(-normal.x, normal.y, normal.z);
colliderSurface.indices.push(colliderSurface.indices.length);
colliderSurface.indices.push(colliderSurface.indices.length);
colliderSurface.indices.push(colliderSurface.indices.length);
colliderSurface.transformKeys.push(0);
colliderSurface.transformKeys.push(0);
colliderSurface.transformKeys.push(0);
for (v in [p1, p2, p3]) {
var buckets = vertexBuckets.get(v);
if (buckets == null) {
buckets = [];
vertexBuckets.set(v, buckets);
}
var bucket:VertexBucket = null;
for (j in 0...buckets.length) {
bucket = buckets[j];
if (normal.dot(bucket.referenceNormal) > Math.cos(Math.PI / 12))
break;
bucket = null;
}
if (bucket == null) {
bucket = {
referenceNormal: normal,
triangleIndices: [],
normals: []
};
buckets.push(bucket);
}
bucket.triangleIndices.push(triangles.length - 1);
bucket.normals.push(normal);
}
for (i in 0...geo.surfaces.length) {
var surfaceindex = i;
var surface = geo.surfaces[surfaceindex];
if (surface == null)
continue;
var planeindex = surface.planeIndex;
var planeFlipped = (planeindex & 0x8000) == 0x8000;
if (planeFlipped)
planeindex &= ~0x8000;
var plane = geo.planes[planeindex];
var normal = geo.normals[plane.normalIndex];
if (planeFlipped)
normal = normal.scalar(-1);
var texture = geo.materialList[surface.textureIndex];
if (!textures.contains(texture))
textures.push(texture);
var points = geo.points;
var colliderSurface = new CollisionSurface();
colliderSurface.points = [];
colliderSurface.normals = [];
colliderSurface.indices = [];
colliderSurface.transformKeys = [];
colliderSurface.originalIndices = [];
colliderSurface.originalSurfaceIndex = surfaceindex;
for (k in (surface.windingStart + 2)...(surface.windingStart + surface.windingCount)) {
var p1, p2, p3;
if ((k - (surface.windingStart + 2)) % 2 == 0) {
p1 = points[geo.windings[k]];
p2 = points[geo.windings[k - 1]];
p3 = points[geo.windings[k - 2]];
colliderSurface.originalIndices.push(geo.windings[k]);
colliderSurface.originalIndices.push(geo.windings[k - 1]);
colliderSurface.originalIndices.push(geo.windings[k - 2]);
} else {
p1 = points[geo.windings[k - 2]];
p2 = points[geo.windings[k - 1]];
p3 = points[geo.windings[k]];
colliderSurface.originalIndices.push(geo.windings[k - 2]);
colliderSurface.originalIndices.push(geo.windings[k - 1]);
colliderSurface.originalIndices.push(geo.windings[k]);
}
var e1 = new TriangleEdge(geo.windings[k], geo.windings[k - 1], geo.windings[k - 2], surfaceindex);
var e2 = new TriangleEdge(geo.windings[k - 1], geo.windings[k - 2], geo.windings[k], surfaceindex);
var e3 = new TriangleEdge(geo.windings[k], geo.windings[k - 2], geo.windings[k - 1], surfaceindex);
edges.push(e1);
edges.push(e2);
edges.push(e3);
var texgen = geo.texGenEQs[surface.texGenIndex];
var uv1 = new Point2F(p1.x * texgen.planeX.x
+ p1.y * texgen.planeX.y
+ p1.z * texgen.planeX.z
+ texgen.planeX.d,
p1.x * texgen.planeY.x
+ p1.y * texgen.planeY.y
+ p1.z * texgen.planeY.z
+ texgen.planeY.d);
var uv2 = new Point2F(p2.x * texgen.planeX.x
+ p2.y * texgen.planeX.y
+ p2.z * texgen.planeX.z
+ texgen.planeX.d,
p2.x * texgen.planeY.x
+ p2.y * texgen.planeY.y
+ p2.z * texgen.planeY.z
+ texgen.planeY.d);
var uv3 = new Point2F(p3.x * texgen.planeX.x
+ p3.y * texgen.planeX.y
+ p3.z * texgen.planeX.z
+ texgen.planeX.d,
p3.x * texgen.planeY.x
+ p3.y * texgen.planeY.y
+ p3.z * texgen.planeY.z
+ texgen.planeY.d);
var tri = new DifBuilderTriangle();
tri.texture = texture;
tri.normal1 = normal;
tri.normal2 = normal;
tri.normal3 = normal;
tri.p1 = p1;
tri.p2 = p2;
tri.p3 = p3;
tri.uv1 = uv1;
tri.uv2 = uv2;
tri.uv3 = uv3;
triangles.push(tri);
var materialName = stripTexName(texture).toLowerCase();
var hasMaterialInfo = materialDict.exists(materialName);
if (hasMaterialInfo) {
var minfo = materialDict.get(materialName);
colliderSurface.friction = minfo.friction;
colliderSurface.restitution = minfo.restitution;
}
colliderSurface.addPoint(-p1.x, p1.y, p1.z);
colliderSurface.addPoint(-p2.x, p2.y, p2.z);
colliderSurface.addPoint(-p3.x, p3.y, p3.z);
colliderSurface.addNormal(-normal.x, normal.y, normal.z);
colliderSurface.addNormal(-normal.x, normal.y, normal.z);
colliderSurface.addNormal(-normal.x, normal.y, normal.z);
colliderSurface.indices.push(colliderSurface.indices.length);
colliderSurface.indices.push(colliderSurface.indices.length);
colliderSurface.indices.push(colliderSurface.indices.length);
colliderSurface.transformKeys.push(0);
colliderSurface.transformKeys.push(0);
colliderSurface.transformKeys.push(0);
for (v in [p1, p2, p3]) {
var buckets = vertexBuckets.get(v);
if (buckets == null) {
buckets = [];
vertexBuckets.set(v, buckets);
}
var bucket:VertexBucket = null;
for (j in 0...buckets.length) {
bucket = buckets[j];
if (normal.dot(bucket.referenceNormal) > Math.cos(Math.PI / 12))
break;
bucket = null;
}
if (bucket == null) {
bucket = {
referenceNormal: normal,
triangleIndices: [],
normals: []
};
buckets.push(bucket);
}
bucket.triangleIndices.push(triangles.length - 1);
bucket.normals.push(normal);
}
colliderSurface.generateBoundingBox();
collider.addSurface(colliderSurface);
colliderSurfaces.push(colliderSurface);
}
colliderSurface.generateBoundingBox();
collider.addSurface(colliderSurface);
colliderSurfaces.push(colliderSurface);
}
var edgeMap:Map<Int, TriangleEdge> = new Map();

View file

@ -1131,10 +1131,14 @@ class Marble extends GameObject {
continue;
}
var tsi = Collision.TriangleSphereIntersection(testTri.v[0], testTri.v[1], testTri.v[2], testTri.n, position, radius);
if (tsi.result) {
var separatingDistance = position.sub(tsi.point).normalized();
var distToContactPlane = tsi.point.distance(position);
// Intersection with plane of testTri and current position
var t = (testTri.v[0].sub(position)).dot(testTri.n) / testTri.n.lengthSq();
var intersect = position.add(testTri.n.multiply(t));
var tsi = Collision.PointInTriangle(intersect, testTri.v[0], testTri.v[1], testTri.v[2]);
if (tsi) {
var separatingDistance = position.sub(intersect).normalized();
var distToContactPlane = intersect.distance(position);
if (radius - 0.005 - distToContactPlane > 0.0001) {
// Nudge to the surface of the contact plane
position = position.add(separatingDistance.multiply(radius - distToContactPlane - 0.005));

View file

@ -139,6 +139,7 @@ class PathedInterior extends InteriorObject {
stopped = false;
if (currentTime == targetTime) {
velocity.set(0, 0, 0);
this.collider.velocity.set(0, 0, 0);
} else {
var delta = 0.0;
if (targetTime < 0) {

View file

@ -164,7 +164,7 @@ class CollisionEntity implements IOctreeObject implements IBVHObject {
public function sphereIntersection(collisionEntity:SphereCollisionEntity, timeState:TimeState) {
var position = collisionEntity.transform.getPosition();
var radius = collisionEntity.radius;
var radius = collisionEntity.radius + 0.001;
var invMatrix = invTransform;
if (this.go is PathedInterior)
@ -221,7 +221,7 @@ class CollisionEntity implements IOctreeObject implements IBVHObject {
// var closest = Collision.ClosestPtPointTriangle(position, radius, v0, v, v2, surfacenormal);
if (closest != null) {
var contactDist = closest.distanceSq(position);
if (contactDist <= radius * radius) {
if (contactDist <= radius * radius && contactDist > 0.0225) {
var normal = res.normal;
if (position.sub(closest).dot(surfacenormal) > 0) {

View file

@ -27,7 +27,7 @@ class MainMenuGui extends GuiImage {
versionText.vertSizing = Top;
versionText.position = new Vector(289, 450);
versionText.extent = new Vector(62, 18);
versionText.text.text = "1.1.11";
versionText.text.text = "1.1.12";
versionText.text.textColor = 0;
this.addChild(versionText);