【问题标题】:SpriteKit physics body returning to original vertical position after hit/knockSpriteKit 物理体在撞击/敲击后返回到原始垂直位置
【发布时间】:2014-08-22 07:26:57
【问题描述】:

我正在尝试制作一种物理机制,其中垂直站立的物体可以被击中或击倒,然后将旋转回到它的原点位置。把它想象成一个安装在地板上的沙袋。因此对象将具有较低的枢轴/锚点。

我只是想要一个关于如何使用 SpriteKit 物理来解决这个问题的理论指导。

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 使用 SKPhysicsJointPin 并根据身体摆动的一侧修改摩擦扭矩
  • 对象不能永久锚定或连接到“地面”对象。我可能正在考虑在底部加入“高质量”,在顶部加入“低质量”,希望“低质量”物体会在一定程度上抵抗重力,但无法将“高质量”物体抬起跨度>
  • 如果设计是问题,这个问题更适合gamedev.stackexchange.com 您可以通过对身体施加力/扭矩来实现效果,行为不一定必须专门建模有身体和关节。

标签: ios7 sprite-kit physics game-physics skphysicsbody


【解决方案1】:

下面通过连接两个物体来创建一个复合对象:一个圆和一个重量。权重相对于圆心偏移,并且密度更大。当添加到场景中时,重力会旋转组合对象,因此具有重量的一侧位于底部。使用它 1) 创建一个新的 sprite kit 游戏,2) 用此代码替换默认的 initWithSize 和 touchesBegan 方法,以及 3) 在场景中的各个位置运行并单击。

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        SKShapeNode *circle = [SKShapeNode node];
        circle.path =[UIBezierPath bezierPathWithOvalInRect: CGRectMake(-32, -32, 64, 64)].CGPath;
        circle.position = location;
        circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:32];

        SKSpriteNode *weight = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(8, 8)];
        // Adjust this to get the desire effect
        weight.position = CGPointMake(location.x+1, location.y+28);
        weight.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:4];
        // Adjust this to get the desired effect
        weight.physicsBody.density = 100.0;

        // The physics bodies must be in the scene before adding the joint
        [self addChild:circle];
        [self addChild:weight];

        // Join the circle and the weight with a physics joint
        SKPhysicsJoint *joint = [SKPhysicsJointFixed jointWithBodyA:circle.physicsBody bodyB:weight.physicsBody anchor:weight.position];
        [self.physicsWorld addJoint:joint];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 2012-06-05
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多