Skip to content

Commit

Permalink
working box-sphere collision
Browse files Browse the repository at this point in the history
  • Loading branch information
fridek committed Jun 13, 2011
1 parent 8c91e7c commit 046e4bd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
27 changes: 6 additions & 21 deletions js/collision.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] ) {
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions js/load.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions js/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
6 changes: 3 additions & 3 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 046e4bd

Please sign in to comment.