★ TouchArcade needs your help. Click here to support us on Patreon.

CGRectIntersects problem

06-22-2009, 09:01 AM
#1
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
CGRectIntersects problem

Hey guys,
I'm using CGRectIntersect to determine if a ball is on a platform, and if it is, I stop it moving, if it is not, I enable my gravity. I can move the ball with buttons. However, the ball does not fall due to the gravity when it reaches the end of the platform. Somehow, when I move further on right or left, I'll be "floating in mid air" and until I reach a point, I'll fall. Anyone has a solution or has this issue before?

Thanks.
06-22-2009, 10:36 AM
#2
its kinda hard to tell without seeing some of your code.
Do you only check collision if your ball has gravity (yeah seems obvious but you never know)
maybe you could post some of the code or PM and ill take a look at it.

06-22-2009, 08:01 PM
#3
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
No, I check for gravity all the time. Only when the ball intersects the platform then I set the ball's yspeed to -1, to stop it from moving. Here's a screenshot...
Attached Thumbnails
Click image for larger version

Name:	Picture 1.png
Views:	3
Size:	14.9 KB
ID:	4892  
06-22-2009, 08:06 PM
#4
Joined: May 2009
Location: Palmerston North, New Zealand
Posts: 464
This button that you press to move the ball across, how does it affect the ball? Is it performing a translation or directly changing the frame or what?

Check out Sheepish, 100+ levels of puzzle fun! With sheep!
And more about us: www.pixelthismobile.com
plus+ id: Newtz
06-22-2009, 09:11 PM
#5
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
It calls a timer which moves the ball to the left or the right depending on which button is pressed.
06-22-2009, 09:27 PM
#6
Joined: May 2009
Location: Palmerston North, New Zealand
Posts: 464
What are you using? Just straight UIKit elements so that your ball and platform are UIImageViews or something? Or are you using openGL?

Check out Sheepish, 100+ levels of puzzle fun! With sheep!
And more about us: www.pixelthismobile.com
plus+ id: Newtz
06-23-2009, 01:17 AM
#7
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
I'm using UIImageViews, not Open GL, so yes, I'm using UIKits.
06-23-2009, 02:11 AM
#8
Make sure you're defining your rectangles properly before calling CGRectIntersection (or some similar method). Where is the origin of images for your graphics system? Pay attention to see if it's in the middle or on one of the corners, and create your containing rect appropriately. Then, to check for rect-rect collisions:

Code:
if (!CGRectIsNull(CGRectIntersection(rect1, rect2))) {
    //handle collision
}
Or if you want to check for rect-point collisions:

Code:
if (CGRectContainsPoint(rect, point)) {
    //handle collision
}

Development blog for Wormies, my first iPhone game:
http://wormiesiphone.blogspot.com/
06-23-2009, 02:31 AM
#9
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
Huh? I'm not exactly sure what you mean...In iCodeBlog's iTennis tutorial they used CGRectIntersects and it works fine, I don't think they defined any points or rectangles...

Oh and btw your game Wormies looks great.
06-23-2009, 08:53 PM
#10
Quote:
Originally Posted by froggy1233 View Post
Huh? I'm not exactly sure what you mean...In iCodeBlog's iTennis tutorial they used CGRectIntersects and it works fine, I don't think they defined any points or rectangles...

Oh and btw your game Wormies looks great.
I've never read the iTennis tutorial, but in order to do collision detection in 2d, you really need to define shapes around your objects. Most people use rectangles or circles. In your case, you'd probably have one CGRect defining the outline of your ball, and another CGRect outlining the platform. Maybe these rects are available as part of your image class, or you could make them yourself knowing the position and width/height of your objects... I work with cocos2d, where rectangles can be determined from calls to the Sprite or AtlasSprite classes.

And thanks for the Wormies comment... hopefully I can get the ambition to finish it soon.

Development blog for Wormies, my first iPhone game:
http://wormiesiphone.blogspot.com/