Skip to content

Commit

Permalink
Added mouse controls
Browse files Browse the repository at this point in the history
- Pressing the mouse / holding finger down should move forward
- Dragging the mouse / finger should rotate
- State transitions between forward and rotate could be handled better
Also, added side panel with debug info, but hid it for now...
  • Loading branch information
AluminumOxide committed Oct 28, 2016
1 parent c679f06 commit 5cbfb55
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
78 changes: 74 additions & 4 deletions server/static/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ var cameraDirection = new Vector(0, 1, 0);
var lastRenderDirection = new Vector(0, 1, 0);
var upDirection = new Vector(0, 0, 1);
var keys = new Array();
var draggingCanvas = false;
var dragVector = {x: null, y: null};
var pressForward = false;
var activeDrag = false;

function initKeys() {
for (var i = 0; i < 256; ++i) {
Expand All @@ -22,10 +26,73 @@ function initKeys() {
window.onkeyup = function(event) {
keys[event.keyCode] = 0;
};

window.onkeydown = function(event) {
keys[event.keyCode] = 1;
};
window.ontouchstart = function(event) {
startDrag(event.touches[0].clientX,event.touches[0].clientY);
};
window.onmousedown = function(event) {
startDrag(event.clientX,event.clientY);
};
window.ontouchmove = function(event) {
dragCanvas(event.touches[0].clientX,event.touches[0].clientY);
};
window.onmousemove = function(event) {
dragCanvas(event.clientX,event.clientY);
};
window.ontouchend = function(event) {
endDrag();
};
window.onmouseup = function(event) {
endDrag();
};

function startDrag(x,y) {
draggingCanvas = true;
pressForward = false;
activeDrag = false;
dragVector = {x: x, y: y};
setTimeout(function(){if(draggingCanvas && !activeDrag){pressForward = true;}}, 500);
};

function endDrag() {
draggingCanvas = false;
pressForward = false;
activeDrag = false;
dragVector = {x: null, y: null};
};

function dragCanvas(x,y) {
if(dragVector.x == null || dragVector.y == null) {
dragVector.x = x;
dragVector.y = y;
}
if(draggingCanvas && Math.abs(x-dragVector.x) > 2 && Math.abs(y-dragVector.y) > 2) {
pressForward = false;
activeDrag = true;
var right = Vector.crossProduct(cameraDirection, upDirection);
if(y < dragVector.y) {
cameraDirection = cameraDirection.rotate(right, turnRate*((dragVector.y-y)/10));
upDirection = upDirection.rotate(right, turnRate*((dragVector.y-y)/10));

} else if(y > dragVector.y) {
cameraDirection = cameraDirection.rotate(right, -turnRate*((y-dragVector.y)/10));
upDirection = upDirection.rotate(right, -turnRate*((y-dragVector.y)/10));
}
if(x < dragVector.x) {
cameraDirection = cameraDirection.rotate(upDirection, -turnRate*((dragVector.x-x)/10));
} else if(x > dragVector.x) {
cameraDirection = cameraDirection.rotate(upDirection, turnRate*((x-dragVector.x)/10));
}
dragVector.x = x;
dragVector.y = y;
} else if(draggingCanvas) {
activeDrag = false;
setTimeout(function(){if(draggingCanvas && !activeDrag){pressForward = true;}}, 1000);
}

};

function renderStar(context, screenX, screenY, area, color) {
var radius = Math.sqrt(area / Math.PI);
Expand All @@ -46,7 +113,8 @@ function renderStar(context, screenX, screenY, area, color) {
}

function doOneFrame() {
if (keys[87]) {
//if (keys[87] ) {
if (keys[87] || pressForward) {
cameraPosition.addInPlace(cameraDirection.multiplyScalar(speed));
}
if (keys[83]) {
Expand All @@ -60,8 +128,8 @@ function doOneFrame() {
cameraPosition.addInPlace(right.multiplyScalar(speed));
}
if (keys[38]) {
cameraDirection = cameraDirection.rotate(right, -turnRate);
upDirection = upDirection.rotate(right, -turnRate);
cameradirection = cameradirection.rotate(right, -turnrate);
updirection = updirection.rotate(right, -turnrate);
}
if (keys[40]) {
cameraDirection = cameraDirection.rotate(right, turnRate);
Expand All @@ -81,6 +149,8 @@ function doOneFrame() {
}
lastRenderPosition.copyFrom(cameraPosition);
lastRenderDirection.copyFrom(cameraDirection);
document.getElementById('controlsBody').innerHTML = "Position: "
+cameraPosition.print()+"<br>Direction: "+cameraDirection.print();
starsUpdated = false;

var canvas = document.getElementById('stars');
Expand Down
1 change: 1 addition & 0 deletions server/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</head>
<body>
<canvas id="stars" width="100" height="100"></canvas>
<div id="controls"><div id="controlsBody">test test test</div></div>
<script src="StarMap.js"></script>
<script src="colorMap.js"></script>
<script src="vector.js"></script>
Expand Down
12 changes: 12 additions & 0 deletions server/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@ html, body {
height: 100%;
margin: 0px;
}
#controls {
position: absolute;
z-index: 2;
top: 0px;
right: 0px;
width: 310px;
height: 100%;
padding: 5px;
margin: 0px;
background-color:rgba(255,255,255,0.5);
display:none;
}
4 changes: 4 additions & 0 deletions server/static/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ function Vector(x, y, z) {
this.z = z;
}

Vector.prototype.print = function() {
return "X: "+this.x+" Y: "+this.y+" Z: "+this.z;
}

Vector.prototype.copyFrom = function(v) {
this.x = v.x;
this.y = v.y;
Expand Down

0 comments on commit 5cbfb55

Please sign in to comment.