Jump to content
• ### What is your GameDev Story?

• Advertisement

# 2D game mechanic question

This topic is 3104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hi!

I want to create a game in which rocks fall from the sky randomly in different positions and at different times?

Well I know a simple way to do that, but if I try to use a big number of rocks the game starts to lag...

So how can I do this dynamically?

#### Share this post

##### Share on other sites
Advertisement
There's no way to answer the question with the information you've provided. If you want help with this, you'll have to provide quite a bit more info about the problem (e.g. platform, programming language(s), API(s), how many rocks is a 'big number', etc.).

#### Share this post

##### Share on other sites
Ok...
Platform: Android OS
Programming Language: Java
Big Number: 1000

#### Share this post

##### Share on other sites
Could you outline in detail your game loop and rock-updating algorithm? do the rocks have collision with eachother?

#### Share this post

##### Share on other sites
When user starts the game, all rocks positions are generated with coordinates like x = 5 ; y = -5 or x = 50 ; y = -1000.
Then in game loop, game keep increasing the y value of each rock so they will appear on the screen after a certain number of frames.

This is really expensive because I'm calculating the position of each rock each frame, even for rocks that aren't even visible to the player...

So I want to know the best, or one of the best, techniques to do this dynamically, like only updating the position of visible rocks...

#### Share this post

##### Share on other sites
Reconsider what you are doing; the 'y' value acts as a delay, so why not make that delay time based and only add rocks to be processed when it is time for them to become active.

#### Share this post

##### Share on other sites
Perhaps lower the number of rocks and only drop them around the player.

So if you have a large world and drop 1000 rocks. Limit it to, like, 50 rocks. And only drop them in the space around the character. So if the screen is like 200x400 (I have no idea what the androids screen is like) drop them in a 600x1200 area around the player, so that all sides are surrounded by falling rocks and the player isn't able to run really fast and dodge them.

+----------------+|  Falling Rocks ||    +------+    ||    |Player|    ||    |      |    ||    |      |    ||    +------+    ||                |+----------------+

Also to cut memory operations (I don't know exactly what your doing), don't create a new rock everytime one needs to fall. If a rock collides with the ground, don't delete and create a new one, simply move it back into the sky and drop it again. This way the phone won't have to allocate and delocate memory so fast.

#### Share this post

##### Share on other sites
1 don't use multiple thread.
2 try to depart the drawing code from other code, make it independent.

#### Share this post

##### Share on other sites
Sometimes the performance problems of your game is dued to the bad design.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 13
• 9
• 15
• 14
• 46
• Advertisement
• ### Forum Statistics

• Total Topics
634058
• Total Posts
3015287
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!