スプライトに物理ボディがあります。スプライトがいずれかの方向に移動すると、物理ボディはその方向に移動します(添付の画像を参照)。これは接触と衝突に悪いことです。スプライトキット:移動中に物理ボディが移動した
物理的なボディは、移動中にのみ移動します。スプライトが静止していると、物理学のボディはずれることはありません。
私の質問: スプライトキットの物理方向が移動方向にずれているのはなぜですか?それを防ぐ方法はありますか?
どうもありがとう
EDIT:
OK ..私はこの問題を示すために小さなテストを書きました。重力の影響を受けていない物理的なボディを持つノードは1つしかありません。物理学の世界にも重力はありません。スクリーンに触れることで、ヒーローが上に移動します。コードをテストしたい場合は、SKView showsPhysics
プロパティ= YESを設定します。タッチすることで、物理的な体が正しい場所からどのように動くかを観察します。その後、移動のたびに移動方向に移動します。
ありがとうございます。
// header file
#import <SpriteKit/SpriteKit.h>
@interface TestScene : SKScene <SKPhysicsContactDelegate>
@end
// implementation file
#import "TestScene.h"
@implementation TestScene
{
SKNode *_world;
SKSpriteNode *_hero;
BOOL _play;
BOOL _touch;
CGFloat _startY;
}
- (instancetype)initWithSize:(CGSize)size
{
if(self = [super initWithSize:size])
{
self.backgroundColor = [SKColor whiteColor];
self.physicsWorld.contactDelegate = self;
// no gravity
//self.physicsWorld.gravity = CGVectorMake(0.0f, -9.8f);
_play = NO;
_touch = NO;
_startY = 50;
[self addWorld];
[self addHero];
}
return self;
}
- (void)addWorld
{
_world = [SKNode node];
_world.position = CGPointMake(0, 0);
[self addChild:_world];
}
- (void)addHero
{
_hero = [SKSpriteNode spriteNodeWithImageNamed:@"hero"];
_hero.size = CGSizeMake(50, 50);
_hero.position = CGPointMake(CGRectGetMidX(self.frame), _startY);
[_world addChild:_hero];
_hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:_hero.size.width/2.0f];
_hero.physicsBody.affectedByGravity = NO;
_hero.physicsBody.dynamic = YES;
_hero.physicsBody.allowsRotation = NO;
}
- (void)applyHeroUpwardForce
{
[_hero.physicsBody applyForce:(CGVectorMake(0, 100))];
// limit velocity
CGVector vel = _hero.physicsBody.velocity;
vel.dy = vel.dy > 1000 ? 1000 : vel.dy;
_hero.physicsBody.velocity = vel;
// control the hero movement. it really moves upwards even we don’t see that, since the _world moves downwards
NSLog(@"_hero.position.y: %f", _hero.position.y);
}
- (void)updateWorld
{
_world.position = CGPointMake(_world.position.x, -(_hero.position.y - _startY));
}
- (void)didSimulatePhysics
{
if(!_play)
return;
if(_touch)
{
[self applyHeroUpwardForce];
[self updateWorld];
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if(!_play)
_play = YES;
_touch = YES;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
_touch = NO;
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
_touch = NO;
}
@end
EDIT 2:
The same issue in the game 'Pierre Penguin Escapes The Antarctic'
私は本当にあなたが求めていることを理解していません。もちろん、物理学のボディはスプライトと一緒に動きますが、なぜあなたはそれを望んでいませんか? – Pierce
関連するコードをアップロードする必要があります... – Whirlwind
はい、物理ボディはスプライトと共に移動します。しかし、物理学の身体は追い出される。添付の画像をご覧ください。ミサイルが裏側の宇宙船に当たった場合、宇宙船の物理的ボディが添付の画像に示すように前面に移動するため、接触/衝突は発生しません – suyama