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  
  Collision with platforms  (Read 2600 times)
0 Members and 1 Guest are viewing this topic.
Offline jammas615

Junior Member


Projects: 1



« Posted 2012-02-11 04:19:32 »

I know i had a post about this last week, but i have since changed my design and am now really in need of some ideas and ways that i can implement collisions between my player and platforms.

This is what my game currently looks like:


I have all platforms loaded into an array list, but i don't want to check for collisions against every platform as that just would cause the performance to decrease dramatically.
So i've been trying to think of ways to decide on which platform/s to check for collisions based on the players position.
Which is what i need help with.

So, if you have any ideas or suggestions please reply.


Offline ReBirth
« Reply #1 - Posted 2012-02-11 04:22:50 »

If you're going to make Doodle Jump clone, then I sugesst you to only check platform that appear in screen. Should no more than 10 right?

However if it's clone, as ra4king who has made it successfully Wink

Offline UprightPath
« Reply #2 - Posted 2012-02-11 04:32:39 »

To check a subsection of the map...

Things you need:
1) Map (Defined some how that allows you to get position information)
2) Player (Defined by a position and dimensions)
3) Information about relative dimensions of platform blocks.

Then:
int startX = Player.position.x / width
int startY = Player.position.y / height
int endX = (Player.position.x + player.width) / width
int endY = (Player.position.y + player.height) / height

boolean isOccupied(int startX, int startY, int endX, int endY) {
for(int i = startX; i <= endX; i++) {
for(int j = startY; j <= endY; j++) {
if(map.isFilled(i,j)){
return true;
}
}
return false;
}

One thing to note is that this only checks current position. Of course, you can use it to check whether or not the result of a movement. If you're really smart, you can have it return the position of the 'first' collision that occurs in the direction of a motion. So you know which way to shift for the stop to happen.

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

Junior Member


Medals: 3



« Reply #3 - Posted 2012-02-12 00:09:13 »

Doesn't that startx stuff seem unnecessary? Why not just do this:

if (!map.collisionAt(player.getX() + player.getXVel(), player.getY() + player.getYVel()) {
        // No collision if you move here, go ahead and move
}

The collisionAt(int x, int y) method in Map simply loops and checks each platform to see if 'x' or 'y' is between their position and position + size.

No need to complicate it =)

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-02-12 02:52:47 »

A Rectangle v Rectangle check is very cheap so you should be fine looping through all Platform's rectangle bounds against the player's bounds (please have a Rectangle instance in each Platform and in the Player class, instead of creating an instance within the getBounds() method. That's a GREAT way to waste memory Smiley). If the velocity of the player is greater than 0, check for collisions Smiley

Offline ReBirth
« Reply #5 - Posted 2012-02-12 03:12:04 »

A Rectangle v Rectangle check is very cheap so you should be fine looping through all Platform's rectangle bounds against the player's bounds (please have a Rectangle instance in each Platform and in the Player class, instead of creating an instance within the getBounds() method. That's a GREAT way to waste memory Smiley). If the velocity of the player is greater than 0, check for collisions Smiley
you mean this? Grin
1  
2  
3  
public Rectangle getBounds(){
    return new Rectangle(x, y, width, height);
}

I really believe OP want make DJ.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-02-12 03:15:51 »

Yes.

Offline evilfrenchguy

Junior Member


Medals: 3



« Reply #7 - Posted 2012-02-12 13:46:10 »

A rectangle vs rectangle is great too. So, essentially, after initializing all the rectangles (having each rect cover the entire platform) when you create your player/platforms, it's as simple as:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public boolean collisionAt(Player player) {
              // Loop through all platforms in the map
             for (Platform platform : platformList) {
                          // If the player's rectangle is inside this particular platform's rectangle (intersects() is a method in Rectangle
                         // that takes another Rectangle as an argument and checks this for you)
                         if (player.getCollisionRect().intersects(platform.getCollisionRect())) {
                                         return true;      // Collision! Return true so player knows
                         }
              }
              return false;         // Nothing returned true, so report false
}


Like ra4king said, this only checks the current position so in your update move player, then check. Move back if there was a collision. So your player's update method might look like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void update(float elapsedTime) {
             // ....
            // Other updates
            // ....

             // Move player
            x += xVel;
             y += yVel;

             // Check map now that I've moved
            if (map.collisionAt(this)) {
                       // We can't go here now, so 'undo'
                      x -= xVel;
                       y -= yVel;
             }
}


EDIT: Actually, wouldn't it be more readable to pass an x or y and collisionAt() makes a player rect before the loop? That sounds more readable with negligible overhead. Correct me if I'm wrong.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-02-12 15:35:10 »

Think of design here, why would the map care what the player is? The way you have it is fine, however it is best to make checkCollision take an object of type Entity or whatever other superclass all entities extend. Then make it return an Entity instead of boolean (null being false) so you could have specialized behavior Smiley

Offline evilfrenchguy

Junior Member


Medals: 3



« Reply #9 - Posted 2012-02-12 15:47:17 »

That's a good idea. Then Player could use instanceof to have reactions against spikes or an enemy.

That way enemies could utilize it as well to climb walls or have different behavior.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jammas615

Junior Member


Projects: 1



« Reply #10 - Posted 2012-02-12 20:00:50 »

Thanks for your help guys, I've got heaps of ideas on collision now, its just i can't find a good way to move the camera with the player as they climb each platform. I've tried changing the velocity of the camera and/or adding to its position, but i can't get something solid.
PS. My camera class just holds its yPosition and all entities are drawn based on its position.

Is there better design i could be using? Im really not sure.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2012-02-12 20:04:14 »

Your design is the best way to do it. All entities should have some way to access this Camera instance and then just add yOffset to their Y coordinates Smiley

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.

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

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

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

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

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

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

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

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

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

mitcheeb (39 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!