Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (761)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (846)
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  
  Tile Collision  (Read 2189 times)
0 Members and 1 Guest are viewing this topic.
Offline steg90

Senior Devvie


Medals: 2



« Posted 2015-07-13 16:16:25 »

Hi,

I got my tile collision working in my sandbox game, but when a player is say moving right and hits a block/platform, I'd like him to automatically move up this block like in Terraria.

                            BBBBB
                     PBBBB
BBBBBBBBBBBBB

B is block, P is player, player has hit block, and they continue pressing right key so now want them to go up the block onto the platform without having to jump, hope this makes sense?

Thanks
Offline orangepascal
« Reply #1 - Posted 2015-07-13 17:23:43 »

so the logic is pretty simple:

if you detect a block on the right of the player, AND the player is pressing right:
 you start code to move the player "up and over" (for platformer a simple call to trigger the "jump" action should do)



the guy behind Orangepixel | twitter@orangepascal
http://www.orangepixel.net
Offline chrislo27
« Reply #2 - Posted 2015-07-13 17:32:05 »

Minecraft has this too (half-slabs, but stairs are a bit of an exception as they're specially handled). Each entity has a "stepFactor" which is how much of a block they can step up without jumping. For players and general mobs it's 0.5F. As orangepascal said, you can detect if the player is pressing right onto the platform. Another option is when you're resolving collision detection to also move the player onto the top of the platform if you're colliding directly with the platform.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline steg90

Senior Devvie


Medals: 2



« Reply #3 - Posted 2015-07-13 20:12:43 »

Got this working with the code below (snippet):

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  
      Vector3 poss = new Vector3(camera.position);
      poss.y-=8;
      // Need to check if not jumping as we need to jump Y amount before any downward force happens
       if(!bRight)  // pressing right key, if so, don't check for collision downwards
      {
          if(checkCollision(poss))
         {
            camera.position.y -= 3;  // scroll map down
         }
      }
     

      if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
         
         player.moveRight(1);
         Vector3 po = new Vector3(camera.position);
         po.y-=TILEHEIGHT/2;
         
         if(checkCollision(po)) {        
            camera.position.x += 2; // scroll map right
            bRight = false;
         }
         else // collision - how do we go up - go up until no collision
         {  
               bRight = true;            
               camera.position.y += 3; // scroll map up
         }
      }
   


and collision code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
   private boolean checkCollision(Vector3 position)
   {
      Vector3 screenSpace = toScreenSpace(position);
      int x = Math.round(screenSpace.x);
      int y = Math.round(screenSpace.y);
      BlankEntity entity = this.worldMap.getBlock(x, y);
      if(entity ==null) return true;
         return false;
     
   }
   // Some helper methods
   private Vector3 toScreenSpace(Vector3 position) {
      Vector3 v = new Vector3(0, 0, 0);
      v.x = position.x / TILEWIDTH;
      v.y = position.y / TILEHEIGHT;
      return v;
   }


This now works :-)  Still to put jumping in then digging and some lighting...
Offline steg90

Senior Devvie


Medals: 2



« Reply #4 - Posted 2015-07-14 09:41:20 »

Thought it was working well, but now well collide say with a big wall, moves all the way up it, just want it to move up to a maximum amount...


       T
       BBBBBBBBBB
       BBBBBBBBBB
     PBBBBBBBBBB
BBBBBBBBBBBBBB

Above P (player) when pressing right would go all the way up the wall and end up at T, now what I want, just want to go up one block...
Would this require me to save the players position when collsion occured on right key?

Thanks
Offline Springrbua
« Reply #5 - Posted 2015-07-14 09:52:03 »

I guess the only thing you need to do here is to check the Block diagonally above you:

       BBBBBBBBBB
       XBBBBBBBBB
     PBBBBBBBBBB
BBBBBBBBBBBBBB

In this example you need to check the "X". If "X" is empty or a non-blocking Block, you can move, else you can't.
Offline steg90

Senior Devvie


Medals: 2



« Reply #6 - Posted 2015-07-14 13:46:55 »

Got this working now :-)

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  
      if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
         bRight = false;

         player.moveRight(1);
         Vector3 po = new Vector3(camera.position);
         po.x-=TILEWIDTH/2;
         po.y-=4;//TILEHEIGHT/2;
         if(checkCollision(po)) {
            camera.position.x -= 2;  // scroll map left
            bLeft = false;  
         }
         else // collision, move up
         {
            if(displayBlockDiagAbove(false,camera.position))
            {
               bLeft = true;
               camera.position.y += 3;
            } else
            {
               bLeft = false;
            }
         }
      }
     
      if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
     
         bLeft = false;
         
         player.moveRight(1);
         Vector3 po = new Vector3(camera.position);
         po.y-=4;//TILEHEIGHT/2;
         
         if(checkCollision(po)) {        
            camera.position.x += 2; // scroll map right
            bRight = false;
         }
         else // collision - how do we go up - go up until no collision
         {  
            // is it clear above block? (water, cave, blank)
            if(displayBlockDiagAbove(true,camera.position))
            {
               bRight = true;            
               camera.position.y += 3; // scroll map up
            }
            else
            {
               bRight = false;
            }
         }
      }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
   private boolean displayBlockDiagAbove(boolean dir, Vector3 position)
   {
      int val = dir == true ? 0 : -1;
      Vector3 screenSpace = toScreenSpace(position);
      int x = Math.round(screenSpace.x)+val;
      int y = Math.round(screenSpace.y)+1;
      BlankEntity entity = this.worldMap.getBlock(x, y);

      if(entity!=null)
      {  
         if(entity.toString().equals(".") || entity.toString().equals("CAVE")
               || entity.toString().equals("WATER") )
               
         {
            return true;
         }
      }
      else // entity is null
         return true;
           
      return false;
   }


The method displayBlockDiagAbove did the trick - as Springrbua suggested, check block diagnally above.

Thanks
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (351 views)
2018-06-10 19:43:48

EgonOlsen (344 views)
2018-06-10 19:43:44

EgonOlsen (292 views)
2018-06-10 19:43:20

DesertCoockie (516 views)
2018-05-13 18:23:11

nelsongames (843 views)
2018-04-24 18:15:36

nelsongames (829 views)
2018-04-24 18:14:32

ivj94 (1251 views)
2018-03-24 14:47:39

ivj94 (395 views)
2018-03-24 14:46:31

ivj94 (1058 views)
2018-03-24 14:43:53

Solater (413 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!