more optimizations for mp

This commit is contained in:
RandomityGuy 2024-07-17 16:27:24 +05:30
parent 4cf4748813
commit 97e8f5a753
5 changed files with 21 additions and 19 deletions

View file

@ -22,11 +22,11 @@ typedef ForceData = {
class ForceObject extends DtsObject {
var forceDatas:Array<ForceData>;
public function getForce(pos:Vector) {
public function getForce(pos:Vector, outForce:Vector) {
if (pos.distanceSq(this.getAbsPos().getPosition()) > 50 * 50)
return;
var strength = 0.0;
var dot = 0.0;
var posVec = new Vector();
var retForce = new Vector();
for (forceData in forceDatas) {
if (forceData.forceType == NoForce) {
continue;
@ -40,7 +40,7 @@ class ForceObject extends DtsObject {
nodeVec = forceData.forceVector;
}
posVec = pos.sub(node.getPosition());
var posVec = pos.sub(node.getPosition());
dot = posVec.length();
if (forceData.forceRadius < dot) {
@ -52,23 +52,21 @@ class ForceObject extends DtsObject {
if (forceType == ForceSpherical) {
dot = strength / dot;
retForce = retForce.add(posVec.multiply(dot));
outForce.load(outForce.add(posVec.multiply(dot)));
}
if (forceType == ForceField) {
retForce = retForce.add(nodeVec.multiply(strength));
outForce.load(outForce.add(nodeVec.multiply(strength)));
}
if (forceType == ForceCone) {
posVec = posVec.multiply(1 / dot);
posVec.load(posVec.multiply(1 / dot));
var newDot = nodeVec.dot(posVec);
var arc = forceData.forceArc;
if (arc < newDot) {
retForce = retForce.add(posVec.multiply(strength).multiply(newDot - arc).multiply(1 / (1 - arc)));
outForce.load(outForce.add(posVec.multiply(strength).multiply(newDot - arc).multiply(1 / (1 - arc))));
}
}
}
return retForce;
}
}

View file

@ -705,12 +705,13 @@ class Marble extends GameObject {
if (helicopter) {
A.load(A.multiply(0.25));
}
if (this.level != null) {
if (this.level != null && level.forceObjects.length > 0) {
var mass = this.getMass();
var externalForce = new Vector();
for (obj in level.forceObjects) {
var force = cast(obj, ForceObject).getForce(this.collider.transform.getPosition());
A.load(A.add(force.multiply(1 / mass)));
cast(obj, ForceObject).getForce(this.collider.transform.getPosition(), externalForce);
}
A.load(A.add(externalForce.multiply(1 / mass)));
}
if (contacts.length != 0 && this.mode != Start) {

View file

@ -28,7 +28,7 @@ class CollisionWorld {
public function new() {
this.octree = new Octree();
this.dynamicOctree = new Octree();
this.dynamicOctree = new Octree(true);
this.staticWorld = new CollisionEntity(null);
}

View file

@ -16,8 +16,8 @@ class Octree {
var prevBoundSearch:Bounds;
var boundSearchCache:Array<IOctreeElement>;
public function new() {
this.root = new OctreeNode(this, 0);
public function new(disableMerge:Bool = false) {
this.root = new OctreeNode(this, 0, disableMerge);
this.objectToNode = new Map();
}

View file

@ -32,7 +32,9 @@ class OctreeNode implements IOctreeElement {
public var depth:Int;
public function new(octree:Octree, depth:Int) {
var disableMerge:Bool;
public function new(octree:Octree, depth:Int, disableMerge:Bool = false) {
this.octree = octree;
this.depth = depth;
this.xMin = 0;
@ -41,6 +43,7 @@ class OctreeNode implements IOctreeElement {
this.xMax = 1;
this.yMax = 1;
this.zMax = 1;
this.disableMerge = disableMerge;
}
public function insert(object:IOctreeObject) {
@ -90,7 +93,7 @@ class OctreeNode implements IOctreeElement {
public function createOctants() {
this.octants = [];
for (i in 0...8) {
var newNode = new OctreeNode(this.octree, this.depth + 1);
var newNode = new OctreeNode(this.octree, this.depth + 1, disableMerge);
newNode.parent = this;
var newSize = new Vector(xMax - xMin, yMax - yMin, zMax - zMin);
newNode.xMin = this.xMin + newSize.x * ((i & 1) >> 0);
@ -137,7 +140,7 @@ class OctreeNode implements IOctreeElement {
}
public function merge() {
if (this.count > 8 || (this.octants == null))
if (this.count > 8 || (this.octants == null) || disableMerge)
return;
// Add all objects in the octants back to this node
for (i in 0...8) {