HTML and CSS Reference
In-Depth Information
fixDef.density = 0.6;
fixDef.friction = 0.8;
fixDef.restitution = 0.1;
bodyDef.type = b2Body.b2_staticBody;
bodyDef.position.x = positionX/pxPerMeter;
bodyDef.position.y = positionY/pxPerMeter;
fixDef.shape = new b2CircleShape(radius/pxPerMeter);
this.ball = this.world.CreateBody(bodyDef);
this.ball.CreateFixture(fixDef);
};
3. We will need to get the ball posiion to calculate the throwing angle. We deine the
following method to get the ball posiion and convert it into screen coordinates:
physics.ballPosition = function(){
var pos = this.ball.GetPosition();
return {
x: pos.x * pxPerMeter,
y: pos.y * pxPerMeter
};
};
4. By using the cursor and ball posiion, we can calculate the angle. This is the Math
funcion that returns the angle, which will be explained later:
physics.launchAngle = function(stageX, stageY) {
var ballPos = this.ballPosition();
var diffX = stageX - ballPos.x;
var diffY = stageY - ballPos.y;
// Quadrant
var degreeAddition = 0; // Quadrant I
if (diffX < 0 && diffY > 0) {
degreeAddition = Math.PI; // Quadrant II
} else if (diffX < 0 && diffY < 0) {
degreeAddition = Math.PI; // Quadrant III
} else if (diffX > 0 && diffY < 0) {
degreeAddition = Math.PI * 2; // Quadrant IV
}
 
Search WWH ::




Custom Search