# Measuring distances on 2D perspective beat'em'up

I'm making a 2-D beat'em up. The tech demo is here.

As you can see, the game is in perspective, pretty much like all old school brawlers. What I want to do is measure the distance between objects on the level on screen. The game is made on Construct2, so AFAIK the only distances I can work out are absolute and on a straight line, without accounting for perspective. Ideally, I would like a function that says "if this is here and this is here, this is the real distance between these two objects".

I've been reading about perspective matrixes but can't for the life of me understand what they are or how could I use it. I'm not afraid to read though, so if you could point me to something (book, website) that can explain how to do that in layman's terms I'd be most grateful.

Thank you! :D

• Nothing to do with this question but... if you play your game and hold down right and then push the left arrow key while still holding down the right key, you totally moon walk lol... – Savlon Mar 14 '14 at 5:43
• Yeeeah, everything is still incredibly rough around the edges. Pay no mind to the awfully shoddy collision detection and placeholder sprites, please. I promise it'll get better as soon as I can lay down all entities properly. – Ottomic Mar 14 '14 at 6:07
• Shouldn't eucldean distance be good enough for this? – Vaughan Hilts Mar 14 '14 at 6:30
• @VaughanHilts No, because something directly above is actually further away then something above and slightly to the left. – AturSams Mar 14 '14 at 8:59
• It would be somewhat hard to answer correctly cause it is not clear how you defined your game level space and I suppose it is possible you didn't take the math into consideration when doing so. In order for use to provide you with a formula to calculate distance in your game world, we need to know the math behind your game world which we don't (cause we didn't implement your game) and you don't because you didn't take math into consideration. That being said I can give you a rough approximation assuming those things are supposed to be perfect squares. – AturSams Mar 14 '14 at 9:03

## 1 Answer

Normally (x^2 + y^2)^0.5 would give you the distance, however your y vector is not straight up and down. It actually represents the true vertical with an additional horizontal component. First, find the angle difference between vertical on the screen and vertical in the game world (probably around 20 degrees.) We'll call this o.

sin o * y = a (the real vertical offset)

cos o * a = b (an additional horizontal offset that needs to be included)

The distance should be: ((x + b)^2 + a^2)^0.5

See if that works (I might have switched a vector by mistake).

Anyway, the answer is going to require some trigonometry.

• Thanks a lot! Gonna try this out and be back with the results. – Ottomic Mar 15 '14 at 12:46
• Go for it, I might have missed a negative sign or two, but the logic should be sound. – Hoytman Mar 17 '14 at 18:32