Created
November 8, 2022 01:41
-
-
Save haehn/9c36c57f2c845eb8791331628896ef5b to your computer and use it in GitHub Desktop.
robot.js from class Fall 2022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Robot = function(x, y, z) { | |
// | |
// HEAD, NECK, TORSO | |
// | |
var fromhelper = HELPER.cylinderSkeletonMesh(3, 5, 'blue') | |
var geometry = fromhelper[0]; | |
var material = fromhelper[1]; | |
var bones = fromhelper[2]; | |
var mesh = new THREE.SkinnedMesh( geometry, material ); | |
var skeleton = new THREE.Skeleton( bones ); | |
mesh.add( bones[0] ); | |
mesh.bind( skeleton ); | |
// bones is 4 element array | |
// element 0 is anchor point | |
// element 1 is the head | |
// element 2 is the neck | |
// element 3 is the torso | |
this.root = bones[ 0 ]; // anchor point of robot | |
this.root.position.set( x, y, z ); | |
this.head = bones[ 1 ]; | |
this.neck = bones[ 2 ]; | |
this.neck.position.y = -10; | |
this.torso = bones[ 3 ]; | |
this.torso.position.y = -30; | |
this.body_mesh = mesh; | |
// | |
// LEFT ARM | |
// LEFT UPPER ARM, LEFT LOWER ARM, LEFT HAND | |
// | |
var fromhelper = HELPER.cylinderSkeletonMesh(3, 5, 'blue') | |
var geometry = fromhelper[0]; | |
var material = fromhelper[1]; | |
var bones = fromhelper[2]; | |
var mesh = new THREE.SkinnedMesh( geometry, material ); | |
var skeleton = new THREE.Skeleton( bones ); | |
mesh.add( bones[0] ); | |
mesh.bind( skeleton ); | |
// bones is 4 element array | |
// element 0 is anchor point | |
// element 1 is the upper arm | |
// element 2 is the lower arm | |
// element 3 is the hand | |
this.neck.add( bones[0] ); | |
this.left_upperarm = bones[ 1 ]; | |
this.left_upperarm.position.y = -5; | |
this.left_upperarm.position.x = 5; | |
this.left_lowerarm = bones[ 2 ]; | |
this.left_lowerarm.position.y = -15; | |
this.left_lowerarm.position.x = 5; | |
this.left_hand = bones[ 3 ]; | |
this.left_hand.position.x = 5; | |
this.left_hand.position.y = -5; | |
this.leftarm_mesh = mesh; | |
this.left_upperleg = new THREE.Bone(); | |
this.left_upperleg.position.x = 5; | |
this.left_upperleg.position.y = -5; | |
this.left_lowerleg = new THREE.Bone(); | |
this.left_lowerleg.position.x = 5; | |
this.left_lowerleg.position.y = -15; | |
this.left_foot = new THREE.Bone(); | |
this.left_foot.position.x = 5; | |
this.left_foot.position.y = -5; | |
this.torso.add(this.left_upperleg); | |
this.left_upperleg.add(this.left_lowerleg) | |
this.left_lowerleg.add(this.left_foot); | |
this.right_upperarm = new THREE.Bone(); | |
this.right_upperarm.position.y = -5; | |
this.right_upperarm.position.x = -5; | |
this.right_lowerarm = new THREE.Bone(); | |
this.right_lowerarm.position.y = -15; | |
this.right_lowerarm.position.x = -5; | |
this.right_hand = new THREE.Bone(); | |
this.right_hand.position.x = -5; | |
this.right_hand.position.y = -5; | |
this.neck.add(this.right_upperarm); | |
this.right_upperarm.add(this.right_lowerarm) | |
this.right_lowerarm.add(this.right_hand); | |
this.right_upperleg = new THREE.Bone(); | |
this.right_upperleg.position.x = -5; | |
this.right_upperleg.position.y = -5; | |
this.right_lowerleg = new THREE.Bone(); | |
this.right_lowerleg.position.x = -5; | |
this.right_lowerleg.position.y = -15; | |
this.right_foot = new THREE.Bone(); | |
this.right_foot.position.x = -5; | |
this.right_foot.position.y = -5; | |
this.torso.add(this.right_upperleg); | |
this.right_upperleg.add(this.right_lowerleg) | |
this.right_lowerleg.add(this.right_foot); | |
this.movement = null; | |
}; | |
Robot.prototype.show = function(scene) { | |
scene.add( this.body_mesh ); | |
scene.add( this.leftarm_mesh ); | |
// var rGroup = new THREE.Group(); | |
// rGroup.add( this.head ); | |
// var helper = new THREE.SkeletonHelper( rGroup ); | |
// helper.material.linewidth = 3; | |
// scene.add(rGroup); | |
// scene.add(helper); | |
}; | |
Robot.prototype.raise_left_arm = function() { | |
this.movement = 'raise left arm'; | |
}; | |
Robot.prototype.lower_left_arm = function() { | |
this.movement = 'lower left arm'; | |
}; | |
Robot.prototype.kick = function() { | |
this.movement = 'kick'; | |
}; | |
Robot.prototype.dance = function() { | |
this.movement = 'dance'; | |
}; | |
Robot.prototype.onAnimate = function() { | |
if (this.movement == 'raise left arm') { | |
var T = Math.PI; | |
this.left_upperarm.quaternion.slerp( new THREE.Quaternion(Math.sin(-T/2), // w | |
0, // x | |
0, // y | |
Math.cos(-T/2)), // z | |
0.1 ); | |
} else if (this.movement == 'lower left arm') { | |
this.left_upperarm.quaternion.slerp( new THREE.Quaternion(0, 0, 0, 1), | |
0.1 ); | |
} else if (this.movement == 'kick') { | |
// check if slerp reached almost the end | |
if (this.right_upperleg.quaternion.w < 0.72) { | |
// signal that the kick is done and the leg should move back | |
this.movement = 'kick done'; | |
} else { | |
var T = -Math.PI/2; | |
this.right_upperleg.quaternion.slerp( new THREE.Quaternion( Math.sin( T / 2 ), // x | |
0, // y | |
0, // z | |
Math.cos( T / 2 ) ), // w | |
0.1 ); | |
} | |
} else if (this.movement == 'kick done') { | |
// reset leg back to identity | |
this.right_upperleg.quaternion.slerp( new THREE.Quaternion(0,0,0,1), 0.1 ); | |
} else if (this.movement == 'dance') { | |
if (typeof this.dancer === 'undefined') { | |
this.dancer = setInterval(function() { | |
// | |
// some random translation | |
// | |
var shakehead = 3*Math.random(); | |
if (Math.random() < .5) { | |
shakehead *= -1; | |
} | |
var shakeneck = 3*Math.random(); | |
if (Math.random() < .5) { | |
shakeneck *= -1; | |
} | |
var shaketorso = 3*Math.random(); | |
if (Math.random() < .5) { | |
shaketorso *= -1; | |
} | |
this.head.position.x += shakehead; | |
this.neck.position.x += shakeneck; | |
this.torso.position.x += shaketorso; | |
// | |
// use actions | |
// | |
if (Math.random() < .3) { | |
this.raise_left_arm(); | |
} | |
if (Math.random() < .3) { | |
this.lower_left_arm(); | |
} | |
if (Math.random() < .3) { | |
this.kick(); | |
} | |
if (Math.random() < .3) { | |
this.movement = 'kick done'; | |
} | |
}.bind(this), 500); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment