Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
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  
  Need some minor help in fixing/implementing some code!  (Read 658 times)
0 Members and 1 Guest are viewing this topic.
Offline RyanMB97

Junior Member


Medals: 1
Projects: 1



« Posted 2013-03-03 00:27:52 »

Hello! I'm relatively new to the Java scene, but I've studied most of the important stuff, how it works, etc. This is my first "Game", as in with animations/sprites/what have you. I'm having a bit of trouble implementing my "kill mobs" style.

In brief, I have a "bullet" arraylist (of bullet objects, obviously), and a mob arraylist. In my "tick" method, I have iterators that are ticking and checking for collisions. The ticking works, but the collision doesn't. Me and one of my friends have been puzzling over this the past day or 2, and we're no closer to finding out what's wrong. So, I thought of asking you guys for help!

Here's the github link: https://github.com/RyanMB97/SideScroller.git and thanks in advance Cheesy
P.S. The problem is in the "tick" method of "Game".
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2013-03-03 00:31:00 »

Could you state specifically whats wrong? Not many people want to read through all that code!

Offline RyanMB97

Junior Member


Medals: 1
Projects: 1



« Reply #2 - Posted 2013-03-03 00:33:53 »

Right, sorry! My main problem is getting the collision to work. For some reason, the bullets will collide with 1 or 2 of the mobs, but not the others. The thing is, it should be checking against every single last one of the mobs/bullets for collisions. Here's the spot in focus:

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  
Iterator<Bullet> bulletIterator = bulletList.iterator();
      Iterator<Mob> mobIterator = mobList.iterator();

      while (bulletIterator.hasNext()) {
         Bullet bullet = bulletIterator.next();
         bullet.tick();
         if (!bullet.living) {
            bulletIterator.remove();
         }
      }

      while (mobIterator.hasNext()) {
         Mob mob = mobIterator.next();
         mob.tick();
         if (!mob.living) {
            mobIterator.remove();
         }
      }

      bulletIterator = bulletList.iterator(); // Re-creates the bullet iterator after removing dead bullets
     mobIterator = mobList.iterator();// Re-creates the mob iterator after removing dead mob

      while (bulletIterator.hasNext()) {
         Bullet bullet = bulletIterator.next();
         while (mobIterator.hasNext()) {
            Mob mob = mobIterator.next();

            if (bullet.intersects(mob)) {
               mob.living = bullet.living = false;
               System.out.println("Intersection!");
            }

            if (!mob.living) {
               mobIterator.remove();
            }
         }
         if (!bullet.living) {
            bulletIterator.remove();
         }
      }


Like I said, it's only colliding with some of the mobs that appear, and going completely through other mobs when it should be "Destroying" them.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online actual

JGO Coder


Medals: 23



« Reply #3 - Posted 2013-03-03 00:35:41 »

Slow your bullets down and see if the problem persists. Unless you are doing interpolation, the bullet could be passing completely through your target in a single tick which would mean it would never actually register a collision.
Offline RyanMB97

Junior Member


Medals: 1
Projects: 1



« Reply #4 - Posted 2013-03-03 00:40:54 »

That's not the issue. Both the mobs and "bullets" are moving 1 pixel per tick, tied into the main game loop. I can clearly see the bullets going through the mobs, but not colliding. The bullet is physically inside of the mob, and it's not destroying itself. The strange thing is that some of the mobs DO destroy themselves, while the bullet just skips through others.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2013-03-03 01:42:40 »

I would suggest using the distance formula (Math.abs(bullet.x - mob.x) + (bullet.y - mob.y) I believe that's correct). It will allow you to create your own collision system, and it isn't that hard to implement. While
1  
object.intersects(object)
isn't a bad way of handling small collisions in small programs, you will definitely need to create your own collision system later on to handle different types of collisions and what happens depending on how you collide. Search AABB on this site, there are at least two tutorials that I've seen on how to implement AABBs.
Good luck and here's to no bugs!  Cheesy

Offline sproingie

JGO Kernel


Medals: 202



« Reply #6 - Posted 2013-03-03 01:53:57 »

I would suggest using the distance formula (Math.abs(bullet.x - mob.x) + (bullet.y - mob.y)

That's Manhattan Distance.  Cartesian distance requires you square each term and take the square root, though you can skip the square root step and just compare against the square of the distance you're testing against.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #7 - Posted 2013-03-03 02:11:09 »

I would suggest using the distance formula (Math.abs(bullet.x - mob.x) + (bullet.y - mob.y)

That's Manhattan Distance.  Cartesian distance requires you square each term and take the square root, though you can skip the square root step and just compare against the square of the distance you're testing against.

Ahh thank you! I always get them mixed up!

Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #8 - Posted 2013-03-03 12:42:12 »

the collision method you used is fine. the problem is these iterators (im not familiar with these but i hope what i say is right).

you create these two iterators, then you loop through the bullets. then you loop through all the mobs, but you never recreate the iterator for mobs.
when you come to check the second bullet in the list of bullets, (mobIterator.hasNext())  is now false because you've been through them all and none are left. this is why only the first bullet can collide with mobs.

so adding    mobIterator = mobList.iterator();   right before you cycle through the mob, between lines 173-174, should solve.. i hope..

gl
Offline RyanMB97

Junior Member


Medals: 1
Projects: 1



« Reply #9 - Posted 2013-03-03 13:19:32 »

JESTERRRRRR, you are amazing! Thanks for pointing that out, I feel like an idiot now for not realizing that Tongue. Thanks to everyone else who responded, I'm sure that if Jester hadn't posted, I'd have been able to do it with one of your methods. Thanks a bunch Cheesy.
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.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!