如何检测物理体不同区域的接触 [英] How to detect contact on different areas of a physicsbody

查看:18
本文介绍了如何检测物理体不同区域的接触的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 sprite-kit 游戏中实现经典的爆头"效果.

I would like to achieve the classic 'headshot' effect in a sprite-kit game.

我的想法和尝试:

  1. 在同一个精灵上创建单独的body"和head"physicsBodies,并用关节固定它们.不可能:(

  1. Create separate 'body' and 'head' physicsBodies on the same sprite and pin them with a joint. Impossible :(

在敌人"精灵上创建一个body"物理体,并创建另一个透明精灵,其头部"physicsBody 跟随身体(位于顶部).这种方法要求我跟踪什么头部与什么身体相关联.它还会降低游戏效率.我很好奇我是否可以创建一个 SKSpriteNode 的子类并创建一个将这两个 spritenode 和physicsBodies 捆绑在一起的敌人".

Create a 'body' physics body on the "enemy" sprite and create another transparent sprite with a 'head' physicsBody that follows the body (sits on top). This approach requires me to keep track of what head is associated with what body. It will also decrease the games efficiency. I am curious if I could make a subclass of SKSpriteNode and create an "enemy" that has these two spritenodes and physicsBodies tied together.

检查弹丸与敌人的位置,看看弹丸是否在精灵顶部的某个 Y 范围内接触.我认为这将是低效的,因为弹丸飞行速度很快.

Check the location of the projectile vs. the enemy to see if the projectile made contact in a certain Y range on the top of the sprite. I believe this will be inefficient as the projectiles are flying quite fast.

如果有人创造了这种效果并可以分享他们的知识,我将不胜感激.谢谢!

I would appreciate it if anyone has created this effect and can share their knowledge. Thank you!

推荐答案

我在很多地方都使用这个设置.如果您发现自己想要添加更多部件或武器,它可以很好地扩展,它可以让您在视觉上轻松隐藏任何这些部件或武器.如果你有很多事情发生并且不同的敌人有不同的东西",或者你正在做一些疯狂的事情,比如根据游戏玩法将子精灵养育到不同的实体等,userData 的使用会派上用场.

I use this setup in a lot of places. It scales pretty well if you find yourself wanting to add more parts, or weapons, and it lets you visually hide any of those parts or weapons easily. The use of userData can come in handy if you have a lot going on and different enemies have different "things", or you're doing crazy stuff like parenting child sprites to different entities based on gameplay, etc.

-(SKSpriteNode*)makeAnEnemy
{

    SKSpriteNode *mainEnemy;
    SKSpriteNode *enemyHead;
    //setup mainEnemy physics body
    //setup enemyHead physics body
    [mainEnemy addChild:enemyHead];

    // depending on your plans, on hit you could use the following code later:
    SKSpriteNode *theEnemyThatWasHit = (SKSpriteNode*)enemyHead.parent;

    //or you can use userdata and set it all up here
    [mainEnemy.userData setObject:enemyHead forKey:@"myHead"];
    [enemyHead.userData setObject:mainEnemy forKey:@"myBody"];

    return mainEnemy;
}

这篇关于如何检测物理体不同区域的接触的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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