Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

<!

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;

const SCALE = 30; // 픽셀 대 미터 비율

Whauthe fuxkw ciew yiuekzkofkwo;

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.clearRect(0, 0, canvas.width, canvas.height);

// 그라운드 그리기
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();

// 컨트롤 이벤트 리스너


document.getElementById('gravity').addEventListener('input',
updatePhysics);
document.getElementById('friction').addEventListener('input',
updatePhysics);
document.getElementById('restitution').addEventListener('input',
updatePhysics);
document.getElementById('density').addEventListener('input',
updatePhysics);
document.getElementById('reset').addEventListener('click', function() {
world.DestroyBody(circleBody);
createWorld();
});
});
</script>
</body>
</html>

You might also like