Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Collision again. Good detection system. on: 2012-09-09 12:44:09
gameLoop() is called from initialize():
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   private void gameLoop() {
      timer = new Timer();
      timer.schedule(game, 0, 1000 / 60);
   }

   TimerTask game = new TimerTask() {
      public void run() {
         gravity.pulling();
         collision.checkY();
         collision.checkX();
         repaint();
      }
   };

keyTyped() and pulling() in Gravity:
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  
41  
42  
43  
44  
45  
46  
47  
public void keyTyped(KeyEvent kevt) {
      char c = kevt.getKeyChar();
      if (!game.man.isFalling) {
         if (c == 'w') {
            vspeed = 0;
            gUp = true;
            gDown = false;
            gLeft = false;
            gRight = false;
         } else if (c == 's') {
            vspeed = 0;
            gDown = true;
            gUp = false;
            gLeft = false;
            gRight = false;
         } else if (c == 'a') {
            vspeed = 0;
            gLeft = true;
            gUp = false;
            gDown = false;
            gRight = false;
         } else if (c == 'd') {
            vspeed = 0;
            gRight = true;
            gUp = false;
            gDown = false;
            gLeft = false;
         }
      } else {

      }
   }
   public void pulling() {
      if (gUp) {
         //vspeed += gravity / 2;
        game.man.setY(-3);
      } else if (gDown) {
         //vspeed += gravity / 2;
        game.man.setY(3);
      } else if (gRight) {
         //vspeed += gravity / 2;
        game.man.setX(3);
      } else if (gLeft) {
         //vspeed += gravity / 2;
        game.man.setX(-3);
      }
   }

2  Game Development / Newbie & Debugging Questions / Re: Collision again. Good detection system. on: 2012-09-09 11:59:00
Also one little thing: please rename some of your methods and variables. I don't quite understand what lvl_CenterX is, and I don't think this is the variable for describing the x mid of the whole level Smiley
lvl_CenterX (and Y) is a center X of every rectangle on the map. When we'll get rid of the problem I should do some refactoring.

Now I'm calling checkY() before checkX() and this is happening:
Click to Play

Upper and lower platform collision works well, but left-right makes player's rectangle... you see, it shouldn't be like that.
3  Game Development / Newbie & Debugging Questions / Re: Collision again. Good detection system. on: 2012-09-09 07:51:59
Look at the second gif again.
Using WASD I can change the gravity's direction. When player's rectangle collides with left or right wall it works well, doesn't move and stay near (not in) the wall. When gravity is changed on up or down, player's rectangle stops before wall, but it's automatically moving to right or left.
You probably still don't understand me, so here's the "game":
http://www.mediafire.com/?adrhfc1uw2vscfo
4  Game Development / Newbie & Debugging Questions / Re: Collision again. Good detection system. on: 2012-09-08 19:28:27
Well, I think I need to get rid of this moving when collision detected on Y-axis. In my game player will be able to change the gravity direction, so there shouldn't be any auto-move while standing on the ground.
5  Game Development / Newbie & Debugging Questions / Re: Collision again. Good detection system. on: 2012-09-08 17:46:48
I split (splitted, splote? No idea Emo) check() method into two:
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 checkX() {
      System.out.println("X" + intersectsX);
      man_CenterX = (int) game.man.getBounds().getCenterX();
      for (int i = 0; i < game.level1.getX().size(); i++) {
         lvl_CenterX = (int) game.level1.getBounds(i).getCenterX();
         intersectsX = game.man.getBounds().intersects(game.level1.getBounds(i));
         if (intersectsX) {
            if (man_CenterX - lvl_CenterX > 0) {
               game.man.setX(3);
            //    break;
           } else if (man_CenterX - lvl_CenterX < 0) {
               game.man.setX(-3);
            //    break;
           }
         }
      }
   }

   public void checkY() {
      System.out.println("Y" + intersectsY);
      man_CenterY = (int) game.man.getBounds().getCenterY();
      for (int i = 0; i < game.level1.getY().size(); i++) {
         lvl_CenterY = (int) game.level1.getBounds(i).getCenterY();
         intersectsY = game.man.getBounds().intersects(game.level1.getBounds(i));
         if (intersectsY) {
            if (man_CenterY - lvl_CenterY > 0) {
               game.man.setY(3);
               //break;
           } else if (man_CenterY - lvl_CenterY < 0) {
               game.man.setY(-3);
               //break;
           }
         }
      }
   }
}


My game is based on the rectangles, not tiles.

Now gif again. This is happening after splitting the check():
Click to Play
6  Game Development / Newbie & Debugging Questions / Collision again. Good detection system. on: 2012-09-08 16:05:59
Hi, I'm working on gravity-based logic game. Please, look at this gif:
Click to Play

Lower and upper rectangles detect collision correctly, but as you can see left-right is the problem. I have no idea how to fix it.
My check() method is called from game loop.
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  
   public void check() {
      man_CenterX = (int) game.man.getBounds().getCenterX();
      man_CenterY = (int) game.man.getBounds().getCenterY();
      for (int i = 0; i < game.level1.getX().size(); i++) {
         System.out.println(i + " " + intersects);
         lvl_CenterX = (int) game.level1.getBounds(i).getCenterX();
         lvl_CenterY = (int) game.level1.getBounds(i).getCenterY();
         intersects = game.man.getBounds().intersects(game.level1.getBounds(i));
         if (intersects) {
            if (man_CenterY - lvl_CenterY > 0) {
               game.man.setY(3);
               System.out.println("HEJ1");
            //   break;
           } else if (man_CenterY - lvl_CenterY < 0) {
               game.man.setY(-3);
               System.out.println("HEJ2");
               //break;
           } else if (man_CenterX - lvl_CenterX > 0) {
               game.man.setX(-3);
               System.out.println("HEJ3");
            //   break;
           } else if (man_CenterX - lvl_CenterX < 0) {
               game.man.setX(3);
               System.out.println("HEJ4");
            //   break;
           }
         }
      }
   }
7  Game Development / Newbie & Debugging Questions / Re: Matrix camera, movement concept on: 2012-08-29 09:41:34
Thank you! Smiley
Now I can go back implement more physics and monsters.
8  Game Development / Newbie & Debugging Questions / Re: Matrix camera, movement concept on: 2012-08-28 15:46:51
No, I'm using only Java2D.
And thank you for fast response Smiley
9  Game Development / Newbie & Debugging Questions / Matrix camera, movement concept on: 2012-08-28 15:38:33
Hi, I was talking to some guy. He said that my movement concept in game is bad. When left or right arrow is pressed I'm scrolling background what makes you feel that player is moving (player's X remains same).
So... he told me something about matrix view. I should create all walls and platforms static and scroll only the camera and move player's rectangle.
I did a little research in Google, but nothing found.
Can you tell me anything about it? How to start? Maybe links, books and resources?
My programming language is Java (2d).

Thank you!
10  Game Development / Game Mechanics / [SOLVED] How to make Rectangle not moving after collision on: 2012-08-25 16:42:18
Hi,
How to make Rectangle not moving after collision?
Look at this GIF:
Click to Play

I don't know how to fix it, my checkCollision() and move():
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  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
   @Override
   public void keyPressed(KeyEvent k) {
      int z = k.getKeyCode();
      switch (z) {
      case KeyEvent.VK_LEFT:
         left = true;
game.physic.leftVel = 2;
         break;
      case KeyEvent.VK_RIGHT:
         right = true;
game.physic.rightVel = 2;
         break;
      case KeyEvent.VK_SPACE:

         break;

      default:
         break;
      }

   }

   public void move() {
      if (left) {
         switch (game.currentLevel()) {
         case 1:
            for (int i = 0; i < game.lvl1.getX().length; i++)
               game.lvl1.getX()[i] += game.physic.leftVel;
            break;

         }
      } else if (right) {
         switch (game.currentLevel()) {
         case 1:
            for (int i = 0; i < game.lvl1.getX().length; i++)
               game.lvl1.getX()[i] -= game.physic.rightVel;
            break;
         }
      }
   }

/*
*
*
*/


public void checkCollision() {
      switch (game.currentLevel()) {
      case 1: //level 1
        for (int i = 0; i < game.lvl1.getX().length; i++) { //scroll through all the platforms
           if (game.man.getBounds().intersects(game.lvl1.getBounds(i))) {
               if (game.man.getBounds().getY() < game.lvl1.getBounds(i)
                     .getY() && !game.man.isOnGround) {
                  // above
                 System.out.println("works^");
                  break;
               } else if (game.man.getBounds().getY() > game.lvl1
                     .getBounds(i).getY() && isJumping) { //when jumping
                 // hit below
                 System.out.println("worksV");
                  break;
               } else if (game.man.getBounds().getX() < game.lvl1
                     .getBounds(i).getX() && right) { //move to the right
                 right = false;
                  game.physic.rightVel = 0;
                  System.out.println("works<");
                  break;
                  // hit left
              } else if (game.man.getBounds().getX() > game.lvl1
                     .getBounds(i).getX() && left) { //move to the left
                 left = false;
                  game.physic.leftVel = 0;
                  System.out.println("works>");
                  break;
                  // Hit was on right
              }

            }else{ //if doesn't intersect
              game.physic.rightVel = 2;
               game.physic.leftVel = 2;
               game.man.isOnGround = false;
            }
         }
      }
   }


Hope you help me Smiley
Thank,
Szinek

Solved
11  Game Development / Newbie & Debugging Questions / Re: Gravity is colliding with collide detection on: 2012-08-24 17:49:48
I just read about game loops (I didn't have Lips Sealed), did some research and finally I got this working.

Now I can go ahead with game physics.
12  Game Development / Newbie & Debugging Questions / [SOLVED] Gravity is colliding with collide detection on: 2012-08-24 10:03:47
Hello, I'm new here.
I'm coding platform game. I had been stuck on collision detection for 3 days, finally I've solved it.
But now I have to pull player down when he doesn't intersect anything. I did it this way:
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  
Runnable gravity = new Runnable() {
      public void run() {
         while (true) {
            player.y += yspeed;
            collide.gravity();
            repaint();
            try {
               Thread.sleep(10);
            } catch (InterruptedException e) {
               // TODO Auto-generated catch block
              e.printStackTrace();
            }
         }
      }
   };

//Gravity method:
  public void gravity() {
      for (int i = 0; i < box.getX().length; i++)
         if (player.getBounds().intersects(box.getBounds(i))
               && game.yspeed != 0) {
            game.yspeed = 0;
            break;
         } else {
            game.yspeed = 1;
         }
   }


And it works well, player is going down and stops when collides. But there's one glitch caused by right-left collision. Player can't move.

check() method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
   public void check() {
      // collision from right side
     for (int i = 0; i < box.getX().length; i++) {
         if (player.getBounds().intersects(box.getBounds(i))
               && player.xspeed > 0) {
            setLeft(true);
         } else if (!player.getBounds().intersects(box.getBounds(i))
               && player.xspeed < 0) {
            setLeft(false);
         }
         if (player.getBounds().intersects(box.getBounds(i))
               && player.xspeed < 0) {
            setRight(true);
         } else if (!player.getBounds().intersects(box.getBounds(i))
               && player.xspeed > -1) {
            setRight(false);
         }
      }
   }

And methods responsible for movement:
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  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
@Override
   public void keyPressed(KeyEvent e) {
      int key = e.getKeyCode();
      switch (key) {
      case KeyEvent.VK_LEFT:
         left = true;
         game.collide.check();
         move();
         break;
      case KeyEvent.VK_RIGHT:
         right = true;
         game.collide.check();
         move();
         break;
      case KeyEvent.VK_UP:
         y -= 3;
         game.repaint();
         break;
      case KeyEvent.VK_DOWN:
         y += 3;
         game.repaint();
         break;
      default:
         break;
      }
   }

   private void move() {
      if (left) {
         leftSpeed = 1;
         xspeed = 1;
         scroll();
      } else if (right) {
         leftSpeed = 1;
         xspeed = -1;
         scroll();
      }
   }

   public boolean dudeLeftCollision() {
      return game.collide.left;
   }

   public boolean dudeRightCollision() {
      return game.collide.right;
   }

   private void scroll() {
      if (dudeLeftCollision() && !dudeRightCollision()) {
         leftSpeed = 0;
         rightSpeed = -1;
         for (int i = 0; i < game.box.getX().length; i++) {
            game.box.getX()[i] = game.box.getX()[i];
         }
      }
      if (!dudeLeftCollision() && dudeRightCollision()) {
         leftSpeed = 0;
         rightSpeed = 1;
         for (int i = 0; i < game.box.getX().length; i++) {
            game.box.getX()[i] = game.box.getX()[i];
         }
      }
      if (!dudeLeftCollision() && !dudeRightCollision()) {
         for (int i = 0; i < game.box.getX().length; i++) {
            game.box.getX()[i] += xspeed;
         }
      }
      game.repaint();
   }


It's my story. I have to figure out how to code collision-detection gravity-right-left without any glitches.

Hope you help me,
Thank you,
Szinek

[size=5pt]I wonder if my english is very bad.[/size]

Oops, wrong board? I should write this in "Game Mechanics"?
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

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

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

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

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

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

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

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

BurntPizza (49 views)
2014-08-09 21:09:32
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!