Skip to content

Commit

Permalink
transitions midwork
Browse files Browse the repository at this point in the history
  • Loading branch information
ivankuzev committed Feb 5, 2012
1 parent 893a15a commit 89ee528
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 76 deletions.
2 changes: 1 addition & 1 deletion dummies.js
@@ -1,4 +1,4 @@
systemData = {
dummyData = {
star: {
type: "g",
size: 1.9
Expand Down
1 change: 1 addition & 0 deletions index.html
Expand Up @@ -70,6 +70,7 @@
<script src="./js/app/app.js"></script>
<script src="./js/app/HyperEngine.js"></script>
<script type="text/javascript" src="./js/app/controlers.app.js"></script>
<script type="text/javascript" src="./js/app/controlers.network.js"></script>
<script type="text/javascript" src="./js/app/controlers.webgl.js"></script>
<script type="text/javascript" src="./js/app/stage.galaxy.js"></script>
<script type="text/javascript" src="./js/app/stage.starsystem.js"></script>
Expand Down
1 change: 1 addition & 0 deletions js/app/HyperEngine.js
Expand Up @@ -80,6 +80,7 @@ function createStars() {
x: left,
y: depth,
z: top,
name: 'x'+ left + 'y' + depth + 'z' + top,
type: starType()
});

Expand Down
13 changes: 13 additions & 0 deletions js/app/controlers.network.js
@@ -0,0 +1,13 @@
App.Controllers.Network = (function() {




return {
//function for getting all information
getSystemInfo:function(id,callback){

callback({act:'GalaxyToStar',data:dummyData})
}
}
}());
24 changes: 15 additions & 9 deletions js/app/controlers.webgl.js
Expand Up @@ -41,9 +41,10 @@ App.Controllers.webgl = (function() {
this.jqDiv.append(stats.domElement);
this.jqDiv.append(this.renderer.domElement);

//render the current stage
//initialize all stages
App.Stages.StarSystem.initialize(this)
App.Stages.Galaxy.initialize(this)
currentStage = App.Stages.Galaxy;
currentStage.initialize(this);
//event binding

this.jqDiv.on('mousedown mouseup mousemove dblclick click mousewheel', this._event);
Expand All @@ -52,7 +53,15 @@ App.Controllers.webgl = (function() {
this.animate();
},


switchStages:function(arguments){
if (arguments.act === 'GalaxyToStar'){
currentStage = App.Stages.StarSystem;
currentStage.showSystem(arguments.data);
App.Stages.Galaxy.cameraDistance = 10;
App.Stages.StarSystem.cameraRotations = App.Stages.Galaxy.cameraRotations;

}
},
/*
function animate
Game loop - requests each new frame
Expand Down Expand Up @@ -95,14 +104,11 @@ App.Controllers.webgl = (function() {
//gets position in the z plane of a givvent mouse coordinates
getIntersectionWithY:function(camera,xyPosition,y){

var mousex = ( xyPosition.x / this.jqDiv.width()) * 2 - 1;
var mousey = - ( xyPosition.y / this.jqDiv.height()) * 2 + 1;

var origin = camera.position;
var vector = this.projector.unprojectVector(new THREE.Vector3(mousex,mousey,1), camera);
var vector = this.projector.unprojectVector(new THREE.Vector3(xyPosition.x,xyPosition.y,1), camera);

var y_plane_point = y;
vector.subSelf(origin).normalize();
var y_plane_point = y;
vector.subSelf(origin).normalize();
var scalar =(y_plane_point - origin.y) / vector.y
var intersection = origin.clone().addSelf( vector.multiplyScalar(scalar) );

Expand Down
106 changes: 49 additions & 57 deletions js/app/stage.galaxy.js
Expand Up @@ -4,11 +4,9 @@ App.Stages.Galaxy = (function() {
var scene;
var materials= [];
var starlist;
var farestCameraPosition = 40000;
var nearestCameraPosition = 200;
//the levels of zoom
var zoomLevelCurrent = 0;//
var zoomLevelCount = 37;
var zoomLevelCurrent = 1;//
var zoomLevelCount = 50;
var controller;
var particleSystem;
var starSize = 5;
Expand Down Expand Up @@ -104,13 +102,18 @@ App.Stages.Galaxy = (function() {
},

update: function(){
//we have to rewrite this one
if (ctrPressed) {

}

//updating camera position depending on controlls
var distanceVector = new THREE.Vector3(0,0,-this.cameraDistance);
rotationMatrix.setRotationX(controller.degreesToRadians(this.cameraRotations.x));
distanceVector = rotationMatrix.multiplyVector3(distanceVector);
rotationMatrix.setRotationY(controller.degreesToRadians(this.cameraRotations.y));
distanceVector = rotationMatrix.multiplyVector3(distanceVector);

//i had trouble with just 1 line so i used 3
camera.position.x = this.cameraLookTarget.x;
camera.position.y = this.cameraLookTarget.y;
camera.position.z = this.cameraLookTarget.z;
Expand All @@ -125,22 +128,10 @@ App.Stages.Galaxy = (function() {

render: function(){
//call render for the stage

if (ctrPressed) {
//camera.position.x += ( mouseX - camera.position.x ) * 0.01;
//camera.position.y += ( -mouseY - camera.position.y ) * 0.01;
camera.lookAt( {
x: ( mouseX - camera.position.x ) * 0.01,
y: ( -mouseY - camera.position.y ) * 0.01,
z: 0
} );
}
//postprocessing render
controller.renderer.clear();
composer.render(0.05);

// controller.renderer.render(scene,camera);

},

onKeyDown: function(e) {
Expand All @@ -157,10 +148,12 @@ App.Stages.Galaxy = (function() {
this.cameraRotations.y += 1
//up
}else if (e.keyCode === 38) {
this.cameraRotations.x -= 1
this.cameraRotations.x += 2;
if(this.cameraRotations.x > 80)this.cameraRotations.x =80;
//down
}else if (e.keyCode === 40) {
this.cameraRotations.x += 1
this.cameraRotations.x -= 2;
if(this.cameraRotations.x < -80)this.cameraRotations.x = -80;
//add
}else if (e.keyCode === 107) {
this.cameraDistance -=100;
Expand All @@ -178,9 +171,7 @@ App.Stages.Galaxy = (function() {
},


getClosestStar:function(xy){
xy.x = ( xy.x / controller.jqDiv.width() ) * 2 - 1;
xy.y = -( xy.y / controller.jqDiv.height()) * 2 + 1;
getClosestStar:function(){
controller.toScreenPrepare(camera);
var target = starlist[0];
var targetDistance = 9999;
Expand All @@ -191,17 +182,18 @@ App.Stages.Galaxy = (function() {
}
var vector = new THREE.Vector3( starlist[i].x, starlist[i].y, starlist[i].z );
var pos2d = controller.toScreenXY(vector);
var distanceX = Math.abs(pos2d.x - xy.x);
var distanceY = Math.abs(pos2d.y - xy.y);
var distanceX = Math.abs(pos2d.x - currentMouse.x);
var distanceY = Math.abs(pos2d.y - currentMouse.y);
var distance = Math.sqrt(distanceX*distanceX+distanceY*distanceY);
if(targetDistance > distance){
target = starlist[i];
target = starlist[i];
target.id = i;
targetDistance = distance ;
}
}
return target;
},
zoomIn: function(xy){
zoomIn: function(){
if (zoomLevelCurrent === zoomLevelCount) {
return;
}
Expand All @@ -210,49 +202,50 @@ App.Stages.Galaxy = (function() {
//animate camera to new position
new TWEEN.Tween( this )
.to({
cameraPosition: (1 / zoomLevelCurrent) * 6000 - 100
// cameraDistance: (farestCameraPosition-nearestCameraPosition) - (((farestCameraPosition-nearestCameraPosition)/zoomLevelCount) *zoomLevelCurrent)+nearestCameraPosition
cameraDistance: (1 / zoomLevelCurrent) * 40000 - 750
}, 500 )
.start()

this.centerOn(xy)
this.centerOn()
},
zoomOut: function(xy){
var levelOne = false
zoomOut: function(){
//change the current zoom level
if (zoomLevelCurrent === 0) {
if (zoomLevelCurrent === 1) {
return;
} else if (zoomLevelCurrent === 1) {
levelOne = true
}
}
zoomLevelCurrent--;

new TWEEN.Tween( this )
.to({
cameraPosition: !levelOne ? (1 / zoomLevelCurrent) * 6000 - 100: farestCameraPosition
// cameraDistance: !levelOne ? (farestCameraPosition-nearestCameraPosition) - (((farestCameraPosition-nearestCameraPosition)/zoomLevelCount) *zoomLevelCurrent)+nearestCameraPosition: farestCameraPosition
cameraDistance: (1 / zoomLevelCurrent) * 40000 - 750
}, 500 )
.start()

this.centerOn(xy)
this.centerOn()
},

//moves the stars so that position is at the center of the screen
centerOn: function(mousePosition){
centerOn: function(){
var position;

if (mousePosition === undefined) {
if (currentMouse === undefined) {
position = {
x: 0,
y: 0,
z: 0
};
} else {
//method 1:look for intersection with z plane
position = controller.getIntersectionWithY(camera,mousePosition,0);
position = controller.getIntersectionWithY(camera,currentMouse,0);
//method 2:look for closest star to cursor
// position = this.getClosestStar(mousePosition);
// position = this.getClosestStar();

}

if(zoomLevelCount === zoomLevelCurrent){
position = this.getClosestStar();
}
new TWEEN.Tween( this.cameraLookTarget )
.to({
x: position.x,
Expand All @@ -263,7 +256,7 @@ App.Stages.Galaxy = (function() {


},

//mousewheel handler
onMouseWheel: function(event,delta){
event.preventDefault();
Expand All @@ -273,32 +266,31 @@ App.Stages.Galaxy = (function() {
return;
}

var clickX= event.pageX - $(event.target).position().left;
var clickY= event.pageY - $(event.target).position().top;
var mouseXY = {
x:clickX,
y:clickY
};
currentMouse.x = (event.offsetX / controller.jqDiv.width()) * 2 - 1;
currentMouse.y = -(event.offsetY / controller.jqDiv.height()) * 2 + 1;

if(delta>0){
this.zoomIn(mouseXY);
this.zoomIn();
}else{
this.zoomOut(mouseXY);
this.zoomOut();
}

},
//mouseclick handler
onMouseClick: function(event){

currentMouse.x = (event.offsetX / controller.jqDiv.width()) * 2 - 1;
currentMouse.y = -(event.offsetY / controller.jqDiv.height()) * 2 + 1;

if(zoomLevelCount === zoomLevelCurrent){
App.Controllers.Network.getSystemInfo(this.getClosestStar().id,controller.switchStages)
}

this.centerOn({
x: event.offsetX,
y: event.offsetY
});
this.centerOn();
},

onMouseMove: function(event) {
currentMouse.x = (event.clientX / window.innerWidth) * 2 - 1;
currentMouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
currentMouse.x = (event.offsetX / controller.jqDiv.width()) * 2 - 1;
currentMouse.y = -(event.offsetY / controller.jqDiv.height()) * 2 + 1;
},

//declaring all event handlers
Expand Down

0 comments on commit 89ee528

Please sign in to comment.