Jump to content
  • Advertisement
Sign in to follow this  
kutuup

Mathematical Conundrum!

This topic is 2975 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all,

I am trying to code collision with a tile map.

The problem I have is that the tile map is by array location, but player movement is by pixel.

I need a way to make this calculation to determine whether a tile is passable (array location value 0).


bool tilePassable()
{
if(map[guyPosX / 32][guyPosY / 32] == 0)
{
return true;
}
else
{
return false;
}
}


The problem I have is that naturally the precise pixel the character is sitting on will not necessarily be a multiple of 32, so I will end up with a decimal resule when I divide its location by 32.

Is there a way to make this calculation to the nearest whole number, ie ignoring any remainder, so that, for example 100 / 32 = 3 (ignoring the remaining 4).

Is there a way to make this calculation in code?

Thanks

[Edited by - Zahlman on August 31, 2010 4:08:40 PM]

Share this post


Link to post
Share on other sites
Advertisement
If guyPosX and guyPosY are integers, chances are what you have will work fine.

Share this post


Link to post
Share on other sites
Yes, they are integers.

Wont I get an error saying I'm converting from an integer to a double if I do it though?

Share this post


Link to post
Share on other sites
Quote:
Original post by kutuup
Is there a way to make this calculation to the nearest whole number, ie ignoring any remainder, so that, for example 100 / 32 = 3 (ignoring the remaining 4).


Though in this case it isn't needed since you're using integers, you could use the floor function.

Share this post


Link to post
Share on other sites
While we're at it, let's simplify.

"If it is raining outside, I will take my umbrella with me; otherwise, I will not take my umbrella with me."

Ugh. Wordy, and not actually making things clearer.

"I will take my umbrella with me if (and only if) it is raining".

Much better.

Thus:


bool tilePassable()
{
return map[guyPosX / 32][guyPosY / 32] == 0;
}

Share this post


Link to post
Share on other sites
The problem with this approach, as I learned a few days ago coding my tile engine, is that if the player's sprite isn't the same dimensions as the tiles, then you'll collide with a lot of "air". If the pos of your sprite is based on the top left pixel, then checking collision on the top and left will be perfect but on the right and bottom will not.

I'm sure there are a million ways around this, however I just include 3 small arrays (mine are vectors but you could use what ever you want). One for the xpos for collidable tiles, one for the ypos of collidable tiles, and one for the type of collidable tile (==1 for collidable on all sides, ==2 if it damages you, etc etc.) Then you can make a more precise collision check based off of x/y coord instead of tile spots. This also stops you from cycling through a large map and only cycle through known collidable spots. The array is filled when the level is made by deciding what tiles are collidable, multiplying their index by the tile size, and adding that number to the array.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!