nice kicking

beta.r3js.org
-=yb4f310 2018-05-20 16:20:34 +02:00
parent b17e28f8bb
commit 87b4966bc5
1 changed files with 89 additions and 95 deletions

View File

@ -33,18 +33,20 @@ this.playerRotation = {
y : 0 y : 0
}; };
this.kick = {
start : null,
distance : 0,
time : 0,
speed : 0,
direction : 0
};
this.balls = []; this.balls = [];
this.activeBall = this.football.instance; this.activeBall = this.football.instance;
this.canKick = false;
this.kick = false;
this.startTime = null;
this.kickDuration = 0;
this.kickDistance = 0;
R3.CustomCode.prototype.calcPosition = function(shotAngle, lookAngle, t) { R3.CustomCode.prototype.calcPosition = function(shotAngle, lookAngle, t) {
@ -84,7 +86,7 @@ R3.CustomCode.prototype.render = function(delta) {
); );
ball.mesh.rotateOnAxis(v.normalize(), -0.1); ball.mesh.rotateOnAxis(v.normalize(), -0.2);
//object.ball.translateOnAxis(object.v, 1); //object.ball.translateOnAxis(object.v, 1);
@ -101,44 +103,79 @@ R3.CustomCode.prototype.move = function(data) {
var y = data.y; var y = data.y;
if (data.source === 'mouse' || 'touch') { if (data.source === 'mouse' || 'touch') {
x *= 0.01; x *= 0.0025;
y *= 0.01; y *= 0.0075;
} }
this.playerRotation.x += x; this.playerRotation.x += x;
this.playerRotation.y += y; this.playerRotation.y += y;
var kick = false;
var canKick = false;
if (this.playerRotation.y > Math.PI) {
canKick = true;
}
/** /**
* Clamp player rotation to values between 0 and Math.PI * 2 * Clamp player rotation to values between 0 and Math.PI * 2
*/ */
if (this.playerRotation.y > Math.PI * 2) { if (this.playerRotation.y > Math.PI) {
this.playerRotation.y -= Math.PI * 2; this.playerRotation.y = Math.PI;
kick = true;
canKick = false;
} }
if (this.playerRotation.y < 0) { if (this.playerRotation.y < -Math.PI) {
this.playerRotation.y += Math.PI * 2; this.playerRotation.y = -Math.PI;
canKick = true;
} }
/** if (this.playerRotation.x > Math.PI / 2) {
* Clamp direction angle between 0 and Math.PI * 2 this.playerRotation.x = Math.PI / 2;
*/
if (this.playerRotation.x > Math.PI * 2) {
this.playerRotation.x -= Math.PI * 2;
} }
if (this.playerRotation.x < 0) { if (this.playerRotation.x < -Math.PI / 2) {
this.playerRotation.x += Math.PI * 2; this.playerRotation.x = -Math.PI / 2;
}
if (this.playerRotation.y <= 0) {
this.canKick = true;
}
if (this.canKick && this.playerRotation.y > 0) {
this.canKick = false;
this.kick = true;
}
if (y > 0 && this.canKick) {
/**
* Moving down, initialize start time and record the distance travelled
*/
if (this.startTime === null) {
this.startTime = Date.now();
}
this.kickDistance += Math.abs(y);
}
if (y < 0) {
/**
* We are no longer moving down, also reset our Y distance counter
* @type {boolean}
*/
this.kickDistance = 0;
this.startTime = null;
}
if (this.canKick && this.activeBall === null) {
console.log('can kick - creating new ball');
this.activeBall = new THREE.Mesh(
this.ballGeometry.instance,
[
this.materialWhite.instance,
this.materialBlack.instance
]
);
this.activeBall.scale.copy(this.football.instance.scale);
this.football.parentScene.instance.add(this.activeBall);
} }
this.footballPlayer.instance.matrix = new THREE.Matrix4(); this.footballPlayer.instance.matrix = new THREE.Matrix4();
@ -152,45 +189,48 @@ R3.CustomCode.prototype.move = function(data) {
this.footballPlayer.instance.rotateY(this.playerRotation.x); this.footballPlayer.instance.rotateY(this.playerRotation.x);
this.footballPlayer.instance.translateZ(0.2); this.footballPlayer.instance.translateZ(0.3);
this.footballPlayer.instance.rotateX(this.playerRotation.y); this.footballPlayer.instance.rotateX(this.playerRotation.y);
if (kick) { if (this.kick) {
if (this.kick.start === null) { this.kick = false;
if (this.startTime === null) {
console.warn('no kick start time'); console.warn('no kick start time');
return; return;
} }
this.kick.time = Date.now() - this.kick.start; var kickDuration = Date.now() - this.startTime;
this.kick.speed = this.kick.distance / this.kick.time * 40; var speed = this.kickDistance / kickDuration * 20;
this.kick.direction = this.playerRotation.x; // if (speed > 1.5) {
// speed = 1.5;
// }
if (this.kick.speed > 1) { console.log(speed);
this.kick.speed = 1;
}
console.log(this.kick.speed);
var v = new THREE.Vector3(0, 0, -1); var v = new THREE.Vector3(0, 0, -1);
v.multiplyScalar(this.kick.speed); v.multiplyScalar(speed);
v.applyAxisAngle( v.applyAxisAngle(
new THREE.Vector3(0,1,0), new THREE.Vector3(0,1,0),
this.kick.direction this.playerRotation.x
); );
this.balls.push( this.balls.push(
{ {
mesh : this.activeBall, mesh : this.activeBall,
speed : 6, speed : 8,
mouseYTravelDistance : this.kick.distance, mouseYTravelDistance : this.kick.distance,
directionAngle : this.kick.direction + Math.PI, directionAngle : this.playerRotation.x + Math.PI,
upAngle : Math.PI * this.kick.speed, kickDuration : this.kickDuration,
kickDistance : this.kickDistance,
kickSpeed : speed,
upAngle : Math.PI * speed,
v : v, v : v,
t : 0, t : 0,
s0 : new THREE.Vector3(0,0,0), s0 : new THREE.Vector3(0,0,0),
@ -199,56 +239,10 @@ R3.CustomCode.prototype.move = function(data) {
); );
this.activeBall = null; this.activeBall = null;
//console.log('kick', this.kick);
} }
if (canKick && this.activeBall === null) {
//console.log('can kick');
this.activeBall = new THREE.Mesh(
this.ballGeometry.instance,
[
this.materialWhite.instance,
this.materialBlack.instance
]
);
this.activeBall.scale.copy(this.football.instance.scale);
//this.activeBall.scale.copy(this.football.instance.scale);
this.football.parentScene.instance.add(this.activeBall);
}
if (y > 0 && canKick) {
if (this.kick.start === null) {
this.kick.start = Date.now();
}
/**
* Moving down, record the distance travelled
*/
this.kick.distance += Math.abs(y);
//console.log(this.kick.distance);
}
if (y < 0) {
/**
* We are no longer moving down, also reset our Y distance counter
* @type {boolean}
*/
this.kick = {
start : null,
distance : 0,
time : 0,
speed : 0,
direction : 0
};
}
}.bind(this) }.bind(this)