post list

2014년 8월 31일 일요일

SKNode글 추가 SKPhysicsBody 에 대해서


노드는 옵션으로 물리 몸체(physics body)가질 수 있습니다.

일단  skphysicsbody reference 입니다. 참고 하시고

Tasks


Creating Volume-based Physics Bodies


Creating Edge-based Physics Bodies


Defining How Forces Affect a Physics Body


Defining a Body’s Physical Properties


Working with Collisions and Contacts


Applying Forces and Impulses to a Physics Body


Inspecting the Physics Body’s Position and Velocity


Reading the Physics Body’s Node

  •    node  property

Determining Which Joints Are Connected to a Physics Body


기본적으로 물리몸체는 body를 할당하는 걸로 시작합니다. 

노드 객체가 생성된 상태에서 

클래스 메소드인  


요따위 것들로  바디를 할당합니다.  

여기서 다른건 사용하기 쉬운데 제일 복잡하면서 효율적인 path를 사용하는 
법을 좀 알보겠습니다. 

CGFloat offsetX = self.crocodile.frame.size.width * self.crocodile.anchorPoint.x;
CGFloat offsetY = self.crocodile.frame.size.height * self.crocodile.anchorPoint.y;
 
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 47 - offsetX, 77 - offsetY);
CGPathAddLineToPoint(path, NULL, 5 - offsetX, 51 - offsetY);
CGPathAddLineToPoint(path, NULL, 7 - offsetX, 2 - offsetY);
CGPathAddLineToPoint(path, NULL, 78 - offsetX, 2 - offsetY);
CGPathAddLineToPoint(path, NULL, 102 - offsetX, 21 - offsetY);
 
CGPathCloseSubpath(path);
 
self.crocodile.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];

[self.crocodile skt_attachDebugFrameFromPath:path color:[SKColor redColor]];
CGPathRelease(path);
출처 :http://www.raywenderlich.com/

tc_cut_the-rope_debug
출처 :http://www.raywenderlich.com/

이렇게 빨간 색으로 그려진 것 처럼 path를 이용해서 물리몸체를 할당 할수 있습니다. 
원래 빨간 색선은 나타나지 않으나  
위에 처럼 빨간선을 보기 위해선 
[self.crocodile skt_attachDebugFrameFromPath:path color:[SKColor redColor]]; 
이게 필요합니다. 
먼저 라이브러리를 추가 해야 하구요 

이건 사용법과 설명

Note: You may have noticed a call to skt_attachDebugFrameFromPath: for most of the physics bodies. This is a method from SKNode+SKTDebugDraw, which is part of a group of Sprite Kit utilities developed by Razeware. This particular method helps with debugging physics bodies. To turn it on, openSKNode+SKTDebugDraw.m and change the line BOOL SKTDebugDrawEnabled = NO; to BOOL SKTDebugDrawEnabled = YES;. This will draw a shape that represents your physics body. Don’t forget to turn it off when you’re done!

라이브러리 파일

body를 할당하면 자동으로 중력의 영향을 받기 시작합니다. 

중력 크기 바꾸기 
scene.physicsworld.gravity = CGVectorMake( 0.0 , -9.8);

중력 영향 안 받기 
프로퍼티 affectedByGravity 를 바꿉니다.
node.physicsworld.affectedByGravity = NO;
이렇게 

물리 시뮬레이션 끄기 
프로퍼티 dynamic 값을 no로 설정 

무게 설정
프로퍼티 mass 값 설정 (단위 ,kg)


스프라이트 킷 렌더링 루프 


따라서,
지속적으로 확인 해야 하고  변화를 주어야 하는  코드는 
위의 세개의 메소드에 적어주면 됩니다. 

저 세개의 메소드는  계속 반복하며 돌아 갑니다. 


physicsbody의 핵심은 아마도 충돌과 접촉 이겠죠 
할 이야기가 많아서 다음글에......






댓글 없음:

댓글 쓰기