Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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 Detection  (Read 1197 times)
0 Members and 1 Guest are viewing this topic.
Offline tdegroot96

Junior Member


Projects: 1



« Posted 2012-09-24 23:22:25 »

I made a little Rectangle for my Player, I got it set up for a bit.
Can someone now tell me how I can listen to collisions from my Player?
You can find my source here: https://github.com/Desmaster/Stibbs
Biggest part of the Rectangle is in the class Player (*.player.Player)

Thanks for the help so far!
Offline actual

JGO Coder


Medals: 23



« Reply #1 - Posted 2012-09-24 23:25:42 »

I don't see a player class within Stibbs.  Do you mean here ?

https://github.com/Desmaster/Game/blob/master/src/net/sourceforge/desmaster/entity/Player.java
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #2 - Posted 2012-09-24 23:56:38 »

Your already listening to collissions with these lines?

for (Block blocks: Level.grassBlocks) {
   if (playerRect.intersects(blocks)) {
      playerRect.y = blocks.y;
      Skyrim.say("DERPPP");
   }
}

If you need to do actions on the event, this can be annoying.
you can check wich side is closest to the player (left, right, top or bottom).
Then according to this you could move the player like:

1  
x = blocks.x - playerWidth; //Move to left side of block

or
1  
x = blocks.x + blocks.width; //Move to right side of block


Same with the top and bottom, after this you need to update the player rect.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tdegroot96

Junior Member


Projects: 1



« Reply #3 - Posted 2012-09-25 08:11:18 »

Sorry about that, I forgot to sync my git!
Please go look for it again,.

Thanks
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #4 - Posted 2012-09-25 09:53:21 »

First of all you shoudent put all those lines in your render code, because you will only need to call these when you change projections.
You should put then in your initGL

1  
2  
3  
4  
5  
      GL11.glMatrixMode(GL11.GL_PROJECTION);
         GL11.glLoadIdentity();
         GL11.glOrtho(0, Display.getDisplayMode().getWidth(), 0, Display.getDisplayMode().getHeight(), 1, -1);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
         GL11.glColor3f(1.0f, 1.0f, 1.0f);


I have no idea what your trying to do in your tick() function, but you should move your player and then theck collission with all objects in the room.
Just check the collission rectangle of the player with the collission rectangle of other objects / players.
Offline StumpyStrust
« Reply #5 - Posted 2012-09-25 10:05:05 »

It may be better to check collision first as you don't want to move your player where he cant go.

Basically, check of collision, no collision? move, else do stuff.

Also, after you collide with something, do not check to see if you collided with other things as that is probably unnecessary computations.

Offline tdegroot96

Junior Member


Projects: 1



« Reply #6 - Posted 2012-09-25 10:34:59 »


1  
2  
3  
4  
5  
      GL11.glMatrixMode(GL11.GL_PROJECTION);
         GL11.glLoadIdentity();
         GL11.glOrtho(0, Display.getDisplayMode().getWidth(), 0, Display.getDisplayMode().getHeight(), 1, -1);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
         GL11.glColor3f(1.0f, 1.0f, 1.0f);


I have no idea what your trying to do in your tick() function, but you should move your player and then theck collission with all objects in the room.
Just check the collission rectangle of the player with the collission rectangle of other objects / players.

If I would cut that part of code and put that into initGL(), it would give me plenty of errors.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public void tick() {
   int colsens = (int) (width % (height * width)) / 2;
   rect.setX((int) x);
   rect.setY((int) y);
   colbox.setX((int) x);
   colbox.setY((int) y);
   colbox.setWidth((int) (width + width / colsens));
   Stibbs.say("set width!");
   colbox.setHeight((int) (height + height / colsens));
   Stibbs.say("set height!");
}


First of all, I'm setting my player(rect) to the x and y positions to keep them synced.
Same to colbox(collision box) which will be the area around my player which will detect collision.
After that, it sets the width and height of the collision box, divided by colsens(collision sensivity).
That's basically my tick method
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.

pw (37 views)
2014-07-24 01:59:36

Riven (38 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

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

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

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

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

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

Riven (57 views)
2014-07-14 18:02:53
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!