Maths questions if anyone can help a brother out

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Jakus
Prole
Posts: 6
Joined: Tue May 28, 2013 7:56 am

Maths questions if anyone can help a brother out

Post by Jakus »

Hey just wondering if anyone could help me with the maths for Oblique/"Military" Projection?
Image
Like perspective view.

Things such as screen x,y to grid coordinates and the other way around. Whats the best formula for both in anyone's experience?

As much as you can teach me is awesome, thanks
User avatar
ivan
Party member
Posts: 1923
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Maths questions if anyone can help a brother out

Post by ivan »

I assume you mean converting a point to an index in an 'isometric' projection and vice versa.
Interestingly, with a lot of isometric games the tilt angle is 30 degrees not 45.
Take a look at the bottom of this page: http://2dengine.com/doc_1_3_10/gs_isometric.html
It shows you how to convert the point for 30-degree tilt.
Also note that there are different types of isometric layouts.

Generally, if you want to be able to do the math for ANY tilt angle, you want to research "affine transformations".
There is a nice implementation in the "path" library by Cosmin Apreutesei: https://code.google.com/p/lua-files/wiki/affine2d
Basically you translate, scale and rotate the original point to match your projection (note that order of transformations is important).

So if you're doing something simple you can probably get away without using matrices:

Code: Select all

-- translation by offset ox, oy
function translate(x, y, ox, oy)
  return x + ox, y + oy
end

-- scaling by sx, sy
function scale(x, y, sx, sy)
  return x*sx, y*sy
end

-- rotation by a in radians
local cos, sin = math.cos, math.sin
function rotate(x, y, a)
  local c = cos(a)
  local s = sin(a)
  return c*x - s*y, s*x + c*y
end
With, matrices the math is a little more complicated because these three operations are combined together.
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Maths questions if anyone can help a brother out

Post by arampl »

Recently I've found interesting paper on SSR method (scale-shear-rotate) to do isometric projection. It is important that shearing is involved in the process:

http://design.tutsplus.com/tutorials/ho ... ector-1058
Jakus
Prole
Posts: 6
Joined: Tue May 28, 2013 7:56 am

Re: Maths questions if anyone can help a brother out

Post by Jakus »

Thank you both but I'm actually looking at the 45 Degree version; Like Ultima Online.

Have either of you had experience or know how or where I could learn to do that?
User avatar
ivan
Party member
Posts: 1923
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Maths questions if anyone can help a brother out

Post by ivan »

For 45-degrees, you can just rotate the point math.pi/2, no scaling/skewing involved.
As I said, there are different types of isometric layouts (diamond, staggered, etc).
Generally speaking you could probably get away with something like:

Code: Select all

screenX, screenY = ... -- get mouse coords
-- screen coords to map coords
mapX, mapY = translate(screenX, screenY, -mapOriginX, -mapOriginY)
mapX, mapY = rotate(mapX, mapY, math.pi/2)
-- map coords to tile index
tileX, tileY = floor(mapX/tileWidth) + 1, floor(mapY/tileHeight) + 1
Assuming diamond ordering and the 'mapOrigin' is the top left corner of the diamond in screen coords.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Maths questions if anyone can help a brother out

Post by davisdude »

This is a good video too, you'll learn a lot about some of the techniques used. Kind of lengthy, but worth it.
https://www.youtube.com/watch?v=go1qrWFw_bs
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests