Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
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  
  projectile collisions  (Read 1045 times)
0 Members and 1 Guest are viewing this topic.
Offline pyro sauce

Junior Newbie





« Posted 2011-12-17 06: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.
Offline Cero
« Reply #1 - Posted 2011-12-17 16:49:13 »

Quote
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

thats usually how you do it with small arrays
when you need more performance use quad trees or similar

meaning you divide parts of the screen / map into areas
can reduce the amount of checks

example: if all NPCs are in area 2 and the bullets are in like 5 and 6, you just have to check the areas

Offline ReBirth
« Reply #2 - Posted 2011-12-18 02:36:45 »

From what I learned
1  
2  
3  
for (i=0; i<size; i++)
   for (r=i; r<size; r++)
      //check collision of element[i] and element[r]

works well on small amount (below 1000).

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2011-12-18 14:14:26 »

From what I learned
1  
2  
3  
for (i=0; i<size; i++)
   for (r=i; r<size; r++)
      //check collision of element[i] and element[r]

works well on small amount (below 1000).

1  
2  
3  
for (i=0; i<size; i++)
   for (r=i+1; r<size; r++)
      //check collision of element[i] and element[r]

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ReBirth
« Reply #4 - Posted 2011-12-18 21:57:26 »

Thanks Riven, miss that one Tongue

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.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (26 views)
2014-10-26 10:14:04

theagentd (32 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

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

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

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

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

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

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

List of Learning Resources
by SilverTiger
2014-07-31 16: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!