gpt4 book ai didi

ios - Cocos2d V3 (Chipmunk) 中具有摩擦和限制的 PivotJoints

转载 作者:可可西里 更新时间:2023-11-01 04:35:06 27 4
gpt4 key购买 nike

我尝试将我从 SpriteKit 开始的游戏转换为 Cocos2d V3。不过我在物理学方面有一些问题。我创造了一个像玩人偶一样的人。四肢通过 SpriteKit 中的 PinJoints 连接。我通过 Chipmunk 中的 PivotJoints 重新创建了该基本功能。这按预期工作。

但是我需要给关节增加一些摩擦力。现在它们只是尽可能快地旋转,更重要的是我需要限制关节的旋转角度。可以想象,大多数人都不能 360 度自由旋转肩膀。 ;)

据我所知,我应该使用 ChipmunkRotaryLimitJoint 和 ChipmunkGearJoint 来实现。我的问题是,我无法让它工作,因为我不知道如何正确连接它们。这是我现在的代码:

- (CCPhysicsJoint *)createPinJointWithBodyA:(CCPhysicsBody *)bodyA
bodyB:(CCPhysicsBody *)bodyB
anchor:(CGPoint)anchor
friction:(CGFloat)friction
lowerLimit:(CGFloat)lowerLimit
upperLimit:(CGFloat)upperLimit
{

CCPhysicsJoint *pin = [CCPhysicsJoint
connectedPivotJointWithBodyA:bodyA
bodyB:bodyB
anchorA:anchor];

ChipmunkRotaryLimitJoint *limitJoint = [ChipmunkRotaryLimitJoint
rotaryLimitJointWithBodyA:bodyA.body
bodyB:bodyB.body
min:CC_DEGREES_TO_RADIANS(lowerLimit)
max:CC_DEGREES_TO_RADIANS(upperLimit)];

ChipmunkGearJoint *gearJoint = [ChipmunkGearJoint
gearJointWithBodyA:bodyA.body
bodyB:bodyB.body
phase:0.5
ratio:0.5];

return pin;
}

(gearJoint 调用中的值只是占位符!)

有人知道这应该如何工作吗?我通过谷歌找到了一些东西,但所有教程都假设我在其他环境中使用花栗鼠。我是否必须为这些限制关节和齿轮关节创建自己的 CCPhysicJoint 实现?

谢谢和最良好的祝愿,托马斯

最佳答案

我想我解决了这个问题。事实证明,我必须创建自己的 CCPhysicsJoint 类别来实现 ChipmunkRotaryLimitJoint 和 ChipmunkRotaryDampedSpring。它不完全相同,但现在已经足够好了。

代码现在看起来像这样:

[CCPhysicsJoint connectedRotaryLimitJointWithBodyA:bodyA
bodyB:bodyB
min:CC_DEGREES_TO_RADIANS(lowerLimit)
max:CC_DEGREES_TO_RADIANS(upperLimit)];

[CCPhysicsJoint connectedDampedRotarySpringWithBodyA:bodyA
bodyB:bodyB
restAngle:0
stiffness:friction *1000.0f
damping:10000.0f];

我只是问这是否应该包含在 Cocos2D 代码库中。目前,这是包装器:

CCPhysicsJoint+THCAdditions.h

//
// CCPhysicsJoint+THCAdditions.h
//
// Created by Thomas Hempel on 02.02.14.
// Copyright (c) 2014 Thomas Hempel. All rights reserved.
//

#import "CCPhysicsBody.h"
#import "CCPhysicsJoint.h"

@interface CCPhysicsJoint (THCAdditions)

+ (CCPhysicsJoint *)connectedRotaryLimitJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB min:(CGFloat)min max:(CGFloat)max;
+ (CCPhysicsJoint *)connectedGearJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB phase:(CGFloat)phase ratio:(CGFloat)ratio;
+ (CCPhysicsJoint *)connectedSimpleMotorJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB rate:(CGFloat)rate;
+ (CCPhysicsJoint *)connectedDampedRotarySpringWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB restAngle:(CGFloat)restAngle stiffness:(CGFloat)stiffness damping:(CGFloat)damping;

@end

CCPhysicsJoint+THCAdditions.m

//
// CCPhysicsJoint+THCAdditions.m
//
// Created by Thomas Hempel on 02.02.14.
// Copyright (c) 2014 Thomas Hempel. All rights reserved.
//

#import "CCPhysicsJoint+THCAdditions.h"
#import "CCPhysics+ObjectiveChipmunk.h"

// ------------------------------------------------------------------------
#pragma mark - Rotary Limit Joint
// ------------------------------------------------------------------------

@interface CCPhysicsRotaryLimitJoint : CCPhysicsJoint
@end

@implementation CCPhysicsRotaryLimitJoint {
ChipmunkRotaryLimitJoint *_constraint;
}

- (id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB min:(CGFloat)min max:(CGFloat)max
{
if ((self = [super init])){
_constraint = [ChipmunkRotaryLimitJoint rotaryLimitJointWithBodyA:bodyA.body
bodyB:bodyB.body
min:min
max:max];
_constraint.userData = self;
}
return self;
}

- (ChipmunkConstraint *)constraint
{
return _constraint;
}

- (void)willAddToPhysicsNode:(CCPhysicsNode *)physics
{
}

@end

// ------------------------------------------------------------------------
#pragma mark - Gear Joint
// ------------------------------------------------------------------------

@interface CCPhysicsGearJoint : CCPhysicsJoint
@end

@implementation CCPhysicsGearJoint {
ChipmunkGearJoint *_constraint;
}

- (id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB phase:(CGFloat)phase ratio:(CGFloat)ratio
{
if ((self = [super init])){
_constraint = [ChipmunkGearJoint gearJointWithBodyA:bodyA.body
bodyB:bodyB.body
phase:phase
ratio:ratio];
_constraint.userData = self;
}

return self;
}

- (ChipmunkConstraint *)constraint
{
return _constraint;
}

- (void)willAddToPhysicsNode:(CCPhysicsNode *)physics
{
}

@end

// ------------------------------------------------------------------------
#pragma mark - Simple Motor Joint
// ------------------------------------------------------------------------

@interface CCPhysicsSimpleMotorJoint : CCPhysicsJoint
@end

@implementation CCPhysicsSimpleMotorJoint {
ChipmunkSimpleMotor *_constraint;
}

- (id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB rate:(CGFloat)rate
{
if ((self = [super init])){
_constraint = [ChipmunkSimpleMotor simpleMotorWithBodyA:bodyA.body
bodyB:bodyB.body
rate:rate];
_constraint.userData = self;
}

return self;
}

- (ChipmunkConstraint *)constraint
{
return _constraint;
}

- (void)willAddToPhysicsNode:(CCPhysicsNode *)physics
{
}

@end

// ------------------------------------------------------------------------
#pragma mark - Damped Rotary Spring
// ------------------------------------------------------------------------

@interface CCPhysicsDampedRotarySpring : CCPhysicsJoint
@end

@implementation CCPhysicsDampedRotarySpring {
ChipmunkDampedRotarySpring *_constraint;
}

- (id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB restAngle: (CGFloat)restAngle stiffness:(CGFloat)stiffness damping:(CGFloat)damping
{
if ((self = [super init])){
_constraint = [ChipmunkDampedRotarySpring dampedRotarySpringWithBodyA:bodyA.body
bodyB:bodyB.body
restAngle:restAngle
stiffness:stiffness
damping:damping];
_constraint.userData = self;
}

return self;
}

- (ChipmunkConstraint *)constraint
{
return _constraint;
}

- (void)willAddToPhysicsNode:(CCPhysicsNode *)physics
{
}

@end

// ------------------------------------------------------------------------
#pragma mark - Implementation
// ------------------------------------------------------------------------


@implementation CCPhysicsJoint (THCAdditions)

+ (CCPhysicsJoint *)connectedRotaryLimitJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB min:(CGFloat)min max:(CGFloat)max
{

CCPhysicsJoint *joint = [[CCPhysicsRotaryLimitJoint alloc] initWithBodyA:bodyA bodyB:bodyB min:min max:max];
[bodyA addJoint:joint];
[bodyB addJoint:joint];

[bodyA.physicsNode.space smartAdd:joint];

return joint;
}

+ (CCPhysicsJoint *)connectedGearJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB phase:(CGFloat)phase ratio:(CGFloat)ratio
{
CCPhysicsJoint *joint = [[CCPhysicsGearJoint alloc] initWithBodyA:bodyA bodyB:bodyB phase:phase ratio:ratio];

[bodyA addJoint:joint];
[bodyB addJoint:joint];

[bodyA.physicsNode.space smartAdd:joint];

return joint;
}

+ (CCPhysicsJoint *)connectedSimpleMotorJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB rate:(CGFloat)rate
{
CCPhysicsJoint *joint = [[CCPhysicsSimpleMotorJoint alloc] initWithBodyA:bodyA bodyB:bodyB rate:rate];

[bodyA addJoint:joint];
[bodyB addJoint:joint];

[bodyA.physicsNode.space smartAdd:joint];

return joint;
}

+ (CCPhysicsJoint *)connectedDampedRotarySpringWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB restAngle:(CGFloat)restAngle stiffness:(CGFloat)stiffness damping:(CGFloat)damping
{
CCPhysicsJoint *joint = [[CCPhysicsDampedRotarySpring alloc] initWithBodyA:bodyA bodyB:bodyB restAngle:restAngle stiffness:stiffness damping:damping];

[bodyA addJoint:joint];
[bodyB addJoint:joint];

[bodyA.physicsNode.space smartAdd:joint];

return joint;
}

@end

祝你好运,托马斯

关于ios - Cocos2d V3 (Chipmunk) 中具有摩擦和限制的 PivotJoints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21508667/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com