Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Raycasting in 2D  (Read 6025 times)
0 Members and 1 Guest are viewing this topic.
Offline Swattkidd7

Junior Member





« Posted 2008-11-08 22:47:41 »

Hey everyone, I am creating a 2D top down shooter but am having trouble with collision detection.

I am at the point in the game where the player can shoot but bullet to environment collision isnt going so well. So far I have it so that the parts of the tilemap that entities can collide with are stored in an arraylist of Rectangles representing those cells, and am seeing if the bullet rectangles intersect with them, and if they do, i remove the bullet.

However the problem comes in when the bullets move really fast like they should, even when they move fairly slow actually, but basically what happens is the bullets go through the walls, so if an enemy is on the other side of the wall, it still has a chance of being hit.

I heard from others that for things like this I should use raycasting, however I am not really sure where to start with that. I know that the point of raycasting is to draw a line going from the tip of the gun, toward the direction its facing, and seeing if it every collides with any of  Rectangles, or "forbidden" tiles. However I am not sure about how to go about implementing this.

Also, if i do implement this, I wouldnt need bullets right? Just a flash at the muzzle and maybe a collision drawing, but nothing in between since the bullets would be moving so fast, im sure thats just prefrence though..

Links, help, or anything is appreciated Smiley
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2008-11-08 23:20:52 »

The easiest way out is to interpolate your bullet's positions between the updates, to such a small interval that it gets in the sub-pixel accuracy. At every interval you check whether it is inside those rectangles.

The other way is to do true physics... which will be a lot of work, yet faster to calculate.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Swattkidd7

Junior Member





« Reply #2 - Posted 2008-11-09 00:39:02 »

The easiest way out is to interpolate your bullet's positions between the updates, to such a small interval that it gets in the sub-pixel accuracy. At every interval you check whether it is inside those rectangles.

The other way is to do true physics... which will be a lot of work, yet faster to calculate.

Yea I think i will have to pass on the physics, but am not sure if I understand what you mean. Are you saying, instead of doing raycasting, just update the bullets position by an extremely small amount so that it checks almost every position and wont be able to move 5-10 pixels through the wall?

But wouldnt that effect bullet speed? like wouldnt the bullet move extremely slow?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2008-11-09 01:14:46 »

Check this out:

http://www.java-gaming.org/topics/damage-formula/19328/view.html

See my work:
OTC Software
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #4 - Posted 2008-11-09 01:29:26 »

If you want fast moving bullets then you just need to do "swept" collision detection each frame rather than just testing the new position. For bullets that means taking the last frame's position and the new position and creating some sort of collision geoemtry out of that - line segments work well, or capsules (line segments with a radius) are nice too. Much more efficient and less hacky than doing a fixed number of intermediate steps.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline CommanderKeith
« Reply #5 - Posted 2008-11-09 02:12:46 »

Yeah line-line intersection is one of the best ways, where the line goes from the back of bullet's last position, to the front of its new position. After finding all bodies that intersect with the line, you then test which entity is the closest to the start of the line, and collide the bullet with that entity.

Offline Swattkidd7

Junior Member





« Reply #6 - Posted 2008-11-09 08:09:23 »

Wow, thanks guys for all the replies, im gonna look into them. Also, when you guys say I should do some type of "swept" collision by using a line from the last position of the bullet to the front, are you saying I should have something like.. A function that will contain a last position and new position of the bullet, which will be used to create that line, then it will check and see if that line intersects with anything, and if it does, check to see what it hit, and based on that do what it needs to?
Offline CommanderKeith
« Reply #7 - Posted 2008-11-09 10:09:43 »

Yep, that's what I do. Each bullet has an oldBackX and Y.

Offline Swattkidd7

Junior Member





« Reply #8 - Posted 2008-11-09 10:19:56 »

Yep, that's what I do. Each bullet has an oldBackX and Y.
Nice, thanks man! Ill give it a shot, especially since its what you do and I really enjoyed playing your 2D multiplayer shooter lol, really is kind of fun, although couldnt figure out how to get the sniper,but yea its an awesome display of what your engine can do!
Offline CommanderKeith
« Reply #9 - Posted 2008-11-09 10:38:39 »

oh cheers! well if you think that's good, you should see what others here can do.

good luck with your 2d shooter, let us know how your progress is going on the forums  Cool

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

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2008-11-09 10:45:44 »

My solution regarding reducing the interval is the most simple to implement, that's why I suggested it. It is hardly performant. Anyway, it's amazing how poorly performing code is 'forgiven' by todays incredible CPU speeds.

To answer your question: no, the bullets would not have to move incredibly slow, you just need to do the check N times per frame, instead of 1 time per frame.

Anyway, since a rectangle is composed of 4 lines, and the moving bullet is 1 line, a line vs line collision check is rather simple to implement, and will make your engine more scalable, because performance will be much better.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #11 - Posted 2008-11-09 18:53:19 »

Well the question is how do you do line vs line collision testing if you don't know the level's geometry? If your game is like Worms (the level is a bitmap, not a bunch of geometry) then your only option is to do the pixel-by-pixel method. And yeah, if the rest of your game isn't incredibly complicated this collision method works absolutely fine, basically because processor speeds are so incredibly high now.

See my work:
OTC Software
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2008-11-09 18:54:18 »

Well the question is how do you do line vs line collision testing if you don't know the level's geometry?

You might want to read the first post.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Swattkidd7

Junior Member





« Reply #13 - Posted 2008-11-14 10:04:14 »

Hey everyone, sorry I dont have much progress to tell about with my collision detection, but of course I have another question Tongue

How can we use a directional vector in Java? Is there anything built right in? I remember using something a while back but am not 100% sure what it was and how.

Also, should i store all collidable rectangles in one arraylist? because right now, i have an arraylist of Rectangles representing the tiles on the map that can be collided into, but for the entities, i loop through the ArrayList of entities and then use a getRect method to get theirs..because otherwise, i have to have like 2 or 3 nested for loops, looping through every bullet, then every entity, then ever collidable rectangle..
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2008-11-14 17:00:54 »

You can make an interface:

1  
2  
3  
4  
public interface BoundingBox
{
   public Rectangle getBoundingBox();
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public Wall implements BoundingBox
{
   private final Rectangle bounds;

   public Wall(Rectangle bounds)
   {
      this.bounds = bounds;
   }

   public Rectangle getBoundingBox()
   {
       return bounds;
   }
}


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  
public Player implements BoundingBox
{
   private int x, y;
   private boolean dirty;
   private final Rectangle bounds;

   public Player(Dimension dim)
   {
      this.bounds = new Rectangle(dim);

      this.setPosition(0, 0);
   }



   public void setPosition(int x, int y)
   {
      this.dirty= (this.x != x) || (this.y != y);
      this.x = x;
      this.y = y;
   }

   public Rectangle getBoundingBox()
   {
       if(this.dirty)
       {
            this.bounds.x = this.x - this.bounds.width/2;
            this.bounds.y = this.y - this.bounds.height/2; // either this for vertically centered box
           this.bounds.y = this.y - this.bounds.height; // ...or this, which is what you probably need

            this.dirty= false;
       }
       return bounds;
   }
}


Then add all your Walls AND Players (and many more types) in the same List<BoundingBox>. Now you can iterate over that list, regardless of the types.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #15 - Posted 2008-11-14 17:13:08 »

You might want to read the first post.
Yeah I know he doesn't need to, I was saying in general. If you don't know geometry that's one reason to use pixel-by-pixel collision.

See my work:
OTC Software
Offline Swattkidd7

Junior Member





« Reply #16 - Posted 2008-11-14 21:50:49 »

Whoo! Thanks for that Riven! much more efficient then what im doing persecutioncomplex

Although not sure what the "dirty" boolean is used for, and I think i am just going to have to use a list of rectangles (list<Rectangle>) because my tilemap is stored in a map that my engine calls up, and then i use a method the gives the game an ArrayList of all the rectangles that are collidable on that certain map....

Also, when i find out if the bullet did collide with something, then i can just use instanceof, to find out with what and what to do right?

but once again thanks!
Offline Swattkidd7

Junior Member





« Reply #17 - Posted 2008-11-18 03:56:32 »

Ok, A lot of progress has been made, Bullet collison is pretty good at the moment, I used the method of just checking the bullets position more than once per update, and I used the BoundingBox method for item collision.

So thank you all for those suggestions and advice!!

Also, I have implemented a Weapon class, and LUA scripting so i can easily create many different types of weapons with lua files, rather than hard coding them all in, which was actually fairly simple to implement and very helpful!

However I have another question, for enemy AI (similar to how ComanderKeith did it in his multiplayer shooter "demo" of his engine) should that be included in LUA files or should I just hard code that in? I appreciate any answers like to my previous questions because it really helps me get a view of all the different ways to implement things!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #18 - Posted 2008-11-18 07:43:17 »

You can also really easy use Java's JIT to add in Java code as external scripts for weapons. I've done that and it works well, plus it's more powerful than something like LUA.

See my work:
OTC Software
Offline CommanderKeith
« Reply #19 - Posted 2008-11-18 07:52:37 »

You can also really easy use Java's JIT to add in Java code as external scripts for weapons. I've done that and it works well, plus it's more powerful than something like LUA.

Do you mean by using Janino?

http://www.janino.net/

I was thinking of using this in my next game, it looks really really good.



However I have another question, for enemy AI ...


Enemy AI need not be that complicated by the way. Like the bots can just do laps of way points until they see an enemy that is unobstructed, then they stop and shoot. That seems to work well and is pretty fun, I've seen other people use it too.

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #20 - Posted 2008-11-18 10:53:14 »

Do you mean by using Janino?

http://www.janino.net/

I was thinking of using this in my next game, it looks really really good.

There's also <a href="http://www.beanshell.org/">Beanshell</a>, or if you're using 1.6 then there's the new Compiler API. Although if you're not set on using the java syntax then there are nicer options like Jython or Groovy which can be nicer for scripting in.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline CommanderKeith
« Reply #21 - Posted 2008-11-18 10:59:32 »

Thanks, yeah I've used BeanShell before but it looks inferior to Janino since BeanShell code isn't JIT'ed unlike Janino code which turns your script into proper byte code.

Since I don't know any other language except java i'll have to steer clear of Jython and friends  Tongue

Thanks for the heads-up.

I'm a little surprised at how under-marketed Janino is, because it seems a lot better than most scripting libs in terms of speed and compatibility for how closely it mirrors the java language (beanshell has lots of weird quirks). But for some reason beanshell is much better-known.


Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #22 - Posted 2008-11-18 13:30:51 »

Further, the new Compiler interface in Java 1.6 is very slow (just like javac).

It seems to parse rt.jar for every compilation? persecutioncomplex

Janino is must faster, like 200ms after the first compile.
On the downside, janino doesn't support static imports IIRC, and makes the Java Bytecode Verifier bark sometimes, when your Java sourcecode *is* wrong, but the Janino compiler accepts it.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #23 - Posted 2008-11-18 16:38:40 »

Further, the new Compiler interface in Java 1.6 is very slow (just like javac).

It seems to parse rt.jar for every compilation? persecutioncomplex

Janino is must faster, like 200ms after the first compile.
On the downside, janino doesn't support static imports IIRC, and makes the Java Bytecode Verifier bark sometimes, when your Java sourcecode *is* wrong, but the Janino compiler accepts it.
Yeah I've been using Javac, and it's definitely not incredibly fast on compilation. I'll check out Janino in the future.

See my work:
OTC Software
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.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
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!