Skip to main content
Corrected direction of vector T, I want direction to be from player to platform
Source Link
Hatoru Hansou
  • 1.3k
  • 10
  • 17
T = PlayerAABBPlatformAABB.position - PlatformAABBPlayerAABB.position

Vector from Platform to Player.

If you do not want to use an structure/class to represent vectors, then another option is to use separate variables for x and y elements:

Tx = playerplatform.x - platformplayer.x
Ty = playerplatform.y - platformplayer.y
Tx = (playerplatform.left + playerplatform.width / 2) - (platformplayer.left + platformplayer.width / 2)
Ty = (playerplatform.top + playerplatform.height / 2) - (platformplayer.top + platformplayer.height / 2)
T = PlayerAABB.position - PlatformAABB.position

If you do not want to use an structure/class to represent vectors, then another option is to use separate variables for x and y elements:

Tx = player.x - platform.x
Ty = player.y - platform.y
Tx = (player.left + player.width / 2) - (platform.left + platform.width / 2)
Ty = (player.top + player.height / 2) - (platform.top + platform.height / 2)
T = PlatformAABB.position - PlayerAABB.position

Vector from Platform to Player.

If you do not want to use an structure/class to represent vectors, then another option is to use separate variables for x and y elements:

Tx = platform.x - player.x
Ty = platform.y - player.y
Tx = (platform.left + platform.width / 2) - (player.left + player.width / 2)
Ty = (platform.top + platform.height / 2) - (player.top + player.height / 2)
edited formula to make it more readable and a bug correction
Source Link
Hatoru Hansou
  • 1.3k
  • 10
  • 17
if (penxpeny <<= penypenx)
{
 currentSpr.left -= penx *if (T.xT[1] >=< 0) ?spr1.top 1+= :peny;
 -1) / 2; else
  else currentSpr if (T[1] > 0) spr1.top -= penypeny;
} *else
{
    if (T.yT[0] >=< 0) ?spr1.left 1+= :penx;
 -1   else
    if (T[0] > 0) /spr1.left 2;-= penx;
}

(formula edited to make it more readable)

if (penx < peny) currentSpr.left -= penx * (T.x >= 0 ? 1 : -1) / 2;
  else currentSpr.top -= peny * (T.y >= 0 ? 1 : -1) / 2;
if (peny <= penx)
{
    if (T[1] < 0) spr1.top += peny;
    else
    if (T[1] > 0) spr1.top -= peny;
} else
{
    if (T[0] < 0) spr1.left += penx;
    else
    if (T[0] > 0) spr1.left -= penx;
}

(formula edited to make it more readable)

Only marked the formulas as code because stackexchange does not preserve spacing and linebreaking otherwise
Source Link
Hatoru Hansou
  • 1.3k
  • 10
  • 17

T = PlayerAABB.position - PlatformAABB.position

T = PlayerAABB.position - PlatformAABB.position

Tx = player.x - platform.x Ty = player.y - platform.y

Tx = player.x - platform.x
Ty = player.y - platform.y

Tx = (player.left + player.width / 2) - (platform.left + platform.width / 2) Ty = (player.top + player.height / 2) - (platform.top + platform.height / 2)

Tx = (player.left + player.width / 2) - (platform.left + platform.width / 2)
Ty = (player.top + player.height / 2) - (platform.top + platform.height / 2)

To detect if boxes overlap if (absolute(T.x) > (currentSpr.width / 2) + (otherSpr.width / 2)) stop here; if (absolute(T.y) > (currentSpr.height / 2) + (otherSpr.height / 2)) stop here;

if (absolute(T.x) > (currentSpr.width / 2) + (otherSpr.width / 2)) stop here;
if (absolute(T.y) > (currentSpr.height / 2) + (otherSpr.height / 2)) stop here;

penx = (currentSpr.width / 2) + (otherSpr.width / 2) - absolute(T.x); peny = (currentSpr.height / 2) + (otherSpr.height / 2) - absolute(T.y);

penx = (currentSpr.width / 2) + (otherSpr.width / 2) - absolute(T.x);
peny = (currentSpr.height / 2) + (otherSpr.height / 2) - absolute(T.y);

if (penx < peny) currentSpr.left -= penx * (T.x >= 0 ? 1 : -1) / 2; else currentSpr.top -= peny * (T.y >= 0 ? 1 : -1) / 2;

if (penx < peny) currentSpr.left -= penx * (T.x >= 0 ? 1 : -1) / 2;
  else currentSpr.top -= peny * (T.y >= 0 ? 1 : -1) / 2;

T = PlayerAABB.position - PlatformAABB.position

Tx = player.x - platform.x Ty = player.y - platform.y

Tx = (player.left + player.width / 2) - (platform.left + platform.width / 2) Ty = (player.top + player.height / 2) - (platform.top + platform.height / 2)

To detect if boxes overlap if (absolute(T.x) > (currentSpr.width / 2) + (otherSpr.width / 2)) stop here; if (absolute(T.y) > (currentSpr.height / 2) + (otherSpr.height / 2)) stop here;

penx = (currentSpr.width / 2) + (otherSpr.width / 2) - absolute(T.x); peny = (currentSpr.height / 2) + (otherSpr.height / 2) - absolute(T.y);

if (penx < peny) currentSpr.left -= penx * (T.x >= 0 ? 1 : -1) / 2; else currentSpr.top -= peny * (T.y >= 0 ? 1 : -1) / 2;

T = PlayerAABB.position - PlatformAABB.position
Tx = player.x - platform.x
Ty = player.y - platform.y
Tx = (player.left + player.width / 2) - (platform.left + platform.width / 2)
Ty = (player.top + player.height / 2) - (platform.top + platform.height / 2)

To detect if boxes overlap

if (absolute(T.x) > (currentSpr.width / 2) + (otherSpr.width / 2)) stop here;
if (absolute(T.y) > (currentSpr.height / 2) + (otherSpr.height / 2)) stop here;
penx = (currentSpr.width / 2) + (otherSpr.width / 2) - absolute(T.x);
peny = (currentSpr.height / 2) + (otherSpr.height / 2) - absolute(T.y);
if (penx < peny) currentSpr.left -= penx * (T.x >= 0 ? 1 : -1) / 2;
  else currentSpr.top -= peny * (T.y >= 0 ? 1 : -1) / 2;
Added formulas and demo video of how it is supposed to work
Source Link
Hatoru Hansou
  • 1.3k
  • 10
  • 17
Loading
Source Link
Hatoru Hansou
  • 1.3k
  • 10
  • 17
Loading