Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 18, 2011, 10:06 AM   #1
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Best Way to Check Collisions?

I'm not sure what the best way of putting this is... I'm trying to decide what the best way of drawing levels and detecting collisions with the environment in a 2D platforming game is.

Right now, I have four separate arrays for my grounds, ceilings, left walls, and right walls. Based on x and y velocities, I determine which I need to check for collisions with. For example, if yVelocity is negative, I check whether the character frame intersects with any of the grounds' frames:

Code:
if (yVelocity < 0)
{
    int i;
    for (i = 0; i < [grounds count]; i++)
    {
        CGRect ground = [[grounds objectAtIndex:i] frame];
        if (CGRectIntersectsRect(frame, ground))
        {
            onGround = YES;
            frame.origin.x -= (CGRectGetMaxX(frame) - CGRectGetMinX(ground));
            yVelocity = 0;
            break;
        }    
        else onGround = NO;
    }
}
If I throw in before the break in my check an NSLog that lets me know it's done checking for a collision with a given wall, then I receive the log reports every .003 seconds or so when running on the device. This means that if I have more than 10 different grounds to check, that I risk the game noticeably slowing down... right?

I was thinking about the possibility of instead using a pathing map or something. Like, if I have a bmp with 8 possible colors, it could check the color of the bit in the direction the character is heading to see whether or not they've hit a surface. Would this be faster?

What have other people done to check for collisions?

Edit: I'm looking at Apple's example "GKTank" app. They store walls a different way...

Code:
UIImageView	*walls[NUMWALLS];
whereas I have

Code:
NSMutableArray     *grounds;
Does the fact that they're using the [NUMWALLS] speed up the game somehow? I don't understand straight C so well, but I think that's what they're using by typing that... like, a straight C version of an NSArray. In doing so, I would guess there might be less overhead? Questions it prompts though... would using their method make the array immutable? How would I go from level to level if I wanted the size to change and stuff?
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)

Last edited by ArtOfWarfare; Jul 18, 2011 at 11:21 AM.
ArtOfWarfare is offline   0 Reply With Quote
Old Jul 18, 2011, 11:04 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Pure-C arrays will be much faster than Cocoa NSArray (or NSMutableArrays). The cost of accessing an element in a C array is basically a memory offset operation (addition) and that gets you the memory address. The cost of accessing a Cocoa NSArray is that (right at the end) + a number of Cocoa message calls (not just one as NSArray access is bounds checked) each of which equates to being slightly more expensive that a function call.

There is a (not very good test) showing the sort of speed difference here. I say this is not very good as it may actually be testing the performance of C floats vs NSNumber.

There is an interesting test here showing the sort of improvements that can be made by caching selectors (this is certainly the sort of thing I'd expect you to be doing).
robbieduncan is offline   0 Reply With Quote
Old Jul 18, 2011, 11:23 AM   #3
ArtOfWarfare
Thread Starter
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Ok, thanks for that Robbie.

I'll change my NSMutableArrays to be C Arrays instead... but I'm curious as to how I change the size of a C Array while my app is running?

Maybe I should consult my reference book on ANSI C and see what info it gives on Arrays...
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   0 Reply With Quote
Old Jul 18, 2011, 11:24 AM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by ArtOfWarfare View Post
I'll change my NSMutableArrays to be C Arrays instead... but I'm curious as to how I change the size of a C Array while my app is running?
Erm, basically you don't change the size of a C Array once you've created it.
robbieduncan is offline   0 Reply With Quote
Old Jul 18, 2011, 11:33 AM   #5
ArtOfWarfare
Thread Starter
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by robbieduncan View Post
Erm, basically you don't change the size of a C Array once you've created it.
Huh... but I can change the contents of the array, if I'm reading Apple's example code properly. If I make the array as large as it'll ever need to be, I can keep a separate int that says how many spaces in the array are actually filled, right?
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   0 Reply With Quote
Old Jul 18, 2011, 11:40 AM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by ArtOfWarfare View Post
Huh... but I can change the contents of the array, if I'm reading Apple's example code properly. If I make the array as large as it'll ever need to be, I can keep a separate int that says how many spaces in the array are actually filled, right?
Sure, that is certainly possible.
robbieduncan is offline   0 Reply With Quote
Old Jul 23, 2011, 01:06 AM   #7
seepel
macrumors 6502
 
Join Date: Dec 2009
If the levels need to get more complicated than just some lines that you can look at LevelSVG to create the levels. And if you are making a 2D game I can't recommend highly enough the Cocos2D framework, you should definitely check it out.
__________________
Free iOS App that turns your music library into an info graphic.
Phonographic
seepel is offline   0 Reply With Quote
Old Jul 23, 2011, 06:22 AM   #8
gladoscc
macrumors regular
 
Join Date: Jul 2011
If you are making a tile based game, divide your x and y by the size of the object and floor it. Check if that tile is walkable. That doesn't get more slower the more walls you have.

Tile based doesn't mean you can only move one square at a time, Super Mario Bros is tile based.

As you say you are making a 2d platforms game, depending on the situation you might want to make it tile based. Easier level design, gives a retro feel if you are aiming for that
gladoscc is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Xcode SpriteKit SKPhysics Collisions not Working PrathmeshD iPhone/iPad Programming 5 Apr 18, 2014 02:31 AM
How to check log files to date/time system RAM was changed? Any way to check this? asterix2k10 OS X 1 Mar 7, 2014 08:19 PM
The I may go to bed, and just check back in tomorrow to check my order status thread lsutigerfan1976 iPhone 0 Sep 14, 2012 10:50 PM
Check List++ (World most intuitive and fastest check list) - Free App appinitpro iPhone and iPod touch Apps 5 Jun 15, 2012 11:31 PM

Forum Jump

All times are GMT -5. The time now is 09:14 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC