Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinEast committed Jan 4, 2019
0 parents commit 3e0ce4d
Show file tree
Hide file tree
Showing 24 changed files with 1,142 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
.tmp
sample/bin/
27 changes: 27 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Chrome Debugger",
"url": "file://${workspaceFolder}/index.html",
"webRoot": "${workspaceFolder}",
"preLaunchTask": {
"type" : "haxe",
"args" : "active configuration"
}
},
{
"request": "launch",
"type": "hl",
"name": "HashLink Debugger (WIP)",
"hxml": "compile.hxml",
"cwd": "${workspaceRoot}",
"preLaunchTask": {
"type" : "haxe",
"args" : "active configuration"
}
}
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"[haxe]": {
"editor.formatOnSave": true
}
}
29 changes: 29 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "haxe",
"args": "active configuration",
"problemMatcher": [
"$haxe-absolute",
"$haxe",
"$haxe-error",
"$haxe-trace"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "hxml",
"file": "js.hxml",
"problemMatcher": [
"$haxe-absolute",
"$haxe",
"$haxe-error",
"$haxe-trace"
]
}
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Austin East

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
65 changes: 65 additions & 0 deletions echo/Body.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package echo;

import hxmath.math.Vector2;
import echo.shape.Shape;

class Body {
public var shape:Shape;
public var collides:Bool;
public var moves:Bool;
public var solid:Bool;
@:isVar
public var x(get, set):Float;
@:isVar
public var y(get, set):Float;
@:isVar
public var elasticity(get, set):Float;
public var velocity(get, null):Vector2;
public var drag(get, null):Vector2;
public var dirty:Bool;

public function new(?options:BodyOptions) {
load(options);
}

public function load(?options:BodyOptions) {
if (options == null) options = {};
if (options.shape != null) shape = Shape.get(options.shape);
}

public function dispose() {
shape.put();
}

// getters
function get_x():Float return x;

function get_y():Float return y;

function get_elasticity():Float return elasticity;

function get_velocity():Vector2 return velocity;

function get_drag():Vector2 return drag;

// setters
function set_x(value:Float) return x = value;

function set_y(value:Float) return y = value;

function set_elasticity(value:Float) return elasticity = value;
}

typedef BodyOptions = {
var ?shape:ShapeOptions;
var ?moves:Bool;
var ?collides:Bool;
var ?solid:Bool;
var ?x:Float;
var ?y:Float;
var ?elasticity:Float;
var ?velocity_x:Float;
var ?velocity_y:Float;
var ?drag_x:Float;
var ?drag_y:Float;
}
168 changes: 168 additions & 0 deletions echo/Collisions.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package echo;

import hxmath.math.Vector2;
import echo.shape.*;

using hxmath.math.MathUtil;

class Collisions {
public static inline function point_in_rect(p:Vector2, r:Rect):Bool {
return r.left <= p.x &&
r.right >= p.x &&
r.top <= p.x &&
r.bottom >= p.y;
}

public static inline function point_in_circle(p:Vector2, c:Circle):Bool {
return p.distanceTo(c.position) < c.radius;
}

public static inline function rect_contains(r:Rect, p:Vector2):Bool {
return point_in_rect(p, r);
}

public static inline function circle_contains(c:Circle, p:Vector2):Bool {
return point_in_circle(p, c);
}

public static inline function line_interects_rect(l:Line, r:Rect):Null<IntersectionData> {
return null;
}

public static inline function line_intersects_circle(l:Line, c:Circle):Null<IntersectionData> {
return null;
}

public static inline function rect_intersects(r:Rect, l:Line):Null<IntersectionData> {
return line_interects_rect(l, r);
}

public static inline function circle_intersects(c:Circle, l:Line):Null<IntersectionData> {
return line_intersects_circle(l, c);
}

public static function rect_and_rect(rect1:Rect, rect2:Rect, flip:Bool = false):Null<CollisionData> {
var s1 = flip ? rect2 : rect1;
var s2 = flip ? rect1 : rect2;

// Vector from A to B
var n = s2.position - s1.position;
// Calculate overlap on x axis
var x_overlap = s1.ex + s2.ex - Math.abs(n.x);
// SAT test on x axis
if (x_overlap > 0) {
// Calculate overlap on y axis
var y_overlap = s1.ey + s2.ey - Math.abs(n.y);
// SAT test on y axis.
// If both axis overlap, the boxes are colliding
if (y_overlap > 0) {
// Find out which axis is axis of least penetration
if (x_overlap < y_overlap) {
// Point towards B knowing that n points from A to B
return {
normal: n.x < 0 ? new Vector2(-1, 0) : new Vector2(1, 0),
overlap: x_overlap
};
}
else {
// Point toward B knowing that n points from A to B
return {
normal: n.y < 0 ? new Vector2(0, -1) : new Vector2(0, 1),
overlap: y_overlap
}
}
}
}

return null;
}

public static function circle_and_circle(circle1:Circle, circle2:Circle, flip:Bool = false):Null<CollisionData> {
var s1 = flip ? circle2 : circle1;
var s2 = flip ? circle1 : circle2;

// Vector2 from s2 to s1
var n = s2.position - s1.position;
// radii of circles
var r = s1.radius + s2.radius;
var d = n.lengthSq;

// Do quick check if circles are colliding
if (d >= r * r) return null;
// If distance between circles is zero, make up a number
else if (d == 0) {
return {
overlap: s1.radius,
normal: new Vector2(1, 0)
};
}
else {
// Get actual square root
d = Math.sqrt(d);
// Distance is difference between radius and distance
return {
overlap: r - d,
normal: n / d
};
}
}

public static function rect_and_circle(r:Rect, c:Circle, flip:Bool = false):Null<CollisionData> {
// Vector from A to B
var n = flip ? c.position - r.position : r.position - c.position;
// Closest point on A to center of B
var closest = n.clone();

// Clamp point to edges of the AABB
closest.x = closest.x.clamp(-r.ex, r.ex);
closest.y = closest.y.clamp(-r.ey, r.ey);
var inside = false;

// Circle is inside the AABB, so we need to clamp the circle's center
// to the closest edge
if (n == closest) {
inside = true;
// Find closest axis
if (Math.abs(n.x) < Math.abs(n.y)) {
// Clamp to closest extent
closest.x = closest.x > 0 ? r.ex : -r.ex;
}
else {
// Clamp to closest extent
closest.y = closest.y > 0 ? r.ey : -r.ey;
}
}

var normal = n - closest;
var d = normal.lengthSq;
var rad = c.radius;

// Early out of the radius is shorter than distance to closest point and
// Circle not inside the AABB
if (d > rad * rad && !inside) return null;

// Avoided sqrt until we needed
d = Math.sqrt(d);
// n.normalize();

// Collision normal needs to be flipped to point outside if circle was
// inside the AABB
return {
normal: n.normalize(),
overlap: rad - d
};
}
}

typedef CollisionData = {
/**
* The length of shape1's penetration into shape2.
*/
var overlap:Float;
/**
* The normal vector (direction) of shape1's penetration into shape2.
*/
var normal:Vector2;
}

typedef IntersectionData = {}
62 changes: 62 additions & 0 deletions echo/Echo.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package echo;

import haxe.ds.Vector;

class Echo {
// var states:History;
// var observers:
public static function start(?options:StateOptions):State {
return new State(options);
}

public static function step(state:State, dt:Float) {
// Broadphase

// Narrowphase

// Integrate

// Iterate NarrowPhase && Integrate

// Notify New State and Collisions to Listeners
}

public static function undo(state:State):State {
return state;
}

public static function redo(state:State):State {
return state;
}

// returns observable
public static function listen() {}
}

typedef StateOptions = {
var width:Float;
var height:Float;
var ?bodies:Array<Body>;
var ?iterations:Int;
var ?history:Int;
}

class State {
var width:Float;
var height:Float;
var bodies:Array<Body>;
var iterations:Int;
var history:Vector<Array<Body>>;

public function new(?options:StateOptions) {}

public function add(body:Body):Body {
bodies.remove(body);
bodies.push(body);
return body;
}

public function remove(body:Body):Body {
return body;
}
}
7 changes: 7 additions & 0 deletions echo/Physics.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package echo;

class Physics {
public static function step() {
// Integrate
}
}
Loading

0 comments on commit 3e0ce4d

Please sign in to comment.