SpriteKit.与applyForce混淆,与重力相反 [英] SpriteKit. Confusion with applyForce opposite to gravity

查看:44
本文介绍了SpriteKit.与applyForce混淆,与重力相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将下面列出的代码用作SpriteKit的实验.我所做的是向其施加与重力效果相同的力,但具有反平行向量(物体的质量设置为1.0).该功能通过更新功能在每一帧上执行.我希望该对象不会移动,因为applyForce和重力会相互补偿.

I use the code listed below as an experiment of SpriteKit. What I do is applying the same amount of force as a gravity effects to it but with anti-parallel vector (mass of body is set to 1.0). The function is executed on each frame via update function. I expect the object not to move at all as applyForce and gravity compensate each other.

-(void)didMoveToView:(SKView *)view {

  self.anchorPoint = CGPointMake(0.5, 0.5);

  SKShapeNode *node = [SKShapeNode shapeNodeWithRect:CGRectMake(0,0,WIDTH, HEIGHT)];
  node.position = CGPointMake(30, 280);
  node.strokeColor = [SKColor whiteColor];
  node.fillColor = [SKColor purpleColor];
  node.name = @"node";

  CGSize sz = node.frame.size;
  node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sz center:[GameScene Center]];
  node.physicsBody.restitution = 0.3;
  node.physicsBody.mass = 1.0;
  node.physicsBody.affectedByGravity = YES;

  self.figure = node;

  [self addChild:node];

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


-(void)update:(CFTimeInterval)currentTime {

  [self.figure.physicsBody applyForce:CGVectorMake(
                                                 - self.physicsWorld.gravity.dx,
                                                 - self.physicsWorld.gravity.dy)];

但是,物体会像仅受重力作用一样掉落.我用一个对数检查了它的质量,它总是精确地为1.0

However, the object falls down like it's effected by gravity only. I've checked the mass with a log it's always exactly 1.0

经过一些实验,我发现其中有一些不可思议的数字.它是150.如果使用下一个更新方法,则将对象搁置:

After some experiments, I've found out there is some magic number about it. It is 150. If use next update method the object is rested:

-(void)update:(CFTimeInterval)currentTime {

[self.figure.physicsBody applyForce:CGVectorMake(
                                                 - self.physicsWorld.gravity.dx,
                                                 -150 * self.physicsWorld.gravity.dy)];

在这里,如果物体的尺寸为150 * 150,则质量自动计算为1.0.无论如何,这无济于事.行为与我们直接将质量设置为1相同.

And, another moment here, the mass is automatically calculated to be 1.0 if the body has dimensions 150*150. Anyway that wouldn't help. The behavior is same as we directly set the mass equal one.

如果有人知道这里发生了什么,请帮忙!

If anyone knows what's going on here, please help!

推荐答案

比方说,SpriteKit中点与米的比例为150:1.如果我们创建尺寸为150 * 150的实体,则该质量将恰好是一个.好的,这是很好的,我们有一个等于1.0的质量体,我们不会自己更改它.但是,接下来,我们再次施加与重力相反的力并且具有相同的大小.在这种情况下,身体会再次掉落.不好.

Let's say, there is 150 to 1 ratio of points to meters in SpriteKit. If we create a body of size 150*150 then the mass would be exactly one. Ok, that's good, we have a body of mass equals 1.0, we don't change it ourselves. BUT, next we again apply the force opposite to gravity and with the same magnitude. In this case the body will fall again. No good.

好的,150/1是点对米的关系,我们需要将力乘以150以平衡力.我们可能会开始思考嗯,其中一定有一些线索".可能是重力矢量(0,-9.8)不影响公斤而不是米,而是点!好吧,让我们有150 * 150点的主体.平衡我们需要的力(理论上):

Alright, 150 / 1 is point-to-meter, and we need to multiply the force by 150 to balance the forces. We might start thinking "Hmm, there must be some clue in it". May be the gravity vector (0, -9.8) affects not kilos not meters but points! Alright then, let us have body of 150*150 points. To balance the forces we'll need (theoretically):

一方面重力为9.8牛顿(千*米)/秒^ 2)

on one hand the force of gravity: 9.8 Newtons (kilo * meter) / sec ^ 2)

另一方面,我们施加力:(1.0公斤)* 9.8 *(150分)/(秒^ 2)

on another hand we applyForce: (1.0 kilo) * 9.8 * (150 points) / (sec^2)

因此,在这里我们错过了150.无论如何,这似乎有点愚蠢,但是applyForce的度量单位为((points * kg)/sec ^ 2),但是重力加速度的单位是牛顿((kilo *米))/秒^ 2)(尽管在文档中将其描述为每秒米.事实上,每秒米!加速度!).将其乘以质量并得到力.

So, here we miss the 150. Anyway,this seems to be little bit stupid, but applyForce is measured in ((points * kilo) / sec ^ 2), but the gravity acceleration is in Newtons ((kilo * meter)/ sec ^ 2) (despite the fact it's described as meters per second in documentation. Meters per second! Acceleration!). Multiply it by mass and get the force.

这篇关于SpriteKit.与applyForce混淆,与重力相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