Game Development Community

dev|Pro Game Development Curriculum

Plan: Get this dang collision stuff to work!

by Tom Biagioni · 03/04/2009 (12:25 pm) · 2 comments

Hey guys, first blog, and i hope this is apropriot for a blog. A while back in one of my game math classes i programmedthe collision of balls in OpenGL environment in Visual studio. It was awesome, and pretty realistic. Well a couple months ago i took a couple classes that used TGE, they were fun and i learned a bunch of new stuff. In fact im still reading that huge 3D game prgramming all in one book! I got to thinking about that old simulation i did with the colliding balls. TGB has a build in physics engine using rigid bodies, but its just not realistic enough for what my crazy plan is.

Im trying to make myself a simple Pool game, i thought it would be fun. So i set off in trying to reacreate some good ol fashion collision between two objects from C++, into Torquescript. CRAZYNESS because i barely know torque, and the engine. Ive been scouring the TDN for usefull things, and ive managed to type up some code. Heres what i wrote up:

//============================================================
// Rigid Body Collisions
// By: Tom Biagioni
// Copyright (C) 2009
//============================================================

if (!isObject(BallBounceBehavior))
{
  %template = new BehaviorTemplate(BallBounceBehavior);

  %template.friendlyName = "Ball Bounce";
  %template.behaviorType = "Physics";
  %template.description = "balls that collide bounce off each other";

}

function BallBounceBehavior::onAddToScene(%this, %scenegraph)
  {
     %this.owner.setCollisionResponse("RIGID");
  }  

  function BallBounceBehavior::findBounce(%this)    
  {
  
  //Initialize variables

  %x1 = %dstObj.getPositionX();    //object coordinates
  %y1 = %dstObj.getPositionY();
  %x2 = %srcObj.getPositionX();
  %y2 = %srcObj.getPositionY();

  %r1 = 2.25;
  %r2 = 2.25;

  %dx = %x2 - %x1;
  %dy = %y2 - %y1;

  %d = mSqrt(%dx*%dx + %dy*%dy);   //distance

  while (%d <= %r1 +%r2)

  {
    %vx1 = %dstObj.getLinearVelocityX();
    %vy1 = %dstObj.getLinearVelocityY();
    %vx2 = %srcObj.getLinearVelocityX();
    %vy2 = %srcObj.getLinearVelocityY();

    %mass1 = 0.15;
    %mass2 = 0.15;

    //velocity in the direction of (dx, dy)
    
    %vp1 = (%vx1*dx + %vy1*%dy) / %d;
    %vp2 = (%vx2*dx + %vy2*%dy) / %d;
 
    //collision should of happened dt before

    %dt = (%r1 + %r2 - %d) / (%vp1 - %vp2);

    //move the circles backward in time
    %x1 -= %vx1 * %dt;
    %y1 -= %vy1 * %dt;
    %x2 -= %vx2 * %dt;
    %y2 -= %vy2 * %dt;

    //new collision calculations at impact

    %dx = %x2 - %x1;
    %dy = %y2 - %y1;
    %d = mSqrt(%dx*%dx + %dy*%dy);   //distance

    //unit vector is direction of collision
    %ax = %dx/%d;
    %ay = %dy/%d;

    //projection of the velocites int he axis
 
    %va1 = %vx1*%ax + %vy1*%ay;
    %vb1 = -%vx1*%ay + %vy1*%ax;
    %va2 = %vx2*%ax + %vy2*%ay;
    %vb2 = -%vx2*%ay + %vy2*%ax;

    //calculate new velocity after collision

    %ed = 1;
    %vaP1 = %va1 + (1 + %ed) * (%va2 - %va1) / (1 + %mass1/%mass2);
    %vaP2 = %va2 + (1 + %ed) * (%va1 - %va2) / (1 + %mass2/%mass1);
    
    //undo projections

    %vx += %vx1 * %dt;
    %y1 += %vy1 * %dt;
    %x2 += %vx2 * %dt;
    %y2 += %vy2 * %dt;

    //Update positions and velocities

    %dstObj.setPositionX(%x1);
    %dstObj.setPositionY(%y1);
    %srcObj.setPositionX(%x2);
    %srcObj.setPositionY(%y2);

    %dstObj.setLinearVelocityX(%vx1);
    %dstObj.setLinearVelocityY(%vy1);
    %srcObj.setLinearVelocityX(%vx2);
    %srcObj.setLinearVelocityY(%vy2);
  }
}

  function BallBounceBehavior::onCollision(%srcObj, %dstObj, %srcRef,   %dstRef, %time, %normal,%contactCount, %contacts)
  {
    %srcObj.findBounce();
    %dstObj.findBounce();

  }
}

Doesnt work for some reason, i need to find a way to reference the objects.

About the author

Recent Blogs


#1
03/06/2009 (9:26 am)
Not having looked at TGB for a while, I'm a bit fuzzy, but as I recall it already has 'rigid' collision mode. Something like t2dSceneObject.setResponseMode/setCollisionResponse( "RIGID" );
#2
03/06/2009 (9:33 am)
yeah i was initially trying to make my own code for collision for the practice, and to see if it would actually work. However it looks to me now, the only way to do this is to edit the source :P

PS

i edited my post, i changed the code a bit. Still cant get it to work though :(