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

  • \$\begingroup\$ 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... \$\endgroup\$ – Savlon Mar 14 '14 at 5:43
  • \$\begingroup\$ 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. \$\endgroup\$ – Ottomic Mar 14 '14 at 6:07
  • \$\begingroup\$ Shouldn't eucldean distance be good enough for this? \$\endgroup\$ – Vaughan Hilts Mar 14 '14 at 6:30
  • \$\begingroup\$ @VaughanHilts No, because something directly above is actually further away then something above and slightly to the left. \$\endgroup\$ – AturSams Mar 14 '14 at 8:59
  • \$\begingroup\$ 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. \$\endgroup\$ – AturSams Mar 14 '14 at 9:03

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.

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

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.