Anyone want to help me with my python project? :|

Not about OpenMW? Just about Morrowind in general? Have some random babble? Kindly direct it here.
User avatar
raevol
Posts: 3093
Joined: 07 Aug 2011, 01:12
Location: Caldera

Anyone want to help me with my python project? :|

Post by raevol »

I recently picked up working on a python project I started a while ago, and I have coded myself into a corner.

I am working on a 2d isometric game engine, and I need to figure out how to convert a screen x-y coordinate into the isometric tile that it corresponds with.

The project is here: https://github.com/mickeylyle/hacknslash

The function is called get_tile_from_xy() in the hackmap.py file.

Whatever is there right now is completely unworkable, don't even bother looking at it, it needs to be completely rewritten.

The way the renderer works, the maps are laid out like this:

1, 2, 3
4, 5, 6
7, 8, 9

When the maps are rendered, tile #1 will be the top of the isometric diamond, tile #7 will be the left side, tile #3 the right side, and tile #9 will be the bottom. The tiles are 32 pixels wide and 16 pixels high. So given a screen x,y coordinate, from a mouse click or the player position, or whatever, how would I get the tile's array coordinates back from that? As in, tile #2 has the array coordinates [1, 0], and tile #6 has the coords [2, 1]. Anyone tackled this problem before?
User avatar
sirherrbatka
Posts: 2159
Joined: 07 Aug 2011, 17:21

Re: Anyone want to help me with my python project? :|

Post by sirherrbatka »

Transform pointer position vector to isometric coordinates system and then compare with your flat grid?

PS
http://en.wikipedia.org/wiki/Transformation_matrix
Python will have libs for that so don't bother to write it manually.
User avatar
sjek
Posts: 442
Joined: 22 Nov 2014, 10:51

Re: Anyone want to help me with my python project? :|

Post by sjek »

what kind of movement are you planning for player ?
the pixel getting technic depents what it seems do you want the map area be the window or bigger and does the player move outside the initial position in the window.

as of code don't understand it that much but got this far.

Code: Select all

class hackmap:
def __init__(self, mapfile):
self.title = mapfile
self.tileset = {}
self.mapset = []
self.tile_width = 32
self.tile_height = 16
self.buffer = 10
with open("maps/" + mapfile + ".map", "r") as myfile:
data = myfile.readlines()
this goes good as the initial parameters are set althought might be some extras... if that works then {} jumps to = list and [] to array automatically.

Code: Select all

 for line in data:
line = line.replace("\n", "")
if '=' in line:
self.tileset[line.split('=')[0]] = hacktile(line.split('=')[1])
elif ':' not in line and len(line) > 1:
chars = []
for c in line.split(','):
chars.append(c)
self.mapset.append(chars)
i assume that you try to define the hacktexture, move forward and take the numbers out. i think there might be duplicate with the last line of the latter. http://stackoverflow.com/questions/1435 ... line-split

the map function as is works only in 2.x for a list http://stackoverflow.com/questions/1961 ... -of-python

Code: Select all

 def get_tile_from_xy(self, x_pos, y_pos):
# this is completely broken
tile_x = (y_pos / self.tile_height) + (x_pos / self.tile_width)
tile_y = (x_pos / self.tile_width) - (y_pos / self.tile_height)
return [tile_x, tile_y]
can't get my head around the math behind this.
with surface.get_offset
might be possible as it takes the starting point of subsurface

for rest of the code if you are baking picture blit (dect....) could do the same or if taking collision it might need something like that to detect where the player is
Spoiler: Show
sirherrbatka wrote:Transform pointer position vector to isometric coordinates system and then compare with your flat grid?

PS
http://en.wikipedia.org/wiki/Transformation_matrix
Python will have libs for that so don't bother to write it manually.
http://www.freecadweb.org/ might have some of it in source. it's using external library thought ..... https://bitbucket.org/Coin3D/coin/wiki/Home c++ and needs a wrapper...

don't know if isometric is possible with surface class if you want to move the camera. if only from top side it could be possible.

http://www.pygame.org/docs/ref/surface. ... ls_address
it would be needed to bake it with blip and dect and update pixel_address for more complicated shapes than cube.

