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.
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.
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...
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?
What are you using? Just straight UIKit elements so that your ball and platform are UIImageViews or something? Or are you using openGL?
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 }
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.