Notice
Recent Posts
Recent Comments
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Today
Total
관리 메뉴

만재송

[COCOS2D-JS] chipmunk 물리엔진 정복(3) - staticBody 본문

프로그래밍/COCOS2D-JS

[COCOS2D-JS] chipmunk 물리엔진 정복(3) - staticBody

만재송 2018. 1. 21. 18:00

staticBody


게임을 구현하다보면 밀어도 밀리지 않는 벽을 생성하고 싶다고 하자. 근데 지금까지 배운방법으로 하면 운동성이 있는 body로 인하여 뒤로 밀릴것이다. 아니면 무게를 무한대로 줘서 움직일 수 없게 해야하는 건가... 그렇다고 body를 없애버리면 shape를 생성하지 못해서 시도도 할 수가 없다. 충돌은 하지만 운동의 기능은 전혀 없는 물체를 만들려면 어떻게 해야할까?


그 방법이 바로 staticBody이다. staticBody는 chipmunk 내부 라이브러리에서 정의한 무게가 무한대인 body이다. 물체끼리 충돌을해도 튕기거나 밀려나가지 않고 그자리에서 가만히 있는다. body가 있지만 body의 기능을 하지않는다고 생각하면 될 것이다.



땅을 생성해보자


전에 구현한 GameScene는 물체가 중력의 영향을 받아 계속 떨어졌다. 이제 staticBody를 사용하여 땅을 만들어서 물체가 영원히 떨어지지 못하게 구현해보자.


addWallsAndGrond 메서드를 생성하여 staticBody를 구현해보자.

addWallsAndGround: function() {
var bottomWall = new cp.SegmentShape(this.space.staticBody, cp.v(0, 0),

cp.v(720, 0), 100);
this.space.addStaticShape(bottomWall);
},

생성하는 방법은 Shape를 생성하는 방법과 같다. 원래 첫번째 인자로 body 가 들어갔지만 우린 body지만 body의 기능을 하지않는 staticBody를 사용 할 것이다. staticBody는 chipmunk 라이브러리에 구현이 되어있어서 Space로 접근하면 바로 사용할 수 있다. 


staticBody 를 담은 Shape를 bottomWall 변수에 넣어 Space 공간에 추가만 해주면된다. 원래 Shape를 add 하기 위해서는 body를 먼저 add 해줘야 하는데 staticBody를 사용하면 addStaticShape를 이용하여 Shape만 달수있다. 


이제 구현한 코드를 실행해보자. 아래에 회색의 긴 땅이 생겼다. 이제 원모양의 물체가 영원히 아래로 떨어지는 일은 없다.



var GameScene = cc.Scene.extend({

ctor: function () {
this._super();

this.initPhysics();
this.initDebugMode();
this.scheduleUpdate();
},

initPhysics: function () {
this.space = new cp.Space();
this.space.iterations = 10;
this.space.gravity = cp.v(0, -800);
this.space.damping = 1;
this.space.collisionSlop = 0.1;

this.addPhysicsCircle();
this.addWallsAndGround();
},

addWallsAndGround: function() {
var bottomWall = new cp.SegmentShape(this.space.staticBody,
cp.v(0, 0), cp.v(720, 0), 100);
this.space.addStaticShape(bottomWall);
},

addPhysicsCircle: function() {
var body = new cp.Body(10, cp.momentForCircle(10, 0, 64, cp.v(0, 0)));
body.setPos(cp.v(360, 720));

var shape = new cp.CircleShape(body, 64, cp.v(0, 0));

this.space.addBody(body);
this.space.addShape(shape);
},

initDebugMode: function() {
var phDebugNode = new cc.PhysicsDebugNode(this.space);
this.addChild(phDebugNode, 10);
},

update: function(dt) {
this.space.step(dt);
}
});




chipmunk 물리엔진 정복 시리즈



Comments