with pygame it could be also possible to draw shapes and update those for frames
http://programarcadegames.com/index.php ... #section_5

and the player movement would go via keyboard or mouse with placement function
http://programarcadegames.com/index.php ... section_11

with that also animating would be possible
http://www.cs.ucsb.edu/~pconrad/cs5nm/t ... e/drawing/

don't know how much math and processing power that would take. shapes would be needed to define first before the main loop and update them in.



edit: .................. it's just the viewing angle .?
http://blog.vim.pl/2012/04/gra-izometry ... metryczny/ 2d map can be translated :mrgreen:
http://www.pygame.org/wiki/tutorials#Polish
User avatar
raevol
Posts: 3093
Joined: 07 Aug 2011, 01:12
Location: Caldera

Re: Anyone want to help me with my python project? :|

Post by raevol »

Finally figured it out: https://github.com/mickeylyle/hacknslas ... 335c17e312
Probably not the best implementation, but it do.

@sirherrbatka thanks for the input- I think I did what you said but I am not the best with the maths so I am not sure.

@sjek I'm sorry but I don't really understand your post. :( Thank you for the input though!
wheybags
Posts: 207
Joined: 21 Dec 2012, 19:41

Re: Anyone want to help me with my python project? :|

Post by wheybags »

It's actually a more complicated task than it seems at first :p
I had to do this for freeablo. I eventually solved it by treating the level as a huge rectangular tile, with the isometric tiles drawn onto it (like you drew the whole level into an image file), then conceptually overlaying a grid onto it whose rows and columns intersect at the isometric intersect points. It's pretty trivial to tell which "straight" grid tile was clicked, and from there you can get the actual tile clicked by dividing it into 4 segments correcponding to the 4 corners of the isometric grid tiles it covers, and detrmining which one it lies in.
Spaghetti code which implements this here: https://github.com/wheybags/freeablo/bl ... d.cpp#L574 (that whole file really needs a refactor)

It's shit like this that is the reason if I were ever making an isometric game from scratch I'd just use a 3d engine with isometric projection.
You get sooooo much stuff for free (clipping bulshit is free too)

EDIT: that may be what you did too, I didn't look at your code too long so I'm not sure. I haven't seen anyone else do this before though, and the solutions I found when googling the problem alsways involved some weird floating point trigonometry stuff which wasn't perfectly accurate down to individual pixels.
User avatar
raevol
Posts: 3093
Joined: 07 Aug 2011, 01:12
Location: Caldera

Re: Anyone want to help me with my python project? :|

Post by raevol »

I started to writeup an explanation of what I did, and it turned into an essay, so uh, here: https://github.com/mickeylyle/hacknslas ... xplanation

May not be the most coherent piece of writing, but maybe it helps some.
User avatar
psi29a
Posts: 5361
Joined: 29 Sep 2011, 10:13
Location: Belgium
Gitlab profile: https://gitlab.com/psi29a/
Contact:

Re: Anyone want to help me with my python project? :|

Post by psi29a »

Great work raevol. :) I think Python is a great language to begin with and even make a game with. The only real problem I had was making it releasable for windows/mac without having the user install a full development environment. I've tried everything from cx_freeze and other "compilers" to just shipping full venvs (virtual environments). They all have their pros and contras.
User avatar
raevol
Posts: 3093
Joined: 07 Aug 2011, 01:12
Location: Caldera

Re: Anyone want to help me with my python project? :|

Post by raevol »

Yea... I figure I'll cross that bridge when I get to it, haha.
User avatar
sirherrbatka
Posts: 2159
Joined: 07 Aug 2011, 17:21

Re: Anyone want to help me with my python project? :|

Post by sirherrbatka »

It is nice to know a little bit of math, simply because many problems (like the one above) are strictly matematical in it's nature and already solved, just need to think what is the proper problem description ;-)
User avatar
raevol
Posts: 3093
Joined: 07 Aug 2011, 01:12
Location: Caldera

Re: Anyone want to help me with my python project? :|

Post by raevol »

sirherrbatka wrote:It is nice to know a little bit of math, simply because many problems (like the one above) are strictly matematical in it's nature and already solved, just need to think what is the proper problem description ;-)
Definitely, for sure. From your assessment, did I do basically what you suggested?
Post Reply