HTML and CSS Reference
In-Depth Information
var theta = Math.atan(diffY / diffX) + degreeAddition;
return theta;
};
5. We have prepared the Math methods and can finally throw the ball with the
following method:
physics.shootBall = function(stageX, stageY, ticksDiff) {
this.ball.SetType(b2Body.b2_dynamicBody);
var theta = this.launchAngle(stageX, stageY);
var r = Math.log(ticksDiff) * 50; // power
var resultX = r * Math.cos(theta);
var resultY = r * Math.sin(theta);
this.ball.ApplyTorque(30); // rotate it
// shoot the ball
this.ball.ApplyImpulse(new b2Vec2(resultX/pxPerMeter, resultY/
pxPerMeter), this.ball.GetWorldCenter());
this.ball = undefined;
};
6. We need a target for the throwing acion. So, we create the hoop with the following
code. A hoop is constructed using a board and two squares:
physics.createHoop = function() {
var hoopX = 50;
var hoopY = 100;
var bodyDef = new b2BodyDef;
var fixDef = new b2FixtureDef;
// default fixture
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = 0.2;
// hoop
bodyDef.type = b2Body.b2_staticBody;
bodyDef.position.x = hoopX/pxPerMeter;
bodyDef.position.y = hoopY/pxPerMeter;
bodyDef.angle = 0;
 
Search WWH ::




Custom Search