Skip to content

HAXE port of a simple, open source, 2D impulse based physics engine for non-view (server-side) use.

Notifications You must be signed in to change notification settings

manjav/hx-impulse-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MIT License Haxelib Version

Impulse Engine

Test Project

Impulse Engine is a small 2D physics engine that originaly written in C++ by Randy Gaul. thanks to Philip Diffenderfer for java port. Impulse Engine is intended to be used in an educational manner by other looking to learn the inner workings of physics engines, but you can use in server-side and pure-logic projects.

TestSimple.hx class is demo of haxe-as3 language. based on your needs, build to cpp, java, js, cs, php and other haxe support languages.

Step 1 : Initialize impulse scene

this.impulse = new ImpulseScene(ImpulseMath.DT, 10);

Step 2 : Define static items

// center circle
var b:Body = null;
b = this.impulse.add(new Circle(30.0), 300, 500);
b.setStatic();

// bottom rectangle
var p = new Polygon();
p.setBox(250.0, 10.0);
b = this.impulse.add(p, 300, 600);
b.setStatic();
b.setOrient(0);

Step 3 : Instantiate circle, rectangle and polygon by click

this.stage.addEventListener(MouseEvent.CLICK, this.stage_clickHandler);

private function stage_clickHandler(event:MouseEvent):Void {
  var mx = Math.round(event.stageX);
  var my = Math.round(event.stageY);
  var min = 10;
  var max = 30;
  var b:Body;
  if (event.shiftKey) {
    var p = new Polygon();
    p.setBox(ImpulseMath.random(min, max), ImpulseMath.random(min, max));
    b = this.impulse.add(p, mx, my);
    b.setOrient(0.0);
  } else if (event.ctrlKey) {
    var r = ImpulseMath.random(min, max);
    var vertCount = ImpulseMath.randomR(3, Polygon.MAX_POLY_VERTEX_COUNT);
    var verts = Vec2.arrayOf(vertCount);
    for (i in 0...vertCount)
      verts[i].set(ImpulseMath.random(-r, r), ImpulseMath.random(-r, r));
    b = this.impulse.add(new Polygon(verts), mx, my);
    b.setOrient(ImpulseMath.random(-ImpulseMath.PI, ImpulseMath.PI));
    b.restitution = 0.2;
    b.dynamicFriction = 0.2;
    b.staticFriction = 0.4;
  } else {
    b = this.impulse.add(new Circle(ImpulseMath.random(min, max)), mx, my);
  }
}

Step 4 : Update every frames

this.addEventListener(Event.ENTER_FRAME, this.this_enterFrameHandler);
private function this_enterFrameHandler(event:flash.events.Event):Void {
  var t = Timer.stamp() * 1000;
  this.accumulator += (t - this.dt);
  this.dt = t;
  if (this.accumulator >= this.impulse.dt) {
    this.impulse.step();
    this.accumulator -= this.impulse.dt;
  }
}

About

HAXE port of a simple, open source, 2D impulse based physics engine for non-view (server-side) use.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages