Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
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  
  Collision  (Read 2306 times)
0 Members and 1 Guest are viewing this topic.
Offline PixelDeBurner

Junior Member


Medals: 1



« Posted 2012-08-25 23:40:59 »

Hello everyone, I'm trying to make tile based game... so far I made level that stores tiles into Tile[] variable. I have getTile method to get the tile from level. Now, I would like to implement collision for player and other Entitites.

The thing is, How would I check collision for tile left from entity and also right from entity ? Also, when I detect collision, I set the boolean collision to true. What to do then, how to stop player, and disable him from stucking in wall ?
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #1 - Posted 2012-08-26 00:00:27 »

OK I managed to do the checking on right side, now only how to disable player from stucking in wall ?
Offline GabrielBailey74
« Reply #2 - Posted 2012-08-26 00:01:49 »

If you paste some code I can help Smiley [ code ] [ /code ]

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #3 - Posted 2012-08-26 00:07:20 »

Here you go... the move method :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   public void move(double xa, double ya) {
      if (xa != 0 && ya != 0) {
         move(xa, 0);
         move(0, ya);
         return;
      }

      boolean collision = false;

      if (this.intersects(level.getTile(x / Tile.WIDTH, (y + 16) / Tile.HEIGHT)) || this.intersects(level.getTile((x / Tile.WIDTH) + 1, (y + 16) / Tile.HEIGHT))) {
         collision = true;
      }

      if (!collision) {
         x += xa;
         y += ya;
      }
   }


the intersects method :

1  
2  
3  
4  
5  
6  
7  
public boolean intersects(Tile t) {
      if (t != null && t.solid()) {
         if (this.x < t.x + Tile.WIDTH || this.y < t.y + Tile.HEIGHT || this.x + Tile.WIDTH > t.x || this.y > t.y) return true;
         return false;
      } else
         return false;
   }
Offline StumpyStrust
« Reply #4 - Posted 2012-08-26 00:26:35 »

Once there is a collision, you need to move what ever collided away from what it hit + some small amount so you can move away again.

Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #5 - Posted 2012-08-26 00:29:23 »

Once there is a collision, you need to move what ever collided away from what it hit + some small amount so you can move away again.


When I do that, I get stuttering, and also sometimes, when I try to go away from wall, player moonwalks into wall.
Offline GabrielBailey74
« Reply #6 - Posted 2012-08-26 01:09:22 »

Hope it helps, first diagram of mine xD might be 2 noob for you lol.



IDK, im making a pong game at the moment, physics are just mindboggling >..<

Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #7 - Posted 2012-08-26 13:07:52 »

Actually I'm trying to test it with only one collision variable, and intersects thing...

But I can't make it working... Sad
Offline keldon85

Senior Member


Medals: 1



« Reply #8 - Posted 2012-08-26 13:47:11 »

Firstly I think it's really helpful to have a basic grasp of vector based collision detection, at least to the point that you know how it can make things easier and also what it is able to achieve. Secondly I think it's helpful to be aware of quantisation when checking for floating point equality.

The diagram below shows a moving red box being checked for collision with a green box. On the left you can see the full extent of the relative velocity vector - and FYI if you wanted to take it further you could have a rotating square, the maths just "works". On the right is what vector based collision detection can achieve, the point in time of impact.



Now the question you might want to ask now is, "how do you find the point of collision"? I'm on my way out now, but when I get back I will find a link to one of my previous posts on this forum or another about using vector maths to solve collisions for geometric points, and I'll upload some of my old collision examples to github Smiley

Now you can see what vector collision detection looks like (at least what it takes in and what it puts out) you might notice something specific about the image on the left that might not have stuck out before. Suppose the boxes passed through each other, if you only checked for collision on specific time slices you are going to miss many collisions when velocities exceed the span of the boxes.

Edit: maybe tomorrow actually, so far I've collected links of previous topics, but it's quite a mouthful right now ...

Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #9 - Posted 2012-08-26 13:52:01 »

I hope your post will help me !
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ultroman

JGO Knight


Medals: 24
Projects: 1


Snappin' at snizzes since '83


« Reply #10 - Posted 2012-08-26 18:40:06 »

Hi.

I wrote up an article that might give you a good deal of insight into how point-collisions work.
Be aware that my method uses simple points. If you're going to be dealing with a lot of things impacting the player movement-speed, you might have an easier time using vectors (like keldon85 suggested).
Doing player-collisions with the intersects-function leaves you with no indication of where on the rectangle the collision happened. For that, you need reference points or vectors.
I use the intersects-function for projectile-collisions and such, and it's great for that, because I don't need much information. To make blood-spatter I just need to know roughly where the projectile enters and exits the monster to place a particle-emitter. But if you need to correct something on account of a collision, you have to use something that'll give you pixel-perfect information.

- Jonas
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #11 - Posted 2012-08-26 19:20:14 »

Well, I'm still learning, what I'm trying to do is simply block character by wall....
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #12 - Posted 2012-08-27 21:13:07 »

Is anyone going to help me here ?
Offline jonjava
« Reply #13 - Posted 2012-08-27 21:22:30 »

Every time the entity moves, check if he is colliding, if he is, revert his position back to the previous one. Or set his individual x and y to that of the colliding walls boundaries depending on which side he is colliding on.

Or, before you move your object, check if there is a collision at the point he is going to move to before moving him at all.

Seems like your movement and collision checking code is pretty much good to go - what's the problem?

Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #14 - Posted 2012-08-27 22:00:29 »

Is it ok if I send you my source code, to see it yourself, and tell me what should I change ?
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #15 - Posted 2012-08-27 23:30:36 »

Got some code from source code of LD game genesis by Yan Chernikov ( TheChernoProject on YouTube ) that works, but needs some modification for my game.
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #16 - Posted 2012-08-28 14:07:28 »

Just to tell everyone I got it working !

Here's the code :

move method :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   public void move(int xa, int ya) {
      if (xa != 0 && ya != 0) {
         move(xa, 0);
         move(0, ya);
         return;
      }
     
      if (!collision(xa, ya)) {
         x += xa;
         y += ya;
      }
   }


collision method :

1  
2  
3  
4  
5  
6  
7  
8  
9  
   private boolean collision(int xa, int ya) {
      boolean solid = false;
      for (int i = 0; i < 4; i++) {
         int xt = ((x + 16 + xa * 16)) / 32;
         int yt = ((y + 32 + ya * 8)) / 32;
         if (level.getTile(xt, yt).solid()) solid = true;
      }
      return solid;
   }


The multiplication of xa and ya is used to make player collide further or closer on wall.
Offline keldon85

Senior Member


Medals: 1



« Reply #17 - Posted 2012-08-28 16:17:26 »

Glad you got it sorted. I've been extra busy, but let me know if you still would like to see some of the vector based collision resources as I will still be collating them for future reference anyway.

Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #18 - Posted 2012-08-28 18:27:11 »

Of course !
 I like learning as much as I can !
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.

xsi3rr4x (32 views)
2014-04-15 18:08:23

BurntPizza (29 views)
2014-04-15 03:46:01

UprightPath (44 views)
2014-04-14 17:39:50

UprightPath (27 views)
2014-04-14 17:35:47

Porlus (44 views)
2014-04-14 15:48:38

tom_mai78101 (65 views)
2014-04-10 04:04:31

BurntPizza (125 views)
2014-04-08 23:06:04

tom_mai78101 (225 views)
2014-04-05 13:34:39

trollwarrior1 (190 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!