Skip to content

Instantly share code, notes, and snippets.

@AlexFlasch
Created March 19, 2016 00:06
Show Gist options
  • Save AlexFlasch/ba18978c32dabde4ea5a to your computer and use it in GitHub Desktop.
Save AlexFlasch/ba18978c32dabde4ea5a to your computer and use it in GitHub Desktop.
Trying to get a triangle to rotate around the box with mouse movement :)
import luxe.Input;
import luxe.Sprite;
import luxe.Color;
import luxe.Vector;
import luxe.Draw;
import luxe.Transform;
import luxe.Quaternion;
import phoenix.geometry.Geometry;
class Main extends luxe.Game {
var box : Sprite;
var arrow : Geometry;
var mouseVector : Vector;
var arrowTransform: Transform;
var arrowRotation : Quaternion;
override function config(config : luxe.AppConfig) {
return config;
} // config
override function ready() {
box = new Sprite({
name: 'player',
pos: Luxe.screen.mid,
color: new Color().rgb(0x22A7F0),
size: new Vector(64, 64)
});
arrow = Luxe.draw.ngon({
sides: 3,
r: 16,
solid: true,
color: new Color().rgb(0x22A7F0),
x: Luxe.screen.mid.x,
y: Luxe.screen.mid.y - 64
});
mouseVector = new Vector(0, 0);
arrowTransform = new Transform();
arrowRotation = new Quaternion(0, 0);
} // ready
override function onkeyup(e : KeyEvent) {
if(e.keycode == Key.escape) {
Luxe.shutdown();
}
} // onkeyup
override function update(dt : Float) {
} // update
override function onmousemove(event : MouseEvent) {
mouseVector.set(event.x, event.y, 0, 0);
arrowRotation.set(getAngle(mouseVector, box.pos), 0, 0 , 0);
arrowTransform.rotation = arrowRotation;
arrow.transform = arrowTransform;
} // onmousemove
function getAngle(vec1 : Vector, vec2 : Vector) {
var distX : Float = Math.abs(vec2.x - vec1.x);
var distY : Float = Math.abs(vec2.y - vec1.y);
var dist : Float = Math.sqrt(Math.pow(distY, 2) + Math.sqrt(Math.pow(distX, 2)));
var angle : Float = Math.asin(distY/dist);
return angle;
} // getAngle
} //Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment