Skip to content

Commit

Permalink
Merge pull request #72 from moisesjpelaez/master
Browse files Browse the repository at this point in the history
Add sleeping thresholds to RigidBody and RigidBodyConfig
  • Loading branch information
saharan authored May 16, 2024
2 parents 9ded5ee + 1788ec8 commit b3924ce
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 16 deletions.
3 changes: 1 addition & 2 deletions demos/src/demo/js/minilib/ODrawMode.hx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package demo.js.minilib;
import js.html.webgl.*;

@:enum
abstract ODrawMode(Int) to Int {
enum abstract ODrawMode(Int) to Int {
var Points = GL.POINTS;
var Lines = GL.LINES;
var LineStrip = GL.LINE_STRIP;
Expand Down
3 changes: 1 addition & 2 deletions demos/src/demo/js/minilib/ODrawUsage.hx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package demo.js.minilib;
import js.html.webgl.*;

@:enum
abstract ODrawUsage(Int) to Int {
enum abstract ODrawUsage(Int) to Int {
var StaticDraw = GL.STATIC_DRAW;
var DynamicDraw = GL.DYNAMIC_DRAW;
}
1 change: 1 addition & 0 deletions demos/src/demo/js/minilib/OIndexBuffer.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package demo.js.minilib;
import js.html.*;
import js.html.webgl.*;
import js.lib.Int16Array;

class OIndexBuffer {
var gl:GL;
Expand Down
1 change: 1 addition & 0 deletions demos/src/demo/js/minilib/OVertexBuffer.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package demo.js.minilib;
import js.html.*;
import js.html.webgl.*;
import js.lib.Float32Array;

class OVertexBuffer {
public var numVertices(default, null):Int;
Expand Down
2 changes: 1 addition & 1 deletion src/oimo/common/Setting.hx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Setting {
public static var islandInitialRigidBodyArraySize:Int = 128;
public static var islandInitialConstraintArraySize:Int = 128;

// sleeping
// sleeping, some are just default values and can be changed through RigidBodyConfig
public static var sleepingVelocityThreshold:Float = 0.2;
public static var sleepingAngularVelocityThreshold:Float = 0.5;
public static var sleepingTimeThreshold:Float = 1.0;
Expand Down
4 changes: 2 additions & 2 deletions src/oimo/dynamics/Island.hx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Island {
// update sleep time
if (rb._isSleepy()) {
rb._sleepTime += dt;
if (rb._sleepTime > Setting.sleepingTimeThreshold) {
if (rb._sleepTime >= rb._sleepingTimeThreshold) {
rb.sleep();
}
} else {
Expand Down Expand Up @@ -172,7 +172,7 @@ class Island {
}

// check if the rigid body is awaken
if (rb._sleepTime < Setting.sleepingTimeThreshold) {
if (rb._sleepTime < rb._sleepingTimeThreshold) {
// awaken the whole island
sleepIsland = false;
}
Expand Down
10 changes: 8 additions & 2 deletions src/oimo/dynamics/rigidbody/RigidBody.hx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class RigidBody {
public var _sleepTime:Float;
public var _sleeping:Bool;
public var _autoSleep:Bool;
public var _sleepingVelocityThreshold:Float;
public var _sleepingAngularVelocityThreshold:Float;
public var _sleepingTimeThreshold:Float;

public var _mass:Float;
public var _invMass:Float;
Expand Down Expand Up @@ -112,6 +115,9 @@ class RigidBody {
_sleepTime = 0;
_sleeping = false;
_autoSleep = config.autoSleep;
_sleepingVelocityThreshold = config.sleepingVelocityThreshold;
_sleepingAngularVelocityThreshold = config.sleepingAngularVelocityThreshold;
_sleepingTimeThreshold = config.sleepingTimeThreshold;

_mass = 0;
_invMass = 0;
Expand Down Expand Up @@ -209,8 +215,8 @@ class RigidBody {

extern public inline function _isSleepy():Bool {
return _autoSleep
&& M.vec3_dot(_vel, _vel) < Setting.sleepingVelocityThreshold * Setting.sleepingVelocityThreshold
&& M.vec3_dot(_angVel, _angVel) < Setting.sleepingAngularVelocityThreshold * Setting.sleepingAngularVelocityThreshold;
&& M.vec3_dot(_vel, _vel) < _sleepingVelocityThreshold * _sleepingVelocityThreshold
&& M.vec3_dot(_angVel, _angVel) < _sleepingAngularVelocityThreshold * _sleepingAngularVelocityThreshold;
}

extern public inline function _isAlone():Bool {
Expand Down
35 changes: 28 additions & 7 deletions src/oimo/dynamics/rigidbody/RigidBodyConfig.hx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package oimo.dynamics.rigidbody;

import oimo.common.Setting;
import oimo.common.Mat3;
import oimo.common.Vec3;

Expand Down Expand Up @@ -33,12 +35,6 @@ class RigidBodyConfig {
*/
public var type:Int;

/**
* Whether to automatically sleep the rigid body when it stops moving
* for a certain period of time, namely `Setting.sleepingTimeThreshold`.
*/
public var autoSleep:Bool;

/**
* The damping coefficient of the linear velocity. Set positive values to
* gradually reduce the linear velocity.
Expand All @@ -51,6 +47,27 @@ class RigidBodyConfig {
*/
public var angularDamping:Float;

/**
* Whether to automatically sleep the rigid body when it stops moving
* for a certain period of time, namely `sleepingTimeThreshold`.
*/
public var autoSleep:Bool;

/**
* The linear velocity threshold to sleep the rigid body.
*/
public var sleepingVelocityThreshold:Float;

/**
* The angular velocity threshold to sleep the rigid body.
*/
public var sleepingAngularVelocityThreshold:Float;

/**
* The time threshold to sleep the rigid body.
*/
public var sleepingTimeThreshold:Float;

/**
* Default constructor.
*/
Expand All @@ -60,8 +77,12 @@ class RigidBodyConfig {
linearVelocity = new Vec3();
angularVelocity = new Vec3();
type = RigidBodyType._DYNAMIC;
autoSleep = true;
linearDamping = 0;
angularDamping = 0;
autoSleep = true;
// inherit default value in Setting
sleepingVelocityThreshold = Setting.sleepingVelocityThreshold;
sleepingAngularVelocityThreshold = Setting.sleepingAngularVelocityThreshold;
sleepingTimeThreshold = Setting.sleepingTimeThreshold;
}
}
1 change: 1 addition & 0 deletions src/oimo/m/B.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using Lambda;
/**
* Build Macro
*/
@:haxe.warning("-WDeprecated") // for using @:extern
@:extern
class B {

Expand Down

0 comments on commit b3924ce

Please sign in to comment.