CGRect有一个角度 [英] CGRect with an angle

查看:122
本文介绍了CGRect有一个角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条线,它是使用此代码制作的sprite。

I have a line, which is a sprite made by using this code

CGPoint diff = ccpSub(startLocation, endLocation);
float rads = atan2f( diff.y, diff.x);
float degs = -CC_RADIANS_TO_DEGREES(rads);
float dist = ccpDistance(endLocation, startLocation);
CCSprite *line = [CCSprite spriteWithFile:@"line.png"];
[line setAnchorPoint:ccp(0.0f, 0.5f)];
[line setPosition:endLocation];
[line setScaleX:dist / line.boundingBox.size.width];
[line setRotation: degs];

line.tag = 1;
[_lines addObject:line];
[self addChild:line];



现在在我的碰撞检测代码中,我使用下面的代码创建一个CGRect:

Now in my collision detection code I use the following code to create a CGRect:

        CGRect lineRect = CGRectMake(
        line.position.x - (line.contentSize.width/2), 
        line.position.y - (line.contentSize.height/2), 
        line.contentSize.width, 
        line.contentSize.height);

这当然是错误的,因为线是使用角度。

This of course is faulty because the line is made using an angle.

我试图比较一个矩形,一个方形精灵,与这个矩形。
这个想法是一个角色正在移动,玩家可以画一条线,如果角色击中线,则它的反弹方向相反。

I'm trying to compare a rectangle, a square sprite, with this rectangle. The idea is that a character is moving and the player can draw a line, if the character hits the line it's bounces of in the opposite direction.

具有角度,线的一侧的x& y位置和线的长度。
如何获得另一行的另一个x& y位置?

I have the angle, the x&y position of one side of the line and the length of the line. How do I get the other x&y position of the other side of the line?

希望你们能帮助我。

提前感谢!

推荐答案

这很难知道, ,但是当你说...行另一侧的另一个x& y位置时,我假设你的意思是该行另一端的坐标是什么。

It's a little hard to know if I understand you correctly, but when you say "...the other x&y position of the other side of the line," I'm assuming you meant what is the coordinates of the opposite endpoint of the line.

如果是这样,我相信你可能正在寻找的实际公式是:

If so, I believe the actual formula you may be looking for is:


  1. {x1,y1} =

  2. {x2,y2] =您正在寻找的点

  1. {x1, y1} = starting point that you already have
  2. {x2, y2] = point you're looking to find


  • x2 = x1 +(distance * cosA)

  • y2 = y1 +(distance * sinA)

但是正如我前面提到的,我有点不确定这是否会给你所需要的。如果你实际上正在寻找Line.png的边界框相对边的顶点,那么你也许仍然可以使用该公式,但是改变90度,以确定从x&

But as I mentioned earlier, I'm a little unsure whether this would give you exactly what you needed. If you're actually looking for the points of the vertices of the opposite side of the bounding box for the Line.png, then perhaps you could still use that formula, but change the degrees by 90 to determine the coordinates offset from the x&y that you already know.

无论如何,我希望至少能帮助你在一个有用的方向。

Anyway, I hope that at least helps put you in a helpful direction.

这篇关于CGRect有一个角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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