简体   繁体   English

b2身体问题

[英]b2Body questions

I have 2 questions with b2Body: 我对b2Body有2个问题:

  1. What is the difference between b2Body and b2BodyDef? b2Body和b2BodyDef有什么区别?
  2. How would I add a b2Body to a CCScene with coordinates from a CGRect which I already have coded? 如何使用我已经编码的CGRect中的坐标将b2Body添加到CCScene? Also how would I add userData to it so I can keep a reference to this? 另外,我将如何向其中添加userData以便保留对此的引用?

Thanks! 谢谢!

A b2BodyDef is used to define information about a body as a whole, such as position and rotation. b2BodyDef用于定义有关整体物体的信息,例如位置和旋转。 Compared to the other information you require for a b2Body , such as friction and resititution , that is defined on a per fixture basis using b2Fixtures . b2Body所需的其他信息相比,例如摩擦复位 ,这些信息是使用b2Fixtures在每个夹具的基础上b2Fixtures The b2Body is an amalgamation of a body definition and at least one fixture. b2Body是身体定义和至少一个固定装置的合并。

With regard to creating the body from a predefined rect, I'd advise using setAsBox: assuming you are using a b2PolygonShape . 关于从预定义矩形创建主体,我建议使用setAsBox:假设您正在使用b2PolygonShape

The way I usually accomplish the joining of the two is to create a class called BodyNode which has ivars of a b2Body and a CCSprite . 我通常将两者结合BodyNode的方式是创建一个名为BodyNode的类,该类具有b2BodyCCSprite Assign either the BodyNode , ie self or the sprite as the userData and update them as follows: BodyNode (即self或sprite)分配为userData ,并按以下方式更新它们:

-(void) onEnter
{
    [self scheduleUpdate];
    [super onEnter];
}

-(void) update:(ccTime) dt
{
    //Update the position of the sprite to the position of the body
    //Update the rotation of the body to the rotation of the sprite. Take care to note that the rotation of the sprite is in degrees whereas the rotation of the body is in radians.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM