问题
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];
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.
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?
Hope you guys can help me out.
Thanks in advance!
回答1:
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:
- {x1, y1} = starting point that you already have
{x2, y2] = point you're looking to find
- x2 = x1 + (distance * cosA)
- y2 = y1 + (distance * sinA)
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.