mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
Throwing with Swiper, working but needs to be refined. No angulat verlocity yet.
This commit is contained in:
parent
6d9d02615a
commit
cf2182676b
5 changed files with 198 additions and 85 deletions
109
app/Game/Client/Control/Swiper.js
Normal file
109
app/Game/Client/Control/Swiper.js
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
define([
|
||||
],
|
||||
|
||||
function () {
|
||||
|
||||
var MAX_LENGTH = 300;
|
||||
|
||||
function Swiper() {
|
||||
this.points = [];
|
||||
this.angleSum = 0;
|
||||
this.lengthSum = 0;
|
||||
}
|
||||
|
||||
Swiper.prototype.swipe = function(x, y) {
|
||||
console.log("swipe")
|
||||
|
||||
if(this.lengthSum > MAX_LENGTH) return;
|
||||
|
||||
this.points.push({x:x, y:y});
|
||||
var points = this.points;
|
||||
|
||||
if(points.length >= 3) {
|
||||
|
||||
var i = points.length - 1;
|
||||
|
||||
var vectors = [
|
||||
{
|
||||
x: points[i].x - points[i-1].x,
|
||||
y: points[i].y - points[i-1].y
|
||||
},
|
||||
{
|
||||
x: points[i-2].x - points[i-1].x,
|
||||
y: points[i-2].y - points[i-1].y
|
||||
}
|
||||
];
|
||||
|
||||
var yx = vectors[0].y * vectors[1].x;
|
||||
var xy = vectors[0].x * vectors[1].y;
|
||||
var direction = 0;
|
||||
if(yx > xy) {
|
||||
direction = -1;
|
||||
} else if (yx < xy) {
|
||||
direction = 1;
|
||||
}
|
||||
|
||||
var dotProduct = vectors[0].x
|
||||
* vectors[1].x
|
||||
+ vectors[0].y
|
||||
* vectors[1].y;
|
||||
|
||||
var currentLength = Math.sqrt(
|
||||
Math.pow(vectors[0].x, 2)
|
||||
+ Math.pow(vectors[0].y, 2)
|
||||
);
|
||||
var lastLength = Math.sqrt(
|
||||
Math.pow(vectors[1].x, 2)
|
||||
+ Math.pow(vectors[1].y, 2)
|
||||
);
|
||||
|
||||
var angle = 180 - (Math.acos((dotProduct / (currentLength * lastLength)) % 1) * 180 / Math.PI);
|
||||
angle *= direction;
|
||||
|
||||
if(!isNaN(parseFloat(angle)) && direction != 0) {
|
||||
this.angleSum += angle;
|
||||
}
|
||||
|
||||
if(!isNaN(parseFloat(currentLength))) {
|
||||
this.lengthSum += Math.abs(currentLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Swiper.prototype.swipeEnd = function(x, y) {
|
||||
var spin = this.angleSum;
|
||||
var length = this.lengthSum;
|
||||
var p0x = this.points[0].x;
|
||||
var p0y = this.points[0].y;
|
||||
var sumx = 0;
|
||||
var sumy = 0;
|
||||
|
||||
for(var i=0, count = this.points.length; i < count; i++) {
|
||||
var p = this.points[i];
|
||||
sumx += p.x - p0x;
|
||||
sumy += p.y - p0y;
|
||||
}
|
||||
|
||||
var direction = {
|
||||
x: sumx / count,
|
||||
y: sumy / count
|
||||
};
|
||||
|
||||
var larger = Math.abs(direction.x) > Math.abs(direction.y) ? direction.x : direction.y;
|
||||
direction.x /= Math.abs(larger);
|
||||
direction.y /= Math.abs(larger);
|
||||
|
||||
this.angleSum = 0;
|
||||
this.lengthSum = 0;
|
||||
this.points = [];
|
||||
|
||||
return {
|
||||
x: direction.x * length / 100,
|
||||
y: direction.y * length / 100,
|
||||
spin: spin
|
||||
}
|
||||
}
|
||||
|
||||
return Swiper;
|
||||
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue