简体   繁体   English

如何获得正方形的一角

[英]How to get corner of a square

Hi all I having troubles with physics in spriteKit so I decided to make my own, and I need to get the corners of a square. 大家好,我在spriteKit中遇到物理方面的麻烦,所以我决定自己做一个,而且需要弄个正方形的角。 I've tried 我试过了

    _hero.position.x-_hero.size.width/2-_hero.size.height/2

which I thought it was giving me the down left corner of a square, but when I run my game it doesn't do what I expect. 我以为这给了我正方形的左下角,但是当我运行游戏时,它并没有达到我的期望。 I'm doing a 2D "super mario galaxy" which means that my player runs in circles in little planets, the thing is, I need to put the corners positions in an if() that will check, for example, that the top right corner of the player is greater than the bottom left corner of a square, and in that case I will set a BOOL that will tell the program the player is under the square, because didBeginContact is buggy, I had a lot of problems with it. 我正在执行2D“超级马里奥星系”,这意味着我的玩家在小行星上盘旋运行,问题是,我需要将转角位置放在if() ,以检查例如右上角播放器的角大于一个正方形的左下角,在这种情况下,我将设置一个BOOL来告诉程序播放器在该正方形下,因为didBeginContact是越野车,所以我遇到了很多问题。 Anyone's help would be great. 任何人的帮助都会很大。

PD: I leave a sample of my game, it's still very primitive but hope you like the concept and optionally, any feedback (good or bad) is welcome. PD:我留下了我的游戏示例,它仍然很原始,但是希望您喜欢这个概念,并且可以选择接受任何反馈(好或坏)。

在此处输入图片说明

The square would have an origin and a frame. 正方形将有一个原点和一个框架。 See below. 见下文。 It may be easier to convert from the local coordinate system (square) to the parent's coordinate system if you are trying to find each corner. 它可能会更容易转换 ,如果你正在努力寻找各个角落,从局部坐标系(方)于母公司的坐标系。

和物体的坐标

However, I think your actual question would be about collision detection. 但是,我认为您的实际问题将与碰撞检测有关。 In that case, make a CGRect of each object and then see if they intersecting using CGRectIntersectsRect(rect1,rect2) 在这种情况下, CGRect对每个对象进行CGRect ,然后使用CGRectIntersectsRect(rect1,rect2)查看它们是否相交

twodayslate has some good information, but I thought I would add that there are some pretty cool CGRect APIs that I didn't know were available until recently. twodayslate提供了一些很好的信息,但是我想我要补充一点,直到最近我才知道有一些非常酷的CGRect API。 Consider this rect: 考虑一下这个矩形:

CGRect rect = CGRectMake(100, 100, 100, 200);

Using the following methods will return some nice convenient results: 使用以下方法将返回一些方便的结果:

CGRectGetWidth(rect) --> 100
CGRectGetHeight(rect) --> 200
CGRectGetMaxX(rect) --> 200
CGRectGetMaxY(rect) --> 300

This is compared to what most people use: 与此相比,大多数人使用:

rect.size.width
rect.size.height
rect.origin.x + rect.size.width
rect.origin.y + rect.size.height
rect.origin.x + rect.size.width

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM