CGRectIntersects problem

Discussion in 'Public Game Developers Forum' started by kohjingyu, Jun 22, 2009.

  1. kohjingyu

    kohjingyu Well-Known Member

    Mar 20, 2009
    1,770
    0
    0
    Student/Developer
    Singapore
    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.
     
  2. makeout

    makeout Well-Known Member

    Apr 23, 2009
    46
    0
    0
    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.
     
  3. kohjingyu

    kohjingyu Well-Known Member

    Mar 20, 2009
    1,770
    0
    0
    Student/Developer
    Singapore
    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 Files:

  4. PixelthisMike

    PixelthisMike Well-Known Member

    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?
     
  5. kohjingyu

    kohjingyu Well-Known Member

    Mar 20, 2009
    1,770
    0
    0
    Student/Developer
    Singapore
    It calls a timer which moves the ball to the left or the right depending on which button is pressed.
     
  6. PixelthisMike

    PixelthisMike Well-Known Member

    What are you using? Just straight UIKit elements so that your ball and platform are UIImageViews or something? Or are you using openGL?
     
  7. kohjingyu

    kohjingyu Well-Known Member

    Mar 20, 2009
    1,770
    0
    0
    Student/Developer
    Singapore
    I'm using UIImageViews, not Open GL, so yes, I'm using UIKits.
     
  8. allenfjordan

    allenfjordan Active Member

    May 21, 2009
    39
    0
    0
    Senior Computer Specialist at NOAA
    Colorado
    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
    }
    
     
  9. kohjingyu

    kohjingyu Well-Known Member

    Mar 20, 2009
    1,770
    0
    0
    Student/Developer
    Singapore
    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. :)
     
  10. allenfjordan

    allenfjordan Active Member

    May 21, 2009
    39
    0
    0
    Senior Computer Specialist at NOAA
    Colorado
    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.
     

Share This Page