简体   繁体   中英

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. 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. 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.

在此处输入图片说明

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)

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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