Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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 between player and an object  (Read 1413 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2011-12-26 06:22:45 »

I'm writing a game where the player has to push all of the blocks into the holes in as few moves as possible, but I'm having trouble getting the player colliding with the blocks correctly.

I know I must be doing something VERY inefficiently. The only way I could think to do it was to call a new block like this -

1  
2  
this.block1 = new Block();
this.block2 = new Block();


and to keep doing that until I had as many as I needed. But something about that seems wrong.

Any help?

Thanks,
-Nathan

Check out my website!
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-12-26 06:25:07 »

What seems wrong about it?

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #2 - Posted 2011-12-26 06:29:25 »

When I go to check collisions then, I'm going to have to do something like this... -

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
if ((playerX == block1X) && (playerX == block1Y))
{
   if (direction == 1)
   {
      block1X += 1;
      playerX += 1;
   }
   if (direction == 2)
   {
      block1X -= 1;
      playerX -= 1;
}


...and so on for all four directions and as many blocks as there are.

p.s. I know that code ^^ wouldn't work for moving blocks around, just wanted to quickly write something up

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

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-12-26 06:31:30 »

Oh you are going to have block1, block2, ...blockN? How about you use a 2D array that represents the grid, then when the character moves to a new spot, you check if the new spot has a block and then move that block in the same direction.

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #4 - Posted 2011-12-26 06:33:08 »

Can you give me a quick lesson on arrays? I have never used them before in programming. I know it's something like
1  
int grid[] = *....*
but I don't know how that's done.

Check out my website!
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-12-26 07:07:43 »

This should help Smiley

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #6 - Posted 2011-12-26 07:16:58 »


Thanks!   Grin

Is there a way I can read an array and draw my map based on that?

Example -

1  
2  
3  
4  
5  
int[] anArray = {
            0, 1, 2,
            1, 2, 1,
            2, 1, 2
      };


makes

P B H
B H B
H B H

where P = player, B = block, and H = hole.

Check out my website!
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-12-26 07:18:49 »

Use a 2D array:
1  
2  
3  
int[][] myMap = { { 0, 1, 2 },
                  { 1, 2, 0 },
                  { 2, 0, 1 } };

                      

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #8 - Posted 2011-12-26 07:21:22 »

Use a 2D array:
1  
2  
3  
int[][] myMap = { { 0, 1, 2 },
                  { 1, 2, 0 },
                  { 2, 0, 1 } };

                      

What is the difference between a 2D array and a regular one? All I see is a few more brackets, haha.

Check out my website!
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-12-26 07:24:13 »

A 2D array is basically an array of arrays. When using a normal 1 dimensional array, you get elements with an index: int aNumber = myArray[n];
With a 2 dimensional array, each element of the array is an array too. int[] deeperArray = myArray[n];
So, effectively you could use this as the X and Y axis: int aNumber = myArray[x][y];

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

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #10 - Posted 2011-12-26 07:35:33 »

Ok... so...

I have this 2D array

1  
2  
3  
4  
5  
int[][] myMap = {{
            0, 1, 2,
            1, 2, 1,
            2, 1, 2
      }};


then I call this

1  
int aNumber = myMap[x][y];


according to your little example-thing. My question: what does that second bit of code do?

Check out my website!
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2011-12-26 07:36:58 »

Let's say x = 0 and y = 1, myMap[x][y] would get you the "1" on the first row, second column.
Read up some more on arrays so you can get comfortable with them.

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #12 - Posted 2011-12-26 08:06:41 »

Let's say x = 0 and y = 1, myMap[x][y] would get you the "1" on the first row, second column.
Read up some more on arrays so you can get comfortable with them.

Will-do. Thanks

Check out my website!
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #13 - Posted 2011-12-26 17:25:19 »

Ok, so I'm trying to do tile based collison detection like this -

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
int[][] myMap = {{
            0, 1, 2,
            1, 2, 1,
            2, 1, 2
      }};
     
      for (int x = 0; x < 3; x++)
      {
         for (int y = 0; y <= 1; y++)
         {
            int aNumber = myMap[x][y];
            if (aNumber == 1)
            {
               //collides with block
            }
            if (aNumber == 2)
            {
               //falls in hole
            }
         }
      }


But it throws this error -

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1"

What is going wrong with it? I tried debugging and learned nothing.

Check out my website!
Offline dapy
« Reply #14 - Posted 2011-12-26 17:41:43 »

Try defining myMap like this:

1  
2  
3  
4  
5  
int[][] myMap = {
            {0, 1, 2},
            {1, 2, 1},
            {2, 1, 2}
      };
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #15 - Posted 2011-12-26 17:46:45 »

Try defining myMap like this:

1  
2  
3  
4  
5  
int[][] myMap = {
            {0, 1, 2},
            {1, 2, 1},
            {2, 1, 2}
      };


Thanks! I've been trying to figure this out for an hour.

+1 to you Smiley

Check out my website!
Offline Gingerious

Junior Duke


Medals: 2



« Reply #16 - Posted 2011-12-28 19:50:05 »

When you're dealing with a 2d array for your map, it may help think of it in terms of rows and columns, not x and y.  In that instance, y = the rows (because they're vertical) and x = the columns (because they're horizontal), which REALLY tripped me up when displaying my tile map in the game I'm making, because it seems completely backwards.

Just something to keep in mind.
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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (42 views)
2014-10-16 15:22:06

Norakomi (32 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (85 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!