Project Perfect Mod Forums
:: Home :: Get Hosted :: PPM FAQ :: Forum FAQ :: Privacy Policy :: Search :: Memberlist :: Usergroups :: Register :: Profile :: Log in to check your private messages :: Log in ::


The time now is Thu Mar 28, 2024 6:14 pm
All times are UTC + 0
Question to programmers(algorithm help)
Moderators: Community Tools Developpers
Post new topic   Reply to topic Page 1 of 1 [4 Posts] Mark the topic unread ::  View previous topic :: View next topic
Author Message
¥R_M0dd€r
Cyborg Soldier


Joined: 03 Jan 2011

PostPosted: Mon Nov 19, 2012 12:14 am    Post subject:  Question to programmers(algorithm help) Reply with quote  Mark this post and the followings unread

I need some help with a few lines of code. People familiar with Java, C or C++(and probably more) can help out here.

So, I am making a game, a copy of the infamous game called World Hardest Game.
Every time a MovableObject(in most cases, this is an enemy) moves a step, a function is called to see if this enemy have touched the main character(which means the game is over/you died).

Example:

In this case, the function would have returned true, which means the main character is colliding with the enemy.
The red one is the character you play as, and the green one is an enemy, or the opposite(does not matter).

I have written a piece of code that works, but the code is slow as hell and cause immense lag if you have more than ten objects.
My code:
Code:
   /**
    * Checks if the movable object is touching the main character.
    * @return True if the object and the main character are interacting, otherwise, false.
    */
   public boolean isColliding(MovableObject object)
   {
      for (int y = 0; y < height; y++)
         for (int x = 0; x < width; x++)
            for (int oy = 0; oy < object.height; oy++)
               for (int ox = 0; ox < object.width; ox++)
                  if(posY + y == object.posY + oy && posX + x == object.posX + oy)
                     return true;
      return false;
   }

Variables height and width is the height and width of the character/enemy. posY and posX is the current position of the object(the upper-left corner).

Can someone please give me a code that is super fast?

Back to top
View user's profile Send private message
Lin Kuei Ominae
Seth


Joined: 16 Aug 2006
Location: Germany

PostPosted: Mon Nov 19, 2012 7:33 am    Post subject: Reply with quote  Mark this post and the followings unread

You do many unnecessary comparisons by going through every single tile of the game board. Instead compare only the positions of each object against the maincharacter position.
This way you reduce the amount of comparisons from more than 100 down to 10.

hold every single object in a list/array
//go through each object
for (int i=0; i<objects.length; i++)
{
if (object.x==maincharacter.x)&&(object.y==maincharacter.y) return true;
}

Maybe also search in other forums which specialize in game coding. There are surely more efficient ways like using matrices.

_________________
SHP Artist of Twisted Insurrection:  Nod buildings

Public SHPs
X-Mech Calendar (28 Mechs for GDI and Nod)
5 GDI, 5 Nod, 1 Mutant, 1 Scrin unit, 1 GDI building

Tools
Image Shaper______TMP Shop______C&C Executable Modifier

Back to top
View user's profile Send private message
Banshee
Supreme Banshee


Also Known As: banshee_revora (Steam)
Joined: 15 Aug 2002
Location: Brazil

PostPosted: Mon Nov 19, 2012 9:40 am    Post subject: Reply with quote  Mark this post and the followings unread


Back to top
View user's profile Send private message Visit poster's website Skype Account
¥R_M0dd€r
Cyborg Soldier


Joined: 03 Jan 2011

PostPosted: Tue Nov 20, 2012 5:50 am    Post subject: Reply with quote  Mark this post and the followings unread

Thanks Banshee. Great article.
Got it working without a for-loop.

I can have about 250 objects now without lag, which is more than enough for a game like this.

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] Mark the topic unread ::  View previous topic :: View next topic
 
Share on TwitterShare on FacebookShare on Google+Share on DiggShare on RedditShare on PInterestShare on Del.icio.usShare on Stumble Upon
Quick Reply
Username:


If you are visually impaired or cannot otherwise answer the challenges below please contact the Administrator for help.


Write only two of the following words separated by a sharp: Brotherhood, unity, peace! 

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © phpBB Group

[ Time: 0.1444s ][ Queries: 11 (0.0063s) ][ Debug on ]