본문 바로가기
Three.js

Three.js-collision and bullet engine, ammo -part4

by ses jeon 2019. 10. 22.

https://xoppa.github.io/blog/using-the-libgdx-3d-physics-bullet-wrapper-part1/

 

Using the libGDX 3D physics Bullet wrapper - part1 | blog.xoppa.com

Many 3D games require some sort of collision detection between 3D objects. Sometimes it's possible to do this with some basic math and bounding boxes and...

xoppa.github.io

If you have seen the above, you have probably seen the world settings for collision world and dynamic world.

Ammo can only deal with the dynamic world by default. (You should look at the ammo site for non-implementation)

We'll create another object and bind a world implementation like Ammo.btDiscreteDynamicsWorld.

위의 내용을 봤다면, collision world와 dynamic world의 world setting을 보셨을 겁니다.

 

Ammo는 기본적으로 dynamic world만 다룰수 있습니다. ( 이외의 구현에 대해서는 ammo 사이트를 봐야합니다 )

 

Ammo.btDiscreteDynamicsWorld 같은 월드 구현을 또다른 object를 만들어서 묶어보도록 하겠습니다.

basicStructure5.zip
4.95MB

If you have seen the example that used the motion state of the dynamic world implementation, let's add a class that automatically configures the content.

dynamic world 구현의 motion state 까지 사용한 예제를 보았다면, 그까지 내용을 자동설정하는 class를 하나 추가하겠습니다.

Name it Randomly Dynamics_level0. Number 1 is the default setting for bullet engines. I will see it when I need it and just use it.

In fact, the dynamic world points to this.dynmicsWorld. Number 2 is gravity setting.

Number 3 is an array with rigid objects added differently from the java wrapper example. In the case of the Java wrapper of the bullet engine, the rigid object's movement is automatically determined when the motion state changes, but ammo must be changed again. This is different from the bullet engine java wrapper.

Number 4 is a public variable that can be used to change the movement of an object.

Let the sphere move in the sphere and cube, and think of the cube as the ground.

If so, go to PrimitiveModel and change the mass value. If you are not moving like the ground, you should put the mass at zero.

이름은 임의로 Dynamics_level0 라고 놓았습니다. 1번은 불릿엔진의 기본 설정입니다. 필요할때 보기로 하고 그냥 그대로 쓰기로 하겠습니다.

 

실제로 다이나믹월드는 this.dynmicsWorld를 가리킵니다. 2번은 중력설정을 해주고 있습니다.

 

3번은 java wrapper 예제와 다르게 추가되는 rigid 객체를 가지고 있는 배열입니다. 불릿 엔진의 java wrapper 일 경우는 motion state가 변경될때 자동으로 rigid 객체의 움직임이 결정되지만, ammo의 경우는 따로 다시 변경을 해주어야 합니다. 이점은 bullet engine java wrapper와 다르다고 할수 있습니다.

 

4번은 객체의 움직임 변경에 여러모로 쓰일 공용변수입니다. 

 

구체와 육면체중에 구는 움직이도록하고, 육면체를 지면처럼 생각해보겠습니다.

 

그렇게 설정을 했다면 PrimitiveModel로 가서 질량값을 변경합니다. 지면처럼 움직이는게 아니라면 질량을 0으로 놓아야합니다.

Now put the created object into the dynamic world and specify the initial location.

(Before, let's define a separate function to exchange variables between bullet engine and three.js, such as bullet2three. We defined MathUtils.js separately and added it to index.html. js has been added, please check the source.)

And let's set a new location.

자 이제 만들어진 객체를 dynamic world에 넣고, 초기 위치를 지정하도록한다.

 

(그전에 bullet2three와 같이 불릿엔진과 three.js 사이의 변수 교환을 하는 함수를 따로 정의해서 쓰도록 하겠습니다. MathUtils.js를 따로 정의해서 index.html에 추가하였습니다. 그리고 좀더 필요로 하는 수학 함수를 amendedThree.js 에 추가하였습니다. 소스를 확인해주세요.)

 

그리고 새로 위치를 정해보자.

A matrixAutoUpdate of 1 means to manipulate the object's matrix directly. The initial value is true, in this case sphere.position.y = 5; You can only operate indirectly, such as I'm trying to move the matrix directly. So change it to false.
(see the documentation for three js)

Number 2 did a new matrix position like that. 3 copied the matrix to bounding_line.
4 sets the position to collision_body.

1의 matrixAutoUpdate는 객체의 matrix를 직접 조작하겠다는 뜻입니다. 초기값은 true이고 이 경우는 sphere.position.y = 5; 와 같은 간접식으로만 조작이 가능합니다. 저는 matrix를 직접 움직이려고 합니다. 그래서 false로 바꿔줍니다.

(three js 의 도큐먼트를 참조하십시오)

 

2번은 그렇게 새로 매트릭스 위치를 조정했습니다. 3번은 그 매트릭스를 bounding_line에 복사해주었습니다.

4번은 그 위치를 collision_body에 세팅했습니다. 

 

 

It will be in this state when you do so.

Now let's activate the dynamic world and drop the sphere.

그렇게 했을때 이런 상태가 됩니다.

 

이제 dynamic world를 활성화시키고 구체를 떨어트리도록 하겠습니다.

Number one is to enable dynamicsWorld. Here we only change the motion state of the rigid objects added to the world. If bullet engine java wrapper is done here, we need to do extra work in javascript.
If there are 2 motionstates, change the state accordingly.
Number 3 updates the rotation, position and size respectively. Option 4 is an option, but since we've included bounding_line now, change the bounding_line.

1번은 dynamicsWorld를 활성화시키는것입니다. 여기서는 월드에 추가된 rigid 객체들의 motion state만 변경시킵니다. 만약 bullet engine java wrapper 경우는 여기서 작업이 끝이 나는데, javascript에서는 추가 작업을 해야됩니다.

2번의 motionstate가 있다면 그에 따라 상태를 변경 시킵니다.

3번은 회전, 위치, 크기를 각각 업데이트 시킵니다. 4번은 옵션이라 할수 있는데, 지금 bounding_line을 포함시켰기 때문에 bounding_line을 변경시킵니다.

 

And if you add the following to your render you'll see the falling sphere:

그리고 render에 다음을 추가시키면 떨어지는 구체를 볼수 있습니다.

 

 

 

 

댓글