added basic frictions and cleanup

This commit is contained in:
RandomityGuy 2021-05-31 15:54:09 +05:30
parent 38f77c3003
commit 7b2292739e
4 changed files with 64 additions and 224 deletions

View file

@ -38,7 +38,38 @@ class DifBuilderTriangle {
}
class DifBuilder {
public static function loadDif(path:String, loader:Loader) {
static var materialDict = [
"friction_none" => {
friction: 0.01,
restitution: 0.5
},
"friction_low" => {
friction: 0.2,
restitution: 0.5
},
"friction_high" => {
friction: 1.5,
restitution: 0.5
},
"friction_ramp_yellow" => {
friction: 2.0,
restitution: 1.0
},
"oilslick" => {
friction: 0.05,
restitution: 0.5
},
"base.slick" => {
friction: 0.05,
restitution: 0.5
},
"ice.slick" => {
friction: 0.05,
restitution: 0.5
}
];
public static function loadDif(path:String, loader:Loader, itr:InteriorGeometry) {
var dif = Dif.Load(path);
var geo = dif.interiors[0];
@ -50,6 +81,18 @@ class DifBuilder {
var collider = new CollisionEntity();
function stripTexName(tex:String) {
var dotpos = tex.lastIndexOf(".");
var slashpos = tex.lastIndexOf("/") + 1;
if (dotpos == -1) {
dotpos = tex.length;
}
if (slashpos == -1) {
slashpos = 0;
}
return tex.substring(slashpos, dotpos);
}
for (i in 0...hulls.length) {
var hullTris = [];
var hull = hulls[i];
@ -132,220 +175,13 @@ class DifBuilder {
tri.uv3 = uv3;
triangles.push(tri);
hullTris.push(tri);
colliderSurface.points.push(new Vector(-p1.x, p1.y, p1.z));
colliderSurface.points.push(new Vector(-p2.x, p2.y, p2.z));
colliderSurface.points.push(new Vector(-p3.x, p3.y, p3.z));
colliderSurface.normals.push(new Vector(-normal.x, normal.y, normal.z));
colliderSurface.normals.push(new Vector(-normal.x, normal.y, normal.z));
colliderSurface.normals.push(new Vector(-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.generateBoundingBox();
collider.addSurface(colliderSurface);
}
}
var mats = new Map<String, Array<DifBuilderTriangle>>();
for (index => value in triangles) {
if (mats.exists(value.texture)) {
mats[value.texture].push(value);
} else {
mats.set(value.texture, [value]);
}
}
collider.generateBoundingBox();
var ig = new InteriorGeometry();
ig.collider = collider;
function canFindTex(tex:String) {
if (tex.indexOf('/') != -1) {
tex = tex.split('/')[1];
}
if (File.exists(Path.directory(path) + "/" + tex + ".jpg")) {
return true;
}
if (File.exists(Path.directory(path) + "/" + tex + ".png")) {
return true;
}
var prevDir = Path.directory(Path.directory(path));
if (File.exists(prevDir + "/" + tex + ".jpg")) {
return true;
}
if (File.exists(prevDir + "/" + tex + ".png")) {
return true;
}
return false;
}
function tex(tex:String):String {
if (tex.indexOf('/') != -1) {
tex = tex.split('/')[1];
}
if (File.exists(Path.directory(path) + "/" + tex + ".jpg")) {
return Path.directory(path) + "/" + tex + ".jpg";
}
if (File.exists(Path.directory(path) + "/" + tex + ".png")) {
return Path.directory(path) + "/" + tex + ".png";
}
var prevDir = Path.directory(Path.directory(path));
if (File.exists(prevDir + "/" + tex + ".jpg")) {
return prevDir + "/" + tex + ".jpg";
}
if (File.exists(prevDir + "/" + tex + ".png")) {
return prevDir + "/" + tex + ".png";
}
return null;
}
for (grp => tris in mats) {
var points = [];
var normals = [];
var uvs = [];
for (tri in tris) {
var p1 = new Point(-tri.p1.x, tri.p1.y, tri.p1.z);
var p2 = new Point(-tri.p2.x, tri.p2.y, tri.p2.z);
var p3 = new Point(-tri.p3.x, tri.p3.y, tri.p3.z);
var n1 = new Point(-tri.normal1.x, tri.normal1.y, tri.normal1.z);
var n2 = new Point(-tri.normal2.x, tri.normal2.y, tri.normal2.z);
var n3 = new Point(-tri.normal3.x, tri.normal3.y, tri.normal3.z);
var uv1 = new UV(tri.uv1.x, tri.uv1.y);
var uv2 = new UV(tri.uv2.x, tri.uv2.y);
var uv3 = new UV(tri.uv3.x, tri.uv3.y);
points.push(p3);
points.push(p2);
points.push(p1);
normals.push(n3);
normals.push(n2);
normals.push(n1);
uvs.push(uv3);
uvs.push(uv2);
uvs.push(uv1);
}
var prim = new Polygon(points);
prim.uvs = uvs;
prim.normals = normals;
var texture:Texture = loader.load(tex(grp)).toImage().toTexture();
texture.wrap = Wrap.Repeat;
var material = h3d.mat.Material.create(texture);
// material.mainPass.wireframe = true;
var mesh = new Mesh(prim, material, ig);
}
return ig;
}
public static function loadDifAsPI(path:String, loader:Loader) {
var dif = Dif.Load(path);
var geo = dif.interiors[0];
var hulls = geo.convexHulls;
var triangles = [];
var textures = [];
var collider = new CollisionEntity();
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];
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 = [];
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]];
} else {
p1 = points[geo.windings[k - 2]];
p2 = points[geo.windings[k - 1]];
p3 = points[geo.windings[k]];
var materialName = stripTexName(texture);
var hasMaterialInfo = materialDict.exists(materialName);
if (hasMaterialInfo) {
var minfo = materialDict.get(materialName);
colliderSurface.friction = minfo.friction;
colliderSurface.restitution = minfo.restitution;
}
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);
colliderSurface.points.push(new Vector(-p1.x, p1.y, p1.z));
colliderSurface.points.push(new Vector(-p2.x, p2.y, p2.z));
colliderSurface.points.push(new Vector(-p3.x, p3.y, p3.z));
@ -373,8 +209,7 @@ class DifBuilder {
}
collider.generateBoundingBox();
var ig = new PathedInterior();
ig.collider = collider;
itr.collider = collider;
function canFindTex(tex:String) {
if (tex.indexOf('/') != -1) {
@ -458,9 +293,7 @@ class DifBuilder {
var material = h3d.mat.Material.create(texture);
// material.mainPass.wireframe = true;
var mesh = new Mesh(prim, material, ig);
var mesh = new Mesh(prim, material, itr);
}
return ig;
}
}

View file

@ -1,5 +1,6 @@
package;
import src.InteriorGeometry;
import h3d.Quat;
import src.PathedInteriorMarker;
import src.PathedInterior;
@ -31,10 +32,12 @@ class Main extends hxd.App {
world = new MarbleWorld(s3d);
var db = DifBuilder.loadDif("interiors/beginner/beginner_finish.dif", loader);
var db = new InteriorGeometry();
DifBuilder.loadDif("interiors/beginner/training_friction.dif", loader, db);
world.addInterior(db);
var pi = DifBuilder.loadDifAsPI("interiors/addon/smallplatform.dif", loader);
var pi = new PathedInterior();
DifBuilder.loadDif("interiors/addon/smallplatform.dif", loader, pi);
var pim = pi.getTransform();
pim.setPosition(new Vector(5, 0, 0));
pi.setTransform(pim);

View file

@ -106,9 +106,9 @@ class CollisionEntity implements IOctreeObject {
cinfo.velocity = this.velocity;
cinfo.contactDistance = closest.distance(position);
// cinfo.penetration = radius - (position.sub(closest).dot(normal));
cinfo.restitution = 1;
cinfo.force = 0;
cinfo.friction = 1;
cinfo.restitution = surface.restitution;
cinfo.force = surface.force;
cinfo.friction = surface.friction;
contacts.push(cinfo);
}
}

View file

@ -14,6 +14,10 @@ class CollisionSurface implements IOctreeObject {
public var normals:Array<Vector>;
public var indices:Array<Int>;
public var friction:Float = 1;
public var restitution:Float = 1;
public var force:Float = 0;
public function new() {}
public function getElementType() {