Collision Checking for Animations

Discussion in 'iOS Programming' started by HarryWorksInc, Jun 28, 2010.

  1. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #1
    I have a game in which the the user taps the screen to fire. The problem is that when I animate the moving of the image for the bullet as such
    HTML:
    	[UIView beginAnimations:nil context:nil];
    	[UIView setAnimationDuration:.75];
    	bullet.center = thumbPoint.center;
    	[UIView commitAnimations];
    As expected you cannot detect collisions with other CGRects. Is there a way to animate the bullet in a way that i can use CGRectIntersectsRect?

    Thanks
     
  2. bweberapps macrumors member

    Joined:
    Jun 25, 2010
    #2
    I'm not sure how far into your game you are, but I would suggest working with OpenGL. There are free libraries available to help you code games. I prefer cocos2d with the box2d physics engine. This allows you to add sprites to the screen and a physics body associated with it. Then add a contact listener and when bodyA hits bodyB you can tell it what to do.
     
  3. HarryWorksInc thread starter macrumors regular

    Joined:
    Feb 21, 2010
    #3
    Ive only been working on it for like a week so i could convert but I have never worked with OpenGL and it seems complicated. Sounds interesting though. I think i will stick within my comfort zone for this and move into OpenGL ES later.
     
  4. bweberapps macrumors member

    Joined:
    Jun 25, 2010
    #4
    That's the beauty of cocos2d, you don't actually do any OpenGL code yourself. I have never done OpenGL and went from brand new to cocos2d to the app store in 3 months. Check it out, but I understand sticking with what your comfortable with.
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    I'm not a game programmer but if you break your animation down into steps you should be able to do collision detection at each step. You can chain your animations so that the center moves a little bit at a time and you set a animationDidEnd selector which checks the collisions and then does another step in the animation. Otherwise use a timer and move the center a little bit each tick.
     
  6. NickFalk macrumors 6502

    NickFalk

    Joined:
    Jun 9, 2004
    #6
    I second the comments about Cocos2D. I made my last game Fumbers using nothing but Apple's provided API's thinking Cocos2D would complicate things. When I started working with the framework (for my upcoming Squid Drop) I found that it is actually a lot easier than Apple's stuff when it comes to game programming...
     
  7. PizzaTray macrumors newbie

    Joined:
    Oct 31, 2009
    #7
    very little Exmple:
    Code:
    /* for example create some object named Player - inherit from uiimageView
        and sets its properties. (image,location,size,speed..)
        And another one - Bullet (or something else) */
    
    //h. file:
    #import "Player.h"
    #define SPEED_GAME  0.04
    @interface GameController {
        NSTimer         *mainTimer;
        NSMutableArr  *arrBullets;
        BOOL               isPause;
        Player             *plr;
    }
    - (void) gameLoop;
    
    //m. file
    #import "Bullet.h"
    - (id) init {
       self = [super init];
       if(self!=nil) {
          /* mainTimer run on the gameLoop every 0.04 sec. */
          mainTimer = [NSTimer scheduledTimerWithTimeInterval:SPEED_GAME
                              target:self selector:@selector(gameLoop) userInfo:nil
                              repeats:YES];
       }
       return self;
    }
    - (void) gameLoop {
        if(!isPause) {
            if(movePlrUp)     plr.center.y -= plr.speedY;
            if(movePlrDown) plr.center.y +=plr.speedY;
            ..
            if(Shoot)            [self createBullet];
            [self moveBullets];
    
            if([self collision]) //do Something;
        }
    }
    - (void) createBullet {
       Bullet *b = [[Bullet alloc]init];
       [b getLocation];
       [arrBullets addObject:b];
       [self.view addSubview:b];
       [b release];
    }
    - (void) moveBullets {
       if([arrBullets count]>0) {
          for(int i=0; i<[arrBullets count]; i++) {
              [[arrBullets objectAtIndex:i] move];
              /* the move mehtod inside Bullet.m need to look like this:
                  self.center.x += self.speed; */
              if([[arrBullets objectAtIndex:i] checkIfOutOfBounds]) {
                 [[arrBullets objectAtIndex:i]removeFromSuperview];
                 [arrBullets removeObjectAtIndex:i];
             }
          }
       }
    }
    
     
  8. HarryWorksInc thread starter macrumors regular

    Joined:
    Feb 21, 2010
    #8
    well in this game i have directional shooting so its simple to move it on the y axis (forward) but on the x axis (aiming) that wont work, i could go back to 7th grade math and try and figure out for every 4 pixels you move on the y axis how many to move on the x axis, if any.
     

Share This Page