Table Air Hockey in Box2DAS3

http://tim.kerchmar.com/prototypes/hockey.swf

The above is the efforts of 2 days of work so far (previous week spent investigating networking and getting Red5 installed). I'm currently unemployed and looking for part time contract work using Flash or Unity.

Can anyone explain why the puck doesn't always bounce realistically, but instead will sometimes slide instead of bounce (particularly when bouncing along the Y axis (down the table and back to player))?
Code:
import Box2D.Dynamics.*
import Box2D.Collision.*
import Box2D.Collision.Shapes.*
import Box2D.Dynamics.Joints.*
import Box2D.Dynamics.Contacts.*
import Box2D.Common.Math.*
import flash.events.Event;
import flash.display.*;
import flash.text.*;
import General.*


var m_world:b2World;
var m_iterations:int = 10;
var m_timeStep:Number = 1/30;
// Sprite to draw in to
var m_sprite:Sprite;
var m_input:Input;

addEventListener(Event.ENTER_FRAME, update, false, 0, true);
m_sprite = new Sprite();
addChild(m_sprite);
m_input = new Input(m_sprite);

var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-1000.0, -1000.0);
worldAABB.upperBound.Set(1000.0, 1000.0);

// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0.0, 0.0);

// Allow bodies to sleep
var doSleep:Boolean = true;

// Construct a world object
m_world = new b2World(worldAABB, gravity, doSleep);
// set debug draw
var dbgDraw:b2TransformCallbackDebugDraw = new b2TransformCallbackDebugDraw();
//var dbgSprite:Sprite = new Sprite();
//m_sprite.addChild(dbgSprite);
dbgDraw.m_sprite = m_sprite;
dbgDraw.m_drawScale = 1.0;//160.0;
dbgDraw.m_fillAlpha = 0.3;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
dbgDraw.m_transformCallback = transformPoint;
dbgDraw.m_xformScale = 0.1;
m_world.SetDebugDraw(dbgDraw);


// Create border of boxes
var wallSd:b2PolygonDef = new b2PolygonDef();
var wallBd:b2BodyDef = new b2BodyDef();
var wallB:b2Body;

// 2.1M height, 1.2M width
var dimensions:Point = new Point(1.2, 2.1);
var wallThickness:Number = 0.1;

// Left
wallBd.position.Set(-wallThickness, dimensions.y * 0.5);
wallSd.SetAsBox(wallThickness, dimensions.y * 0.5 + wallThickness);
wallSd.friction = 0.0;
wallSd.restitution = 0.9;
wallB = m_world.CreateBody(wallBd);
wallB.CreateShape(wallSd);
wallB.SetMassFromShapes();
// Right
wallBd.position.Set(dimensions.x + wallThickness, dimensions.y * 0.5);
wallB = m_world.CreateBody(wallBd);
wallB.CreateShape(wallSd);
wallB.SetMassFromShapes();
// Top
wallBd.position.Set(dimensions.x * 0.5, -wallThickness);
wallSd.SetAsBox(dimensions.x * 0.5 + wallThickness, wallThickness);
wallB = m_world.CreateBody(wallBd);
wallB.CreateShape(wallSd);
wallB.SetMassFromShapes();
// Bottom
wallBd.position.Set(dimensions.x * 0.5, dimensions.y + wallThickness);
wallB = m_world.CreateBody(wallBd);
wallB.CreateShape(wallSd);
wallB.SetMassFromShapes();

function transformPoint(pos:b2Vec2):b2Vec2 {
   var dist:Number = Math.sqrt(
      (1.0 + dimensions.y - pos.y) *
      (1.0 + dimensions.y - pos.y) + 1.0);
   
   return new b2Vec2(
      300.0 + (pos.x - dimensions.x * 0.5) * (60.0 * pos.y + 240.0),
      707.0 / dist - 150.0);
}

function copyPhysicsToMovieclip(body:b2Body, mc:MovieClip, scaleAdjust:Number):void {
   var pos:b2Vec2 = body.GetPosition();
   // the assumption here is that the eye is (1m,1m) away
   var dist:Number = Math.sqrt(
      (1.0 + dimensions.y - pos.y) *
      (1.0 + dimensions.y - pos.y) + 1.0);
   // 300.0 is half the width
   
   var transformedPoint:b2Vec2 = transformPoint(pos);
   mc.x = transformedPoint.x;
   mc.y = transformedPoint.y;
   mc.scaleX = scaleAdjust / dist;
   mc.scaleY = mc.scaleX;
}
      
function update(e:Event):void{
   Input.update();
   
   // Update physics
   var physStart:uint = getTimer();
   m_world.Step(m_timeStep, m_iterations);
   //Main.m_fpsCounter.updatePhys(physStart);
   
   var p2:b2Vec2 = new b2Vec2(Input.mouseX * 1.2 / 600.0,
                        Input.mouseY * 2.4 / 400.0);
   m_mouseJoint.SetTarget(p2);
   
   copyPhysicsToMovieclip(m_paddle1Body, paddle1, 0.4);
   copyPhysicsToMovieclip(m_puckBody, puck, 3.0);
}

// Create Paddle1's physics
var bodyDefC:b2BodyDef = new b2BodyDef();
bodyDefC.massData.mass = 1.0;
bodyDefC.position.Set(dimensions.x * 0.5, dimensions.y * 0.75);
bodyDefC.angle = 0.0;
bodyDefC.allowSleep = false;
bodyDefC.fixedRotation = true;

var circDef:b2CircleDef = new b2CircleDef();
circDef.radius = 0.09;
circDef.friction = 0.0;
circDef.restitution = 0.3;

var m_paddle1Body:b2Body = m_world.CreateBody(bodyDefC);
m_paddle1Body.CreateShape(circDef);

// Link mouse to Paddle1 with a b2MouseJoint
var m_mouseJoint:b2MouseJoint;
var md:b2MouseJointDef = new b2MouseJointDef();
md.body1 = m_world.GetGroundBody();
md.body2 = m_paddle1Body;
md.target.Set(dimensions.x * 0.5, dimensions.y * 0.75);
md.maxForce = 300.0 * md.body2.GetMass();
md.timeStep = m_timeStep;
m_mouseJoint = m_world.CreateJoint(md) as b2MouseJoint;

// Create Puck physics
bodyDefC.massData.mass = 0.2;
bodyDefC.position.Set(dimensions.x * 0.5, dimensions.y * 0.5);

circDef.radius = 0.1;
circDef.friction = 0.0;
circDef.restitution = 0.9;

var m_puckBody:b2Body = m_world.CreateBody(bodyDefC);
m_puckBody.CreateShape(circDef);
This is done for very slight collisions to stop things bouncing permanently. It shouldn't happen for the speeds you are using, so you are probably not scaling your objects. Either scale them so each puck is about 1 unit, or adjust the appropriate value in b2Settings.
I see you're using this method to put the game into a 3d perspective:

Code:
var dbgDraw:b2TransformCallbackDebugDraw = new b2TransformCallbackDebugDraw()
...
dbgDraw.m_transformCallback = transformPoint;
...
function transformPoint(pos:b2Vec2):b2Vec2 {
   var dist:Number = Math.sqrt(
      (1.0 + gameWidth - pos.y) *
      (1.0 + gameWidth - pos.y) + 1.0);
   
   return new b2Vec2(
      300.0 + (pos.x - gameHeight * 0.5) * (60.0 * pos.y + 240.0),
      707.0 / dist - 150.0);
}

In the above gameWidth = 1.2, gameHeight =2.1, which is the size of the table top in box2d world.
The whole flash stage is 600x400.

Can somebody explain the formula being used in transformPoint, or post a link to where i can learn more?

I'm currently building a similar game, and was originally planning on using flash 10s 3d rotation to create the 3d perspective, but It seems using the formula directly will be much more efficient.

Thanks!
The formula is a more specific subset of the more general perspective projection matrix, I just fiddled with the numbers to get what I wanted.
Hey I was wondering how things actually turned out with this project?

I have been working on something similar for the past 3 weeks and would love to see a live example of a finished game.




Web designer by profession. Check out my work at air hockey tables
RyanJacobs wrote:
Hey I was wondering how things actually turned out with this project?

I have been working on something similar for the past 3 weeks and would love to see a live example of a finished game.




Web designer by profession. Check out my work at air hockey tables


I have visited your site, your work is really amazing.

Original thread: https://box2d.org/forum/viewtopic.php?t=3451