Skip to content
Browse files

final commit for team panda, small gravity + collision changes

  • Loading branch information...
1 parent 0052fb0 commit 9ca7e47685ee91d852acddcd0d5c37a7de68e68a Henry committed Aug 26, 2011
Showing with 70 additions and 181 deletions.
  1. +52 −16 BUGswarmiPhone/webend/balls.js
  2. +18 −165 BUGswarmiPhone/webend/index.html
View
68 BUGswarmiPhone/webend/balls.js
@@ -6,13 +6,13 @@
var API;
var ID;
- var Ball = function(uiRep, id) {
+ var Ball = function(uiRep, id, xc, yc) {
this.uiRep = uiRep; //the css element
this.id = id;
// Position Variables
- this.x = (document.documentElement.clientWidth - 20 )/ 2;
- this.y = (document.documentElement.clientHeight - 20)/ 2;
+ this.x = xc;
+ this.y = yc;
// Speed - Velocity
this.vx = 0;
@@ -22,13 +22,46 @@
this.ax = 0;
this.ay = 0;
- this.vMultiplier = 0.2;
+ // Velocity Multiplier
+ this.vMultiplier = 0.1;
};
-
- Ball.prototype.getId = function() { return this.id; }
function checkCollision() {
- console.log("implosion!");
+ for (var key in Balls)
+ {
+ var ball = Balls[key];
+ for (var key2 in Balls)
+ {
+ if (key2 != key)
+ {
+ var ball2 = Balls[key2];
+ if (Math.sqrt(Math.pow(Math.abs(ball2.x - ball.x), 2)
+ + Math.pow(Math.abs(ball2.y - ball.y), 2)) < 50 )
+ {
+ if (ball.id > ball2.id)
+ {
+ console.log("collision!");
+ Balls[key].vx = -(ball.vx / 2);
+ Balls[key].vy = -(ball.vy / 2);
+ Balls[key2].vx = -(ball2.vx / 2);
+ Balls[key2].vy = -(ball2.vy / 2);
+ Balls[key].ax = 0;
+ Balls[key].ay = 0;
+ Balls[key2].ax = 0;
+ Balls[key2].ay = 0;
+ console.log(Balls[key].vx);
+ console.log(Balls[key].vy);
+ console.log(Balls[key2].vx);
+ console.log(Balls[key2].vy);
+//Balls[key].uiRep.style.top = Math.floor(Math.random()*(document.documentElement.clientHeight - 50));
+//Balls[key2].uiRep.style.top = Math.floor(Math.random()*(document.documentElement.clientHeight - 50));
+//Balls[key].uiRep.style.left = Math.floor(Math.random()*(document.documentElement.clientWidth - 50));
+//Balls[key2].uiRep.style.left = Math.floor(Math.random()*(document.documentElement.clientWidth - 50));
+ }
+ }
+ }
+ }
+ }
}
@@ -43,19 +76,21 @@
ball.y = parseInt(ball.y + ball.vy * ball.vMultiplier);
ball.x = parseInt(ball.x + ball.vx * ball.vMultiplier);
- if (ball.x<0 || ball.y<0 || ball.x>document.documentElement.clientWidth - 20 || ball.y>document.documentElement.clientHeight - 20)
+ if (ball.x<0 || ball.y<0 || ball.x>document.documentElement.clientWidth - 50 || ball.y>document.documentElement.clientHeight - 50)
{
- ball.x = (document.documentElement.clientWidth - 20)/ 2; ball.vx = 0; ball.ax = 0;
- ball.y = (document.documentElement.clientHeight - 20)/ 2; ball.vy = 0; ball.ay = 0
+ ball.vx = -(ball.vx / 2); ball.ax = 0;
+ ball.vy = -(ball.vy / 2); ball.ay = 0;
}
ball.uiRep.style.top = ball.y + "px";
- ball.uiRep.style.left = ball.x + "px";
+ ball.uiRep.style.left = ball.x + "px";;
+
//console.log(ball.uiRep.style.top);
//console.log(ball.uiRep.style.left);
}
+ checkCollision();
t = setTimeout("update()", 50);
}
@@ -82,12 +117,13 @@
d.setAttribute("id", browserID);
d.setAttribute("class", "ball");
d.style.backgroundColor = Math.floor(Math.random()*16777215).toString(16);
+ var x = Math.floor(Math.random()*(document.documentElement.clientWidth - 50));
+ var y = Math.floor(Math.random()*(document.documentElement.clientHeight - 50));
+ d.style.top = y;
+ d.style.left = x;
document.getElementById('data').appendChild(d);
- var b = new Ball(d, browserID);
+ var b = new Ball(d, browserID, x, y);
Balls[browserID] = b;
- console.log(Balls[browserID].uiRep.style);
- console.log(Balls[browserID].vy);
- console.log(Balls[browserID].vx);
console.log("added ball!");
}
@@ -109,7 +145,7 @@
{
if (panda.presence.type == "unavailable")
{
- //Balls[browserID].uiRep.style.opacity = 0;
+ Balls[browserID].uiRep.style.opacity = 0;
}
else
{
View
183 BUGswarmiPhone/webend/index.html
@@ -5,179 +5,32 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width,user-scalable=no" />
<style>
- #no {
- display: none;
- }
- #ball {
- width: 20px;
- height: 20px;
- border-radius: 10px;
- -webkit-radius: 10px;
- background-color: green;
- position:absolute;
- top: 0px;
- left: 0px;
- }
+ div.ball {
+ width: 50px;
+ height: 50px;
+ border-radius: 25px;
+ -webkit-radius: 25px;
+ position:absolute;
+ background-color: green;
+ top: 0px;
+ left: 0px;
+ opacity: 100; }
</style>
- <script>
-
- var highscore = 0;
- var score = 0;
- var j;
- var panda;
- var s;
+ <script type="text/javascript" src="cookies.js"></script>
+ <script type="text/javascript" src="balls.js"></script>
+</head>
+<body>
- var API;
- var ID;
-
- var kscore = new Boolean();
- kscore = false;
-
- function prompter() {
- var SWARMID = prompt("Yo, wats ur SWARMID?", "");
- createCookie("SWARMID", SWARMID, 7);
- var APIKEY = prompt("APIKEY noob", "");
- createCookie("APIKEY", APIKEY, 7);
- }
-
- function showCookies() {
- alert("SWARMID: " + readCookie("SWARMID") + " , APIKEY: "
- + readCookie("APIKEY"));
- }
-
- window.addEventListener( "devicemotion", onDeviceMotion, false );
- function onDeviceMotion( event )
- {
- try {
- var accel = event.accelerationIncludingGravity;
-
- //document.getElementById("data").innerHTML =
- //"x: " + accel.x + " y: " + accel.y + " z: " + accel.z;
-
- }
- catch (e)
- {
- console.log(e);
- }
- }
-
-
- function createCookie(name,value,days) {
- if (days) {
- var date = new Date();
- date.setTime(date.getTime()+(days*24*60*60*1000));
- var expires = "; expires="+date.toGMTString();
- }
- else var expires = "";
- document.cookie = name+"="+value+expires+"; path=/";
- }
-
- function readCookie(name) {
- var nameEQ = name + "=";
- var ca = document.cookie.split(';');
- for(var i=0;i < ca.length;i++) {
- var c = ca[i];
- while (c.charAt(0)==' ') c = c.substring(1,c.length);
- if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
- }
- return null;
- }
-
- function eraseCookie(name) {
- createCookie(name,"",-1);
- }
-
- function keepScore() {
-
- if (score > highscore) { highscore = score; }
- document.getElementById("score").innerHTML = "Score : " + score + " , High Score : " + highscore;
- score++;
- kscore = true;
- t=setTimeout("keepScore()",1000);
- }
-
- // Position Variables
- var x = (document.documentElement.clientWidth - 20 )/ 2;
- var y = (document.documentElement.clientHeight - 20)/ 2;
-
- // Speed - Velocity
- var vx = 0;
- var vy = 0;
-
- // Acceleration
- var ax = 0;
- var ay = 0;
-
- var delay = 10;
- var vMultiplier = 0.2;
-
- function update() {
- vy = vy + -(ay);
- vx = vx + ax;
-
- var ball = document.getElementById("ball");
- y = parseInt(y + vy * vMultiplier);
- x = parseInt(x + vx * vMultiplier);
-
- if (x<0) { //alert("IMPLOSION");
- x = (document.documentElement.clientWidth - 20 )/ 2; vx = 0; ax = 0;
- y = (document.documentElement.clientHeight - 20)/ 2; vy = 0; ay = 0; score = 0;}
- if (y<0) { //alert("IMPLOSION");
- x = (document.documentElement.clientWidth - 20 )/ 2; vx = 0; ax = 0;
- y = (document.documentElement.clientHeight - 20)/ 2; vy = 0; ay = 0; score = 0;}
- if (x>document.documentElement.clientWidth - 20) { //alert("IMPLOSION");
- x = (document.documentElement.clientWidth - 20 )/ 2; vx = 0; ax = 0;
- y = (document.documentElement.clientHeight - 20)/ 2; vy = 0; ay = 0; score = 0;}
- if (y>document.documentElement.clientHeight - 20) { //alert("IMPLOSION");
- x = (document.documentElement.clientWidth - 20 )/ 2; vx = 0; ax = 0;
- y = (document.documentElement.clientHeight - 20)/ 2; vy = 0; ay = 0; score = 0;}
-
- //ball.style.backgroundColor = Math.floor(Math.random()*16777215).toString(16);
- ball.style.top = y + "px";
- ball.style.left = x + "px";
- }
-
-
-
-
- function joinSwarm(){
- API="fae2bf42003e78465abebb70524f5ce29bda83ac";
- ID="f8d31289f8f6db7d7a66a46d893e74bcaf9dbcc7";
- SWARM.join({apikey: API, swarms: ID, callback: function(message) {
- j = JSON.stringify(message);
- console.log(Date.now() + ': ' + JSON.stringify(message));
- panda = eval('(' + j + ')');
- s = eval('(' + panda.message.body + ')');
- document.getElementById("test").innerHTML = "x : " + s.x +
- ", y : " + s.y +
- ", z : " + s.z;
- ax = s.x;
- ay = s.y;
- update();
-
- }
- });
- }
-
- </script>
- </head>
- <body>
<h1>If you let the ball touch an edge, your phone will implode.</h1>
<input type="button" onclick="prompter()" value="I want to input my apikey and swarmID YO">
<input type="button" onclick="showCookies()" value="Show me my saved cookies yo!">
- <input type="button" onclick="keepScore()" value="Start keeping score yo!">
<input type="button" onclick="joinSwarm()" value="Join Swarm">
- <div id="data" style="font-size: 24pt" align="center"></div>
- <div id="content">
- <div id="yes">
+ <div id="content">
<p>Mantain your device in portrait orientation for better results (you can lock it)</p>
- <div id="ball"></div>
- </div>
- <div id="no"></div>
- <div id="score"></div>
<div id="test"></div>
- </div>
+ <div id="data"></div>
+ </div>
- </body>
+</body>
</html>

0 comments on commit 9ca7e47

Please sign in to comment.
Something went wrong with that request. Please try again.