Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Colision! How the fuq? [solved]  (Read 584 times)
0 Members and 1 Guest are viewing this topic.
Offline GNecro1
« Posted 2014-07-14 21:26:30 »

I have no idea of doing it!
in the begining of the tick method in the player i set xPrev and yPrev to x,y.
Then i do the moving... If intersects then i set x,y to xPrev and yPrev. I get stuck in one place pls help!
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  
public void move() {
      xPrev = x;
      yPrev = y;
      if (Controler.up) {
         y -= Game.pSpeed;
         dir = 1;
         animUp.startAnimation();
      }
      if (Controler.down) {
         y += Game.pSpeed;
         dir = 0;
         animDown.startAnimation();
      }
      if (Controler.left) {
         x -= Game.pSpeed;
         dir = 3;
         animLeft.startAnimation();
      }
      if (Controler.right) {
         x += Game.pSpeed;
         dir = 2;
         animRight.startAnimation();
      }
      colDect();
   }

   private void colDect() {
      for (int i = 0; i < world.tiles.length; i++) {
         for (int j = 0; j < world.tiles[0].length; j++) {
            if (colBox.intersects(world.tiles[i][j]) && world.tiles[i][j].solid) {
               x =  xPrev;
               y = yPrev;
            }
         }
      }
   }

Edit:
What i did is that i moved the collision box first and checked if the is a collision if it was colliding i wouldn't move! But i get it so that my player can't move if i go left and up and there is collision up!For now it's good i guess!

Java freak! Cheesy
Offline CopyableCougar4

Senior Member


Medals: 8
Exp: 1 year



« Reply #1 - Posted 2014-07-14 21:43:04 »

Hi

This is how I do collision detection. Hope this helps Smiley Collisions.isCollision just checks if the player can be at their current location, and returns false if they can be. Your code
colBox.intersects(world.tiles[i][j]) && world.tiles[i][j].solid
would be
Collisions.isCollision()
in my code Smiley
player
is just a Vector2f for the user's position.

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  
   // Check for x
     if(!Collisions.isCollision()){
         // Move forward
        player.translate(x, 0.0f);
         if(Collisions.isCollision()){
                                // Move backward
           player.translate(-x, 0.0f);
         }
      } else {
                        // Move backward
        player.translate(-x, 0.0f);
         if(Collisions.isCollision()){
                                // Move forward
           player.translate(x, 0.0f);
         }
      }
      // Check for y
     if(!Collisions.isCollision()){
         // Move forward
        player.translate(0.0f, y);
         if(Collisions.isCollision()){
                                // Move backward
           player.translate(0.0f, -y);
         }
      } else {
                        // Move backward
        player.translate(0.0f, -y);
         if(Collisions.isCollision()){
                                // Move forward
           player.translate(0.0f, y);
         }
      }


CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline PandaMoniumHUN

JGO Coder


Medals: 31
Exp: 3 years


White-bearded OGL wizard


« Reply #2 - Posted 2014-07-14 23:06:07 »

Isn't this topic essentially the same as the one you opened yesterday and then marked as resolved?
We posted quite a lot of snippets in that topic, if you need help just check back in there.

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (26 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (71 views)
2014-09-10 06:39:09

Tekkerue (36 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (41 views)
2014-09-07 01:13:42

Longarmx (27 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (33 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
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!