I have a 2D map, for each element I apply this isometric projection to place my Sprite

 //Element e;
 float[] f= projection(e.getX(), e.getY() ,z);
         // x and y represent Sprite Coordonate (tile_width and height depend of my 
         // camera size and the number of elements in x and in y
 float x =  f[0]*tile_width;
 float y =  f[1]*tile_height;

   public float[] projection(float x, float y, float z)
     return new float[]{  (( x )-(y) ) , ((x/2) + (y/2) - z  )}; 

the sprite for one element :

enter image description here

The result of my projection :

enter image description here

The problem is I need to add an offset of tile_height/2 to the y and tile_width/2 to the x to have something like this (in the red rectangle I drawed with paint what I want) :

enter image description here

Where did I make wrong?

(I found the projection method in How should I sort images in an isometric game so that they appear in the correct order? )

edit : How I calculate my tile_height and tile_width :

    tile_height = (float) ((CAMERA_HEIGHT/(game.map.getHeight()/2)));
    tile_width =  (float) ((CAMERA_HEIGHT/(game.map.getWidth()/2)));
  • \$\begingroup\$ I haven't done isometric projection before, but shouldn't the tile_height and tile_width be the height and width of the tile sprite? \$\endgroup\$ – tugs Nov 21 '12 at 22:13

I am not sure how to create a single projection for multiplying with width and height, but I believe if you send the height and width along, the Projection would be:

public float[] projection(float x, float y, float z, float width, float height)
  return new float[]{  
    (x * width / 2) + (y * width / 2),
    (y * height / 2) - (x * height / 2) - (z*height)

Maybe have a look at 1ManBandStudios' post here.

I hope this helps :)

| improve this answer | |
  • \$\begingroup\$ I notice an interesting difference in the calculations here. Your code calculates the projection as [x+y, y-x-z] where the code in the question uses [x-y, x+y-z] \$\endgroup\$ – tugs Nov 20 '12 at 23:05
  • \$\begingroup\$ That doesn't matter - it just changes the orientation (which direction is x/y). \$\endgroup\$ – Mario Nov 21 '12 at 0:55
  • \$\begingroup\$ It doesn't work \$\endgroup\$ – Christophe Debove Nov 21 '12 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.