Skip to content

Commit

Permalink
Merge pull request #13 from mjneil/dev-math-2d
Browse files Browse the repository at this point in the history
Dev math 2d
  • Loading branch information
Squeakrats committed Feb 29, 2016
2 parents 836da3c + 12af534 commit ae475fd
Show file tree
Hide file tree
Showing 10 changed files with 817 additions and 128 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
24 changes: 11 additions & 13 deletions cruft/graphics/Camera2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Actor from "engine/core/Actor";
import Renderer from "engine/graphics/Renderer"
import SpriteRenderer from "engine/graphics/plugins/SpriteRenderer";
import Transform2D from "engine/components/Transform2D";
import {vec2} from "engine/lib/gl-matrix";
import {vec2} from "../math/math";
import engine, {scheduler} from "engine/engine";
import Script from "engine/processes/Script";

Expand All @@ -15,7 +15,7 @@ export default class Camera2D extends Actor {//todo set/get widht//height
this._renderer.registerPlugin(new SpriteRenderer());
document.body.appendChild(this._renderer.canvas);//not sure how want to manage access to the dom right now. so tmp here.
this.addComponent(new Transform2D()); //think about using a height variable or somthing? :/ Hmm idk.
this.getComponent("transform").scale = [width/2, height/2] //might want to make variables to like set zoom and stuff idk.
this.getComponent("transform").scale = new vec2(width/2, height/2) //might want to make variables to like set zoom and stuff idk.

//todo track that script so we can delete it.
//also its not in update because I want it to be the last thing that happens
Expand All @@ -40,12 +40,11 @@ export default class Camera2D extends Actor {//todo set/get widht//height
var position = transform.position;
var scale = transform.scale;

var tmp = vec2.create();
tmp[0] = (mouse[0] - window.innerWidth/2);
tmp[1] = (window.innerHeight - mouse[1] - window.innerHeight/2);
var tmp = vec2.zero();
tmp.x = (mouse.x - window.innerWidth/2);
tmp.y = (window.innerHeight - mouse.y - window.innerHeight/2);

tmp[0] += position[0];
tmp[1] += position[1];
tmp.add(position);

return tmp;
}
Expand All @@ -56,14 +55,13 @@ export default class Camera2D extends Actor {//todo set/get widht//height
var transform = this.getComponent("transform");
var world = target.getComponent("transform").getWorldPosition();
var selfWorld = transform.getWorldPosition();
var dif = vec2.create();
vec2.sub(dif, world, selfWorld);
var len = vec2.len(dif);
var scale = (len < 900)? .002:.05;
vec2.scale(dif, dif, .07);
var dif = vec2.subtract(world, selfWorld);
var len = dif.length();
var scale = (len < 900) ? 0.002 : 0.05;
dif.scale(0.07);

var pos = transform.position;
vec2.add(pos, pos, dif);
pos.add(dif)
transform.position = pos;


Expand Down
Loading

0 comments on commit ae475fd

Please sign in to comment.