Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  A good game architecture and design  (Read 2649 times)
0 Members and 1 Guest are viewing this topic.
Offline vbrain
« Posted 2013-07-08 19:59:30 »

I've been searching for weeks for a good game design and architectural approach. I need a great way to manage entities without having to loop through everything.

I'm developing a Real-time Strategy and I have been trying to figure out how to have the entities communicate and attack without having to loop through everything. I am using a grid manager where every entity is in a cell, then I select all the entity's neighbors to determine who to shoot. However, if there are a total of 50 entities in the nearby cells, and I have them all loop over each other and shoot, the game begins to lag. I'm trying to find an approach that doesn't utilize too many loops.

I have read many articles but so far none give a clear answer.
Please don't mention using an entity manager like Artemis, because I would still have to loop through everything.
Offline HeroesGraveDev

JGO Kernel


Medals: 269
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-07-08 20:02:10 »

You'll find in many RTS's, that unit only attack when a) instructed, or b) are attacked themselves.

Offline vbrain
« Reply #2 - Posted 2013-07-08 20:03:55 »

You'll find in many RTS's, that unit only attack when a) instructed, or b) are attacked themselves.

Yes, I have played a few. The units in my game attack automatically when an enemy comes near them. I need someone with a good method to tell me how to go about making my game efficient.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #3 - Posted 2013-07-08 20:42:59 »

Use a profiler to find out the slow parts. Loops do not imply slowness.
One approach could be an event driven system, another to cut the number of processed entities for each frame / switch the entity chunk to process.
But thats all guessing.

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #4 - Posted 2013-07-09 00:04:40 »

Is your game multithreaded?

Maybe try putting the logic for checking targets into threads and run them simultaneously per entity?

Offline Shnagenburg

Junior Newbie





« Reply #5 - Posted 2013-07-11 17:10:41 »

Do units only attack the closet enemy within range? If so you may want to try adjusting your cell size to remove unneeded collision checks (a collision being an enemy collides with a units sight circle). Start with cells that are very small then expand outward until you get a candidate. Is it the case that units are only going to change their attack target if someone in those cell sets moves? An easy optimization is to only check for a new target if the nearby enemies have moved. Be sure your units are only checking the distance to relevant entities, such as not people on the same team. If you're using the distance formula to check who to attack, keep in mind that sqrt() is more expensive than just using radius^2 for everything.
Offline lcass
« Reply #6 - Posted 2013-08-22 20:44:46 »

Id say that you would either have to have them as seperate objects or use polymorphism to make an entity super  that has a tick(){}; and then the object itself handles the rest. there isnt really a better way to see if your troops can do anything other than make them check.
Offline Grunnt

JGO Wizard


Medals: 84
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #7 - Posted 2013-08-22 21:52:36 »

However, if there are a total of 50 entities in the nearby cells, and I have them all loop over each other and shoot, the game begins to lag.

50 entities looping over 50 nearby other entities = 50 * 50 = 2500 decisions who to shoot each update. This is assuming that each entitiy will shoot each and every update loop, which is of course not the case I assume. These numbers are extremely low, and it does not make sense at all that this should cause lag.

Without seeing any code I can just say: do some profiling to find out why this is so slow.

Offline lcass
« Reply #8 - Posted 2013-10-01 20:46:16 »

Store the x and y of enemys in an array . Scan this array and then check the x and y (usin pythag) if the distance is less than their range kabloowy attack. Yes do put this in a seperate loop.
Offline philfrei
« Reply #9 - Posted 2013-10-02 02:34:04 »

If you have 50 entities each in two neighboring cells, are all 100 engaged? or only those closest to each other?

A couple ideas (hard to know without seeing the decision-making code itself, or a better description of the decision algorithm):

1) if you are using distance, remember that distances can be ranked without having to calculate square-roots. (X1-X2) * (X1-X2) + (Y1-Y2) * (Y1-Y2) is sufficient for ranking--no need to get a square root of this if the only purpose is ranking.

2) maybe there should be "sub-cells" of some sort within the cells?

But as several people have pointed out already, if the algorithm is efficient, you can brute force check many more entities against each other than just 50 against 50, so there's probably something in the decision logic that can be honed out.

"It's after the end of the world! Don't you know that yet?"
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-16 18:59:02

Norakomi (40 views)
2014-10-16 06:22:06

Norakomi (31 views)
2014-10-16 06:20:20

lcass (36 views)
2014-10-15 07:18:58

TehJavaDev (67 views)
2014-10-13 15:39:48

TehJavaDev (65 views)
2014-10-13 15:35:47

TehJavaDev (57 views)
2014-10-13 15:32:37

BurntPizza (72 views)
2014-10-11 14:24:42

BurntPizza (44 views)
2014-10-11 14:10:45

BurntPizza (84 views)
2014-10-11 13:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 13:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 13:36:02

List of Learning Resources
by Longor1996
2014-08-16 01:40:00

List of Learning Resources
by SilverTiger
2014-08-05 10:33:27

Resources for WIP games
by CogWheelz
2014-08-01 07:20:17

Resources for WIP games
by CogWheelz
2014-08-01 07:19:50

List of Learning Resources
by SilverTiger
2014-07-31 07:29:50

List of Learning Resources
by SilverTiger
2014-07-31 07:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!