I am looking to draw a transparent rectangle in a class called HitBox using a CGRect object. Is there something aside from "drawPoly" that I could use to draw the rectangle?
how to draw a rectangle from CGRect?
(5 posts) (4 voices)-
Posted 2 years ago #
-
I created a function to draw a rectangle but it doesn't appear to be working with my collision detection. What is happening is that I see the the rectangles overlapping but no collision is taking place unless there is an excessive amount of overlap. I don't know if it's the collision code that is incorrect or the way I'm drawing the hit boxes.
// these 2 methods belong to my HitBox class and are used to to draw the hit box
-(void) update: (CGRect) r;
{
x = r.origin.x;
y = r.origin.y;width = r.size.width;
height = r.size.height;
}-(void) draw
{
CGPoint vertices2[] = {
ccp(x-width/2,y+height/2),
ccp(x+width/2,y+height/2),
ccp(x+width/2, y-height/2),
ccp(x-width/2, y-height/2)
};
drawPoly(vertices2, 4, YES);
}// this is my code in my main game loop for detecting collision
if (!CGRectIsNull(CGRectIntersection(rect1, rect2)))
{
NSLog(@"collision!");
}Posted 2 years ago # -
Hmm, try a different collision detection method?
if (CGRectIntersectsRect(rect1, rect2)) { NSLog(@"collision!"); }How often is your main game loop executed? I've read that draw is called as often as possible.
Are you checking for collisions frequently enough? (this might be a silly idea tbh)Posted 2 years ago # -
Hi,
I think you are getting the vertices wrong
It should be like this:`CGPoint vertices2[] = {
ccp(x,y),
ccp(x+width,y),
ccp(x+width, y+height),
ccp(x, y+height)
};`Posted 1 year ago # -
lol, I hope he wasn't waiting on that one ;)
Posted 1 year ago #
Reply
You must log in to post.