Discussion for Tutorial 10 - Game Structure

Discussions for all the iPhone Screencast Tutorials.

Discussion for Tutorial 10 - Game Structure

Postby jonny » Sun Aug 23, 2009 3:20 am

You will be pleased to know that the first part of tutorial 10 is now available for viewing. It’s taken me much longer than I would have liked to get this ready, but I’m glad that at least part 1 is out.

This part of the tutorial covers the changes to the project structure I have made along with how I am managing different game scenes such as the main menu and core game scene. It also covers some of the changes I’ve made to the Image class etc.
jonny
Site Admin
 
Posts: 11
Joined: Wed Apr 22, 2009 4:50 am

Re: Discussion for Tutorial 10 - Game Structure

Postby rookieStudio » Sun Aug 23, 2009 6:42 am

Good job on this forum. Keep the tutorial coming. Thanks! :D
rookieStudio
 
Posts: 1
Joined: Sun Aug 23, 2009 6:39 am

Re: Discussion for Tutorial 10 - Game Structure

Postby Elphaba » Mon Aug 24, 2009 12:26 am

Mike.

I have a question about your code... I've been looking at this all day and can't for the life of me work out what you are doing and why it works..

Code: Select all
// Macro which returns a random value between -1 and 1
#define RANDOM_MINUS_1_TO_1() ((random() / (GLfloat)0x3fffffff )-1.0f)

// MAcro which returns a random number between 0 and 1
#define RANDOM_0_TO_1() ((random() / (GLfloat)0x7fffffff ))


I mean, why the hex? Why those values? Why does this work?

Surely this makes more sense and would work much better:

Code: Select all
#define RANDOM_0_TO_1() ((arc4random() % 100 ) / 0.01f)



No?
Elphaba
 
Posts: 6
Joined: Sun Aug 23, 2009 3:15 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby mike » Mon Aug 24, 2009 10:42 am

Hi Elphaba

That's not code I've looked at in a while and if I remember rightly it was code borrowed from Cocos2d when I was working on my particle emitter.

Using the hex is just a cleaner way of holding a big number. 0x7fffffff is in decimal 2147483647 which is 2^31, so the macro is just taking the value from random() and dividing it by a big number. I don't see anything wrong with your approach either, other than more operations would be necessary to work it out. That is why I think they are just dividing random by 0x7fffffff. This macro ends up getting run a lot in the game loop by things like the particle emitter so having it take as few cycles as possible is a good thing.

That's about all I can say on it really, it was a case of it worked and so I didn't look at it again, but good question :D

Mike
mike
 
Posts: 108
Joined: Fri Aug 21, 2009 2:10 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby Elphaba » Mon Aug 24, 2009 11:55 am

Thank you again Mike for taking the time to answer.
Elphaba
 
Posts: 6
Joined: Sun Aug 23, 2009 3:15 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby Tony » Tue Aug 25, 2009 6:15 pm

I was looking at this last week after getting the same 'random' numbers over and over. After some googling, I changed everything to arc4random. It gave much better results.

Here are a couple of posts on the subject:

http://discussions.apple.com/thread.jsp ... ID=1590314

http://iphonedevelopment.blogspot.com/2 ... andom.html.

Tony
Tony
 
Posts: 16
Joined: Sun Aug 23, 2009 10:32 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby Elphaba » Tue Aug 25, 2009 6:20 pm

Yes.

Random() will ALWAYS give repeatable results, unless you seed 'srand' with time.

However, even so, the 'randomness' of the 'Random()' statement is much poorer than the 'arc4random()' statement.

I can't speak if 'my' line of code is more efficient / quicker that the ones in the Tutorial, but you don't need to keep re-seeding and also get a much better 'random' number.

Maybe that's worth the 'possible' slight loss of efficiency.
Elphaba
 
Posts: 6
Joined: Sun Aug 23, 2009 3:15 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby Tony » Tue Aug 25, 2009 6:32 pm

It's definitely worth the slight loss.
Tony
 
Posts: 16
Joined: Sun Aug 23, 2009 10:32 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby mike » Wed Aug 26, 2009 12:25 pm

I am also going to try out arc4Random() and see what happens in terms of performance. Even though it may need a few more ticks, having more randomness than random() would be a real advantage.

If I see any performance problems I'll report back but I am expecting things to be fine given the load I'm seeing in my current game project :)

Thanks for the info chaps, very useful.

Mike
mike
 
Posts: 108
Joined: Fri Aug 21, 2009 2:10 pm

Re: Discussion for Tutorial 10 - Game Structure

Postby Elphaba » Thu Aug 27, 2009 2:32 am

And I'm an idiot.

My code had an obvious typo.

It should have been / 100.0f rather than / 0.1f

D'oh!

So it all should look something like this...

Code: Select all
#define RANDOM_MINUS_1_TO_1() ( (( arc4random() % 100 ) / 100.0f ) - 1.0f )

#define RANDOM_0_TO_1() ( ( arc4random() % 100 ) / 100.0f )

#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI )



HOWEVER, I would suggest replacing all DIVIDES with the appropriate MULTIPLYS because usually (and esp. on Intel chips - not so sure about ARM but it's very likely) MULTIPLYS are less operationally expensive than divides. So always use a * instead of a / where you can.

So the 'optimal' code would be the following

Code: Select all
#define RANDOM_MINUS_1_TO_1() ( (( arc4random() % 100 ) * 0.01f ) - 1.0f )

#define RANDOM_0_TO_1() ( ( arc4random() % 100 ) * 0.01f )

#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI )
Elphaba
 
Posts: 6
Joined: Sun Aug 23, 2009 3:15 pm

Next

Return to iPhone Game Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron