Professional Documents
Culture Documents
Fix (1) (1)
Fix (1) (1)
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Interactive Box2D Demo</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/box2dweb/2.1.a.3/Box2dWeb-
2.1.a.3.min.js"></script>
');
alert('Box2D 라이브러리를 불러오는 데 실패했습니다. 인터넷 연결을 확인해주세요.');
return false;
}Dhdkahucjenskhelloxkrmsmekzndkekxnd
return true;
}
document.addEventListener('DOMContentLoaded', function () {
if (!initBox2D()) return;
function createWorld() {
// 월드 생성
world = new b2World(new b2Vec2(0,
parseFloat(document.getElementById('gravity').value)), true);
// 그라운드 생성
const groundBodyDef = new b2BodyDef();
groundBodyDef.position.Set(canvasWidth / 2, 0.5);
// 원 생성
const circleBodyDef = new b2BodyDef();
circleBodyDef.type = b2Body.b2_dynamicBody;
circleBodyDef.position.Set(canvasWidth / 2, canvasHeight - 1);
circleBody = world.CreateBody(circleBodyDef);
const circleShape = new b2CircleShape();
function updatePhysics() {
world.SetGravity(new b2Vec2(0,
parseFloat(document.getElementById('gravity').value)));
circleFixture.SetDensity(parseFloat(document.getElementById('density').value));
circleFixture.SetFriction(parseFloat(document.getElementById('friction').value));
circleFixture.SetRestitution(parseFloat(document.getElementById('restitution').valu
e));
circleBody.ResetMassData();
}
function draw() {
world.Step(1 / 60, 8, 3);
world.ClearForces();
// 그라운드 그리기
context.fillStyle = "brown";
context.fillRect(0, canvas.height - 0.5 * SCALE, canvas.width, 0.5
* SCALE);
// 원 그리기
const position = circleBody.GetPosition();
context.save();
context.translate(position.x * SCALE, canvas.height - position.y *
SCALE);
context.fillStyle = "red";
context.beginPath();
context.arc(0, 0, SCALE, 0, Math.PI * 2, true);
context.fill();
context.restore();
requestAnimationFrame(draw);
}
createWorld();
draw();