상세 컨텐츠

본문 제목

Capsule class

three.js

by 폴리프레임 2024. 11. 21. 17:49

본문

반응형

Capsule은 Three.js 라이브러리의 클래스입니다. 캡슐 형태의 기하학적 구조를 정의하고, 물리적 시뮬레이션이나 충돌 검출 등의 다양한 용도로 사용할 수 있습니다.

 

  • 기하학적 정의: Capsule 클래스는 두 반구 사이에 실린더를 연결한 형태로, 3D 공간에서 캡슐 형태의 기하학적 구조를 생성합니다.
  • 물리적 시뮬레이션: 물리 엔진에서 사용하여 캡슐 형태의 객체 간의 충돌을 검출하거나, 물리적 상호작용을 시뮬레이션할 수 있습니다.
  • 충돌 검출: 게임 개발이나 시뮬레이션에서 캐릭터의 충돌 영역을 정의하는 데 유용합니다. 캡슐 형태는 특히 캐릭터의 몸체를 모델링하는 데 자주 사용됩니다.
  • 경로 찾기 및 충돌 회피: 캐릭터가 장애물을 피하며 이동할 때 유용하게 사용됩니다.
import { Capsule } from 'three/addons/math/Capsule.js';

// 시작과 끝 점 정의
const start = new THREE.Vector3(0, 0, 0);
const end = new THREE.Vector3(0, 1, 0);

// 반지름 설정
const radius = 0.5;

// 캡슐 객체 생성
const capsule = new Capsule(start, end, radius);

// 캡슐을 씬에 추가 (물리엔진 사용 시 필요에 따라 처리)
scene.add(capsule.mesh);

 

 

import * as THREE from 'three';
import { Capsule } from 'three/addons/math/Capsule.js';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;

const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 캡슐 생성
const start = new THREE.Vector3(0, 0, 0);
const end = new THREE.Vector3(0, 1, 0);
const radius = 0.5;
const capsule = new Capsule(start, end, radius);

// 캡슐을 Mesh로 변환하여 씬에 추가
const capsuleGeometry = new THREE.CapsuleGeometry(radius, end.y - start.y - 2 * radius, 4, 8);
const capsuleMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const capsuleMesh = new THREE.Mesh(capsuleGeometry, capsuleMaterial);
scene.add(capsuleMesh);

const animate = function () {
  requestAnimationFrame(animate);

  capsuleMesh.rotation.x += 0.01;
  capsuleMesh.rotation.y += 0.01;

  renderer.render(scene, camera);
};

animate();

 

'three.js' 카테고리의 다른 글

resize - Three.js  (0) 2024.11.22
lookAt(), getWorldDirection()  (1) 2024.11.21
Octree  (0) 2024.11.21
WebGLRenderer(), CSS3DRenderer()  (0) 2024.11.21
camera.lookAt()  (1) 2024.11.21

관련글 더보기