我正在尝试创建一个逼真的摇头应用程序,并希望将物理用于摇头动画。我已经看到其他可以做到这一点的应用程序,从我的收集中我需要使用SpriteKit。
我创建了一个SKScene,并使用了一个SKPhysicsJointPin将头部固定到身体上,但是它根本没有移动。有什么想法或建议吗?
更新代码(5/28):
现在在头上使用2个弹簧接头,它左右移动,但不能上下移动。同样,快速敲击会使头部最终向右走得足够远,以至于“掉落”并且看不见。奇怪的。
有什么想法,可以使它上下浮动,保持居中于起始位置的中心位置并停留在指定区域内,这样它就不会从身体上脱下来,看上去很有趣,它会是什么样的设置?
BobbleheadView是SKView的子类
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor =[UIColor clearColor];
self.showsFPS = YES;
self.showsNodeCount = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(animateBobble)];
[self addGestureRecognizer:tap];
SKScene *bobbleheadScene = [SKScene sceneWithSize:self.bounds.size];
[self presentScene:bobbleheadScene];
// 1. Body
self.body = [SKSpriteNode spriteNodeWithImageNamed:@"Bobble-Body"];
self.body.position = CGPointMake(self.frame.size.width/2, self.body.frame.size.height/2);
self.body.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
[bobbleheadScene addChild:self.body];
// 2. Head
self.head = [SKSpriteNode spriteNodeWithImageNamed:@"Bobble-Head"];
self.head.position = CGPointMake(self.center.x, self.body.frame.size.height);
//self.head.physicsBody.affectedByGravity = YES;
// self.head.physicsBody.dynamic = NO;
//This bobbles head great, but head falls off body and out of view
self.head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.head.size center:self.head.position];
//End
//self.head.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.head.frame];
//self.head.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.head.size.height/2];
[bobbleheadScene addChild:self.head];
// 3. Ceiling
self.ceiling = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(32, 32)];
self.ceiling.position = CGPointMake(self.frame.origin.x+self.frame.size.width/2, self.frame.size.height);
self.ceiling.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
[bobbleheadScene addChild:self.ceiling];
//Spring Joint for Ceiling to Head
SKPhysicsJointSpring *spring1 = [SKPhysicsJointSpring jointWithBodyA:self.ceiling.physicsBody bodyB:self.head.physicsBody anchorA:self.ceiling.position anchorB:CGPointMake(self.head.frame.origin.x+self.head.frame.size.width/2, 0)];
spring1.frequency = 20.0; //gives the spring some elasticity.
spring1.damping = 5.0; //Will remove damping to create the 'pendulum'
[bobbleheadScene.physicsWorld addJoint:spring1];
//Spring Joint for Head to Body
SKPhysicsJointSpring *spring = [SKPhysicsJointSpring jointWithBodyA:self.body.physicsBody bodyB:self.head.physicsBody anchorA:CGPointMake(self.body.position.x+self.body.size.width/2, self.body.position.y) anchorB:CGPointMake(self.head.position.x+self.head.size.width/2, self.body.position.y-self.body.size.height/2)];
spring.frequency = 10.0; //gives the spring some elasticity.
spring.damping = 1.0;
[bobbleheadScene.physicsWorld addJoint:spring];
}
return self;
}
-(void)animateBobble{
NSLog(@"Did Tap Bobblehead!");
[self.head.physicsBody applyImpulse:CGVectorMake(100, -200)];
//[self.body.physicsBody applyImpulse:CGVectorMake(20, 10)];
}
最佳答案
使此工作正常的唯一方法是做一个“雪橇”。
因此:制作一个应用程序,其中在屏幕上有(例如)六个滑块。
TBC,我的意思是,当应用程序实际运行时,您会看到六个滑块。
TBC,这仅是和雪橇,仅适合作为开发人员的您使用,不适用于消费类应用程序。
(我相信“雪橇”一词来自汽车行业;他们制作了底盘/引擎的第一个版本进行测试时使用。)
做到这一点,以便某些滑块控制弹簧力/阻尼(或您在cocos中可用的任何因素),而其他滑块微调连接/弹簧长度的位置。
这是获得结果的唯一方法!确保在屏幕上显示值,或者至少在更改时在控制台上将其打印出来。
这是游戏开发中的日常工作。通常,雪橇要比实际的消费者场景或功能承担更多的工作。没有“雪橇”,您将无法实现它。希望对您有所帮助!