Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

How do I reverse this function?

$
0
0

I’m working on an isometric tile rendering engine and I need to reverse a function I used to translate the Cartesian coordinates into isometric coordinates. I’ve already written a function for this, but I’ve been getting a strange logical error and I think it must be somewhere in my code for translating from isometric coordinates back into Cartesian. Anyways, here’s the code for translating Cartesian->Isometric

    private Vector2 cartToIso(Vector2 init){
        Vector2 iso = new Vector2();
        iso.x = (((init.x - init.y) * TILE_WIDTH_HALF))+X_OFFSET;
        iso.y = MAP_HEIGHT-TILE_HEIGHT-((init.x + init.y) * TILE_HEIGHT_HALF);  
        return iso;  
    }

Any suggestions would be appreciated. Also if anyone was wondering, what I’m trying to do right now is create an algorithm for tile picking in my engine. The API(LibGDX) is a little strange in the sense that it uses a bottom-up Y coordinate system but with any of the input(ie, clicks) it uses a top-down Y coordinate system. I can post the code I have for tile picking later if anyone thinks they can help me more, but I’ll save it for now since it’s pretty long.

Edit:
This is the method I use to reverse it, and it seems to be giving me the incorrect results. Does this look correct?

    private Vector2 isoToCart(int x, int y){
        Vector2 cart = new Vector2();
        x = x-X_OFFSET;
        y = y-MAP_HEIGHT;
        cart.x = (x / TILE_WIDTH_HALF + (y / TILE_HEIGHT_HALF)) /2;
        cart.y = (y / TILE_HEIGHT_HALF -(x / TILE_WIDTH_HALF)) /2;
        return cart;
    }

Viewing all articles
Browse latest Browse all 434

Trending Articles