Giter Club home page Giter Club logo

hx-impulse-engine's Introduction

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;
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.