[Bullet Physics] RigidBody 만들기

물리 2012. 1. 5. 00:01 Posted by 알 수 없는 사용자

안녕하세요 물리프로그래머 조성현 입니다.
모든 분들 새해 복 많이 받으시고요, 오늘은 가장 간단한 RigidBody(박스, 구, 캡슐, 실린더)를 만들어 보겠습니다.


1) Box생성
크기가 (1, 1, 1)의 박스를 위치(-4, 0, 10)에 생성하는 코드 입니다.
내용은 아래와 같습니다.
(1) 위치를 (-4, 0 , 10)로 정한다
(2) (1, 1, 1)크기의 btBoxShape를 생성한다.
(3) 박스 모양와 질량으로 localInertia를 구한다.(Inertia에 대해선 추후 설명)
(4) 위의 요소들을 이용해서 RigidBody를 만든다.
(5) World에 등록한다.

  // 질량을 1로 설정
  const float mass = 1.0f;

  // (1) RidigBody를 (-4, 0, 10)에 생성
  btTransform bodyTM;
  bodyTM.setIdentity();
  bodyTM.setOrigin(btVector3(-4.0f, 0.0f, 10.0f));

  // (2)Box 생성
  btCollisionShape* boxShape = new btBoxShape(btVector3(1.0f, 1.0f, 1.0f) );

  // (3)IneritaTensor를 계산
  btVector3 localInertia(0,0,0);
  boxShape->calculateLocalInertia(mass, localInertia);

  // (4)RigidBody 생성
  btDefaultMotionState* myMotionState = new btDefaultMotionState(bodyTM);
  btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, boxShape, localInertia);
  btRigidBody* body = new btRigidBody(rbInfo);

  // (5)World에 등록
  m_dynamicsWorld->addRigidBody(body);



2) Sphere생성
Radius가 1인 Sphere를 위치(-2, 0, 10)에 생성하는 코드 입니다.
내용은 Box생성과 동일합니다.

  //질량을 1로 설정
  const float mass = 1.0f;

  //(1) RidigBody를 (-2, 0, 10)에 생성
  btTransform bodyTM;
  bodyTM.setIdentity();
  bodyTM.setOrigin(btVector3(-2.0f, 0.0f, 10.0f));

  // (2) Sphere 생성
  btCollisionShape* sphereShape = new btSphereShape(1.0f);

  // (3) IneritaTensor를 계산
  btVector3 localInertia(0,0,0);
  sphereShape->calculateLocalInertia(mass, localInertia);

  // (4)RigidBody 생성
  btDefaultMotionState* myMotionState = new btDefaultMotionState(bodyTM);
  btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, sphereShape, localInertia);
  btRigidBody* body = new btRigidBody(rbInfo);

  (5)  World에 등록
  m_dynamicsWorld->addRigidBody(body);



3) Capsule생성
Radius가 0.5, Height가 2인 Capsule을 위치(2, 0, 10)에 생성하는 코드 입니다.

  //질량을 1로 설정
  const float mass = 1.0f;

  //(1) RidigBody를 (2, 0, 10)에 생성
  btTransform bodyTM;
  bodyTM.setIdentity();
  bodyTM.setOrigin(btVector3(2.0f, 0.0f, 10.0f));

  //(2) Capsule 생성
  btCollisionShape* capsuleShape = new btCapsuleShapeZ(0.5f, 2.0f);

  //(3) IneritaTensor를 계산
  btVector3 localInertia(0,0,0);
  capsuleShape->calculateLocalInertia(mass, localInertia);

  //(4) RigidBody를 생성
  btDefaultMotionState* myMotionState = new btDefaultMotionState(bodyTM);
  btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, capsuleShape, localInertia);
  btRigidBody* body = new btRigidBody(rbInfo);

  //(5) World에 등록
  m_dynamicsWorld->addRigidBody(body);

4) Cylinder생성
Radius가 0.5, Height(Half)가 1인 Cylinder를 위치(4, 0, 10)에 생성하는 코드 입니다.

  //질량을 1로 설정

  const float mass = 1.0f;

  //(1) RidigBody를 (0, 0, 10)에 생성
  btTransform bodyTM;
  bodyTM.setIdentity();
  bodyTM.setOrigin(btVector3(4.0f, 0.0f, 10.0f));

  //(2) Cylinder 생성                                                                
  btCollisionShape* cylinderShape = new btCylinderShapeZ(btVector3(0.5f, 0.5f, 1.0f));

  //(3) IneritaTensor를 계산
  btVector3 localInertia(0,0,0);
  cylinderShape->calculateLocalInertia(mass, localInertia);

  //(4) RigidBody를 생성
  btDefaultMotionState* myMotionState = new btDefaultMotionState(bodyTM);
  btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, cylinderShape, localInertia);
  btRigidBody* body = new btRigidBody(rbInfo);

  //(5) World에 등록
  m_dynamicsWorld->addRigidBody(body);


 

5) 결과
(1) "Create" 버튼을 누르면 RigidBody가 생성됨(계속)
(2) "Release"버튼을 누르면 모두 삭제됨.
(3) "Alt"를 누른상태에서, 마우스 L, R, Wheel로 카메라를 조절함.


다음에는 Friction, Restitution, Damping과 같은 RigidBody의 속성에 대해 설명하겠습니다. 

  

'물리' 카테고리의 다른 글

Inertia Tensor(2)  (0) 2012.02.19
Inertia Tensor(1)  (0) 2012.02.13
RigidBody의 Restitution, Friction, Damping  (2) 2012.02.06
[Bullet Physics] Bullet 물리엔진의 설치  (2) 2011.12.22