sbiermanlytle / iioEngine

iio Engine: A JavaScript game engine for HTML5 Canvas
http://iioengine.com
455 stars 81 forks source link

Polygon Rotation and Collision Detection #11

Closed everettejohnson closed 11 years ago

everettejohnson commented 11 years ago

Collision Detection on Rotating Polygons does not work.

function CollidingPoly(io){

    io.addGroup('g1');

    var poly1 = new iio.ioPoly(40,500,[0,0,80,10,20,70,-30,20])
        poly1.setFillStyle('red')
        poly1.enableKinematics()
        poly1.setTorque(0)
        poly1.setVel(1,0)
        poly1.setStrokeStyle('#000')
    io.addObj(poly1)
    io.addToGroup('g1',poly1)

    var poly2 = new iio.ioPoly(800,500,[0,0,80,10,20,70,-30,20])
        poly2.setFillStyle('blue')
        poly2.enableKinematics()
        poly2.setTorque(.003)
        poly2.setVel(-0.4,0)  
        poly2.setStrokeStyle('#000')
    io.addObj(poly2)
    io.addToGroup('g1',poly2)

    io.setCollisionCallback('g1', function(obj1, obj2){
        obj1.setTorque(0)
        obj1.setVel(0,0) 
        obj2.setTorque(0)
        obj2.setVel(0,0) 
    });
 io.setFramerate(60);

}; iio.start(CollidingPoly);
sbiermanlytle commented 11 years ago

Just resolved this, download the new source from Github. Here's a demo:

CollidingBoxes = function(io){

  createBox = function(xPos, xVel, fillStyle){
    return io.addToGroup('boxes'
      ,new iio.ioRect(xPos,io.canvas.height/2,300)
      .setFillStyle(fillStyle)
      .enableKinematics()
      .setVel(xVel,xVel)
      //.rotate(Math.PI/4)
      .setTorque(.01)
      .setBound('left', 0
        ,function(obj){
          obj.vel.x = 3;
          return true;
         })
      .setBound('right', io.canvas.width
        ,function(obj){
          obj.vel.x = -3;
          return true;
        })
      .setBound('top', 0
        ,function(obj){
          obj.vel.y = 3;
          return true;
         })
      .setBound('bottom', io.canvas.height
        ,function(obj){
          obj.vel.y = -3;
          return true;
        }));
  }

  var b1 = createBox(300, 3, '#00baff');
  var b2 = createBox(io.canvas.width-300, -3, 'red');

  io.setCollisionCallback('boxes'
    ,function(box1,box2){
       var temp = box1.vel;
       box1.vel = box2.vel;
       box2.vel = temp;
       box1.torque=box2.torque*=-1;
     });

  io.setFramerate(60, function(){
    var a = b1.vel;
    var b = b2.vel;
  });

}; iio.start(CollidingBoxes);