Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

working box-sphere collision

  • Loading branch information...
commit 046e4bd713eb020cab5d1941de5eb7d5f82fb782 1 parent 8c91e7c
@fridek authored
Showing with 16 additions and 26 deletions.
  1. +6 −21 js/collision.js
  2. +2 −2 js/load.js
  3. +5 −0 js/object.js
  4. +3 −3 js/utils.js
View
27 js/collision.js
@@ -34,12 +34,11 @@ function detectCollisionBoxSphere(box, sphere) {
// sphere centre relative to box centre
relCentre = M.subVec(sphere.position,box.position),
// box size
- // box_size = M.multiVec(box.scale,0.5),
box_size = box.scale,
collisionNormal, penetration, Vrn, restitution = 1, closestPtWorld, j, i;
- // simple check if sphere is too far to possibly have any contact
+ // simple check if sphere is too far to possibly have any contact (SAT)
if (Math.abs(relCentre[0]) - sphere.boundingSphereRadius > box_size[0] ||
Math.abs(relCentre[1]) - sphere.boundingSphereRadius > box_size[1] ||
Math.abs(relCentre[2]) - sphere.boundingSphereRadius > box_size[2] ) {
@@ -65,31 +64,17 @@ function detectCollisionBoxSphere(box, sphere) {
closestPtWorld = M.addVec(closestPt,box.position);
- collisionNormal = M.subVec(closestPtWorld, centre);
+ collisionNormal = M.subVec(centre, closestPtWorld);
collisionNormal = M.vecNormalise(collisionNormal);
-/*
+ sphere.stepBack();
+
Vrn = [];
for(i=0;i<collisionNormal.length;i+=1) Vrn[i] = collisionNormal[i] * sphere.speed[i];
-// i = M.multiVecVec(collisionNormal, collisionNormal);
-// if(i != 0) {
-
j = M.multiVec(Vrn,-(1+restitution));
-// } else {
-// j = 0;
-// }
- console.log('collision', Date.now(),
- 'col normal', collisionNormal,
- 'penetration', penetration,
- 'Vrn', Vrn,
- 'j', j,
- 'sphere speed', sphere.speed
- );
-// console.log(sphere.speed, M.multiVec(collisionNormal,j));
-
- sphere.setMovement(M.subVec(sphere.speed, j));
-*/
+ sphere.setMovement(M.addVec(sphere.speed, j));
+
return true;
}
View
4 js/load.js
@@ -1,11 +1,11 @@
var settings = {
gravity: [0.0,-1.0,0.0],
- floorLevel: -10,
+ floorLevel: -15,
floorBounce: 0.5
};
$(window).ready(function() {
- var objects = generateSpheresCube(4,4,4,2,true);
+ var objects = generateSpheresCube(5,5,5,2,true);
initScene(settings);
for(var i = 0; i < objects.length; i+=1) {
View
5 js/object.js
@@ -57,6 +57,11 @@ var makeObject = function (type, position, scale, boundingSphereRadius, id) {
this.position = this.nextPosition;
},
+ stepBack: function () {
+ this.nextPosition = this.prevPosition;
+ this.position = this.prevPosition;
+ },
+
prevPosition: [0.0,0.0,0.0],
position: position,
nextPosition: [0.0,0.0,0.0],
View
6 js/utils.js
@@ -26,9 +26,9 @@ function generateSpheresCube(x,y,z,distance,random_movements) {
k_end = 0.5 * z * distance,
objects = [];
- for(i = i_start; i <= i_end; i+=distance) {
- for(j = j_start; j <= j_end; j+=distance) {
- for(k = k_start; k <= k_end; k+=distance) {
+ for(i = i_start; i < i_end; i+=distance) {
+ for(j = j_start; j < j_end; j+=distance) {
+ for(k = k_start; k < k_end; k+=distance) {
objects[cnt] = makeObject('sphere', [i,j,k], [0.5,0.5,0.5], null, "box" + cnt);
if(random_movements) {
objects[cnt].setMovement([0.0,0.0,0.0], [Math.random()-0.5,Math.random()-0.5,Math.random()-0.5]);
Please sign in to comment.
Something went wrong with that request. Please try again.