Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help with Tile collisions  (Read 17767 times)

0 Members and 1 Guest are viewing this topic.

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Re: Help with Tile collisions
« Reply #30 on: February 10, 2014, 10:32:45 pm »
Quote from: gop_t3r
it appears the issue hasn't been resolved and am convinced that this has definitely something to do with the way tileNo is calculated [...]

There doesn't seem to be anything wrong with how the tile number is calculated. It's probably the input values to the calculation that are wrong (i.e. you're returning something funky from 'map.getTileSize()' and/or 'map.getDimensions()').

Quote from: gop_t3r
Have you tested the code to see if you can detect the issue?

Where is the code? :/


gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Help with Tile collisions
« Reply #31 on: February 10, 2014, 11:05:23 pm »
Quote from: gop_t3r
it appears the issue hasn't been resolved and am convinced that this has definitely something to do with the way tileNo is calculated [...]

There doesn't seem to be anything wrong with how the tile number is calculated. It's probably the input values to the calculation that are wrong (i.e. you're returning something funky from 'map.getTileSize()' and/or 'map.getDimensions()').

Quote from: gop_t3r
Have you tested the code to see if you can detect the issue?

Where is the code? :/

The code is everywhere as far as I'm aware. Suffice it to say that I've tested everything and there is nothing obscure as to the values produced. All the input values are correct and I'm convinced that there's something weird with the way the computer treats where the player is located at on the grid.

What I noticed is that a collision only happens when the player is touching the bottom of the tile. I really just want a collision which returns true if any of the sides are touched by the player.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with Tile collisions
« Reply #32 on: February 10, 2014, 11:10:15 pm »
I agree that it's a good idea to check your other functions to make sure they are passing x/y values to the relevant x/y.
Your code is in many fragments here. It's impossible to try it. Why not attach it?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Re: Help with Tile collisions
« Reply #33 on: February 10, 2014, 11:10:53 pm »
The code is everywhere? Lemme just grab my ethereal debugger :/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with Tile collisions
« Reply #34 on: February 10, 2014, 11:28:34 pm »
I've tested everything
Obviously not.

I'm convinced that there's something weird with the way the computer treats where the player is located at on the grid.
The calculations that I gave you will only go far as to tell you which tile (0, 1, 2, 3 in your map) is under a specific coordinate. You've been using the player sprite position for this; it's likely that your sprite's origin is at the top-left corner of the sprite so will "be in" a tile when the top-left corner is. To see if the centre is in a tile, you'll need to offset the value.

What I noticed is that a collision only happens when the player is touching the bottom of the tile. I really just want a collision which returns true if any of the sides are touched by the player.
This can also be caused by the effects of the origin being at the top-left. You can either add half of your sprite's size to its position when you calculate it's grid position, or you can just change where its origin is:
player.sprite.setOrigin(player.sprite.getLocalBounds().width / 2, player.sprite.getLocalBounds().height / 2);
You only need to do that once.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Re: Help with Tile collisions
« Reply #35 on: February 11, 2014, 12:02:05 am »
There is nothing wrong with that line of code. That is a well known method to convert from a 2D coordinate into a 1D array index. But hey, you just know that all your other code is producing the correct values, right? :/

Edit: except that it shouldn't be a floating point value...
« Last Edit: February 11, 2014, 12:05:33 am by Lee R »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with Tile collisions
« Reply #36 on: February 11, 2014, 01:38:03 am »
except that it shouldn't be a floating point value...
Oh wow! I didn't even notice that gop was using float there. He did mention later that I should've put said which types to use for the code.

It was supposed to be helpful and point you in the right direction rather than write the program for you. I obviously overlooked the possibility of using floating point numbers for storage of an array index.

I shall punish myself now.

Gop, you should have a look at the tile map class that you said you adapted. You should also check the file itself...  :P

EDIT: If you changed that code, you'll just be getting random tiles to test. This could be any tile so could deceptively look like it's fixed the other, underlining problem. Trust me; keep that and find out what else is making it not work  ;)
« Last Edit: February 11, 2014, 01:41:08 am by Golden Eagle »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Help with Tile collisions
« Reply #37 on: February 11, 2014, 04:30:05 pm »
There is nothing wrong with that line of code. That is a well known method to convert from a 2D coordinate into a 1D array index. But hey, you just know that all your other code is producing the correct values, right? :/

Edit: except that it shouldn't be a floating point value...

Please tell me you're not assuming pTiles is a 2D array, right? :-/ It's 1D to begin with.

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Re: Help with Tile collisions
« Reply #38 on: February 11, 2014, 05:19:03 pm »
I am not assuming pTiles is a 2D array. I am assuming that your screen is 2D. You're converting 2D screen coordinates into 2D grid coordinates (the grid is conceptually 2D) and then into a 1D array index.

 

anything