🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Figuring out the direction of a block from the player

Started by
4 comments, last by Alberth 4 years, 6 months ago

I'm currently working on a game that features a bunch of blocks that you can mine through (Just think of Minecraft but it's not Minecraft haha). The game is in 2D and I'm wanting a specific block to be mined depending on the keys pressed on the keyboard. So if I press to the left then the left block will be mined. If I press to the left and up at the same time then the block at the top left of the player will be mined.

I'm struggling on figuring out a way to determine which direction a block is from the player. I'm currently looping through all blocks that are near the player but just need a way to determine their direction (preferably as North, South, West, East, etc). Anyone got any ideas of how I could achieve this? (I originally had the idea of using raycasts that point out in all mineable directions to determine the direction but I wasn't sure if that was the best way to do it?)

Advertisement

each block has its center point or a position i dont know how are these blocks build and what sort of data you store them as of.

now lets say you store them as intgere xy positions both blocks andnplayer pos

now normalize(playerpos, testedblock_pos) = x1

normalize(directionofdesired_block) (0,1 or -1,0, or 0,1 etc. = x2

this is a nasty approach here for all blocks find the closest with the most positive value of dot(x1, x2)

dot product means dot(a,b) = x1.x*x2.x + x1.y*x2.y; and its a cosine of angle between vectors

depending on structure you store the information different solutions exist one excludes another use so i wont mindstorm this just to render it useless for you.

_WeirdCat_ said:

each block has its center point or a position i dont know how are these blocks build and what sort of data you store them as of.

now lets say you store them as intgere xy positions both blocks andnplayer pos

now normalize(playerpos, testedblock_pos) = x1

normalize(directionofdesired_block) (0,1 or -1,0, or 0,1 etc. = x2

this is a nasty approach here for all blocks find the closest with the most positive value of dot(x1, x2)

dot product means dot(a,b) = x1.x*x2.x + x1.y*x2.y; and its a cosine of angle between vectors

depending on structure you store the information different solutions exist one excludes another use so i wont mindstorm this just to render it useless for you.

What about going for a Raycast approach? Would that be far less efficient?

it really depends, anyway raycasting can cast ray that won't hit desired block with dot apprpach you find the closest block that is in ‘desired’ direction, however still i do not know the data structurr but i assume its not an 2d array

Codelyy said:
What about going for a Raycast approach? Would that be far less efficient?

How many times per second does a player press a key to start mining?

I would say much much less than 1 . Efficiency is quite a non-issue with such a rare event, unless “a block near to the player” runs in the thousands to millions blocks. Ie don't waste time optimizing code or algorithms that practically use no CPU time during the lifetime of the program.

This topic is closed to new replies.

Advertisement