Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / projectile collisions on: 2011-12-17 07:17:15
The problem: I have up to 20 ish npcs and 20 ish projectiles on screen at a time; how do i check for collisions between any of them?

Some info: I'm extremely inexpierenced in the game dev world and i only just figured out a haxy way to make and draw a tile map platformer. My current 'collision detection' is literally just having player only add x_speed to xpixelposition if Main.map.getMapArray[ytile][xtile+ 1] is greater then 2(cos 0 1 2 are all unwalkable). The same thing goes for projectile.

basically how would i check if if ANY npc was colliding with ANY bullet? that would take way to long with my current method, yet i always see people doing collision detection like this, hell even mario did this without trouble.

The way im doing it now is something like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
Projectile bullet[20] = new Projectile[20];
NPC n[] = new NPC[20];

for(blah){
spawn npcs based on grid
}
if spacebar is pressed{
for(blah){
if(!bullet[i].isActive){
 set it to active and change it's coords to player.xtile,player.ytile;
 break;
}
}
main loop{
for(blah){
//update bullet[i] if bullet[i].isActive is true
if(bullet[i].isActive){
bullet[i].update();
}
}
for(blah){
npc[i].update();
}
p.move();
if(mapNeedsUpdating){
map.update();
}
repaint();
}

then in bullet I would have something like
1  
2  
3  
4  
5  
6  
if(isActive) 
for(blah){
check if I'm colliding with npc[i];
if i am, destroy the npc and me
}
else move me over one

so i have a feeling that that would be EXTREMELY inefecient. If i had all 20 bullets on screen, they would EACH check if they were colliding with ANY npc every time they moved at all(so that's 20 loops each containing 20 loops plus the player updating and the map updating and the npc updating and the drawing all that stuff). but idk how im supposed to do it if i cant do that..
Like I said I'm very new to this, in fact I only really got into this on monday when I was informed that I signed up to make a game for my senior project. Up until now the only ideas I've been able to get are ones that I think of during calculus and write down before i forget, it's a miracle I made it this far.
2  Discussions / General Discussions / Re: Tutorials on: 2011-12-17 04:46:06
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
import java.applet.*;
import java.awt.*;

public class SampleThread extends Applet implements Runnable
{
   Thread t;
   int i;
   
   public void init()
   {
      t = new Thread();
      t.start();
     
      i = 0;
   }
   
   public void run()
   {
      while(true)
      {
         i++;
         repaint();
         
         try{
            t.sleep(1000);
         } catch (InterruptedException e) { ; }
      }
   }
   
   public void paint(Graphics g)
   {
      g.drawString("i = "+i, 10, 20);
   }
}


Thanks to anyone who can help me!


Try this code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
import java.applet.*;
import java.awt.*;

public class SampleThread extends Applet implements Runnable
{
   /**
    *
    */

   private static final long serialVersionUID = 1L; //This is a default ID
  Thread th;
   int i;
        //Initialize vars and such...
  public void init()
   {
      th = new Thread();
      i = 0;
   }

       //Start the Thread...
  public void start() {
      th = new Thread(this);
      th.start();
   }

   public void stop() { //Stopping a thread is important, and destroy() is deprecated so we use this instead
     th = null;
   }

   @Override
   public void run() {
      Thread thisThread = Thread.currentThread();

      while (th == thisThread) {//just makes sense there, if th is null (say, if we wanted it to stop by using stop(),
               //or if it was null because you closed the application straight up) then it wont equal thisThread
                //and it will stop without giving you an error
        i++;
         repaint();

         try{
            Thread.sleep(1000);
         } catch (InterruptedException e) {
                             break; //why didn't you have this there in the first place?
                       }
      }
   }

   public void paint(Graphics g)
   {
      g.drawString("i = "+i, 10, 20);
   }
}

If that doesn't work it's gotta be something wrong with the HTML or something else...
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

ctomni231 (37 views)
2014-07-18 06:55:21

Zero Volt (35 views)
2014-07-17 23:47:54

danieldean (28 views)
2014-07-17 23:41:23

MustardPeter (31 views)
2014-07-16 23:30:00

Cero (46 views)
2014-07-16 00:42:17

Riven (47 views)
2014-07-14 18:02:53

OpenGLShaders (36 views)
2014-07-14 16:23:47

Riven (36 views)
2014-07-14 11:51:35

quew8 (32 views)
2014-07-13 13:57:52

SHC (69 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!