Skip to content

Commit

Permalink
factor out glmatrix from transform2d, add math.js for exports
Browse files Browse the repository at this point in the history
  • Loading branch information
mjneil committed Feb 28, 2016
1 parent a0471dd commit 51f47c3
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 46 deletions.
91 changes: 45 additions & 46 deletions cruft/components/Transform2D.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import Component from "engine/core/Component"
import {mat3, vec2} from "engine/lib/gl-matrix";
import Component from "../core/Component"
import {vec2, mat3} from "../math/math";

var IDENTITY_MATRIX = mat3.create();
mat3.identity(IDENTITY_MATRIX)//DO NOT CHANGE THIS.
var IDENTITY_MATRIX = new mat3();
//mat3.identity(IDENTITY_MATRIX)//DO NOT CHANGE THIS.

export default class Transform2D extends Component {
constructor() {
super("transform");

this._position = vec2.create();
this._scale = vec2.create();
this._scale[0] = 1;
this._scale[1] = 1;
this._position = vec2.zero();
this._scale = new vec2(1,1);
this._rotation = 0;

this.toWorld = mat3.create();
this.matrix = mat3.create();
this.inverse = mat3.create();
mat3.identity(this.toWorld);
mat3.identity(this.matrix);
mat3.identity(this.inverse);
this.toWorld = new mat3();
this.matrix = new mat3();
this.inverse = new mat3();
}

initialize() {
Expand All @@ -29,12 +24,12 @@ export default class Transform2D extends Component {
}

get position () {
return vec2.clone(this._position);
return this._position.clone();
}

//right now this is kinda expensive. So like dont do it unless you need to :/
set position (position) {
vec2.copy(this._position, position);
this._position.copy(position);
this.updateMatrix();
}

Expand All @@ -48,33 +43,32 @@ export default class Transform2D extends Component {
}

getWorldPosition() {
var pos = vec2.create();
pos[0] = this.toWorld[6];
pos[1] = this.toWorld[7];
return pos;
return new vec2(this.toWorld[6], this.toWorld[7]);
}

setDirection(vec) {
var tmp = vec2.clone(vec);
var len = vec2.length(tmp);
if(len == 0){
var tmp = vec.clone();
var len = tmp.length;
if (len == 0) {
this.rotation = 0;
}else{
vec2.scale(tmp, tmp, 1/len);
var theta = Math.acos(tmp[0]);
if(vec[1] < 0) theta *= -1;
} else {
tmp.scale(1/len);
var theta = Math.acos(tmp.x);
if (vec.y < 0) {
theta *= -1;
}
this.rotation = theta;
}


}

get scale() {
return vec2.clone(this._scale);
return this._scale.clone();
}

set scale(scale) {
vec2.copy(this._scale, scale);
this._scale.copy(scale);
this.updateMatrix();
}

Expand All @@ -88,48 +82,53 @@ export default class Transform2D extends Component {
var _rotation = this._rotation;

//optimize this later
mat3.scale(matrix, IDENTITY_MATRIX, this._scale);
mat3.rotate(matrix, matrix, this._rotation);
matrix[6] = _position[0];
matrix[7] = _position[1];
matrix.indentity()
.scale(this._scale)
.rotate(this._rotation);

mat3.invert(this.inverse, matrix);
matrix.data[6] = this._position.x;
matrix.data[7] = this._position.y;

this.inverse.copy(matrix).invert();
//TODO z-index? What are we doing with that exactly.

if(parent) {
mat4.mul(toWorld, parent.getComponent("transform").toWorld, matrix);
}else{
mat3.copy(toWorld, matrix);
if (parent) {
toWorld.copy(mat3.multiply(parent.getComponent("transform").toWorld, matrix));
} else {
toWorld.copy(matrix);
}

actor.emit("transform:change");

var children = this.actor.children;
for(let key in children){
for (let key in children) {
children[key].updateMatrix();
}
}

setFromJSON(json) { //lazy. update this to not call updateMatrix 3 times.
if(!json) return;
if (!json) {
return;
}

var needsUpdate = false;

if(json.position){
vec2.copy(this._position, json.position);
if (json.position) {
this._position.copy(json.position);
needsUpdate = true;
}

if(json.rotation !== undefined){
if (json.rotation !== undefined) {
this._rotation= json.rotation;
needsUpdate = true;
}

if(json.scale){
vec2.copy(this._scale, json.scale);
if (json.scale) {
this._scale.copy(json.scale);
needsUpdate = true;
}

if(needsUpdate) {
if (needsUpdate) {
this.updateMatrix();
}
}
Expand Down
25 changes: 25 additions & 0 deletions cruft/math/mat3.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ export default class mat3 {
return det;
}

static multiply(A, B) {
let out = new mat3(), d = A.data, b = B.data,
d00 = d[0], d01 = d[1], d02 = d[2],
d10 = d[3], d11 = d[4], d12 = d[5],
d20 = d[6], d21 = d[7], d22 = d[8],

b00 = b[0], b01 = b[1], b02 = b[2],
b10 = b[3], b11 = b[4], b12 = b[5],
b20 = b[6], b21 = b[7], b22 = b[8];

out.data[0] = d00 * b00 + d10 * b01 + d20 * b02;
out.data[1] = d01 * b00 + d11 * b01 + d21 * b02;
out.data[2] = d02 * b00 + d12 * b01 + d22 * b02;

out.data[3] = d00 * b10 + d10 * b11 + d20 * b12;
out.data[4] = d01 * b10 + d11 * b11 + d21 * b12;
out.data[5] = d02 * b10 + d12 * b11 + d22 * b12;

out.data[6] = d00 * b20 + d10 * b21 + d20 * b22;
out.data[7] = d01 * b20 + d11 * b21 + d21 * b22;
out.data[8] = d02 * b20 + d12 * b21 + d22 * b22;

return out;
}

multiply(B) {
let d = this.data, b = B.data,
d00 = d[0], d01 = d[1], d02 = d[2],
Expand Down
4 changes: 4 additions & 0 deletions cruft/math/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import vec2 from "./vec2";
import mat3 from "./mat3";

export {vec2, mat3};
6 changes: 6 additions & 0 deletions cruft/math/vec2.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export default class vec2 {
return new vec2(this.x, this.y);
}

copy(b) {
this.x = b.x;
this.y = b.y;
return this;
}

static cross(a, b) {
return a.x * b.y - a.y * b.x;
}
Expand Down
6 changes: 6 additions & 0 deletions docs/cruft/math/mat3.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Creates a copy of matrix a.
let c = mat3.clone(a);
```

### static multiply( [A](mat3.md), [B](mat3.md) )
Calculates A * B.

```javascript
let c = mat3.multiply(A, B);

## Methods

### add( [B](mat3.md) )
Expand Down
7 changes: 7 additions & 0 deletions docs/cruft/math/vec2.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ Returns clone of this.
let c = a.clone();
```

### copy( [b](vec2.md) )
Copies vec2 b into this.

```javascript
a.copy(b);
```

### cross( [b](vec2.md) )
Calculates this cross b, returning this.

Expand Down

0 comments on commit 51f47c3

Please sign in to comment.