How can I resolve collisions at different speeds, depending on the direction?
- by Raven Dreamer
I have, for all intents and purposes, a Triangle class that objects in my scene can collide with (In actuality, the right side of a parallelogram).
My collision detection and resolution code works fine for the purposes of preventing a gameobject from entering into the space of the Triangle, instead directing the movement along the edge.
The trouble is, the maximum speed along the x and y axis is not equivalent in my game, and moving along the Y axis (up or down) should take twice as long as an equivalent distance along the X axis (left or right).
Unfortunately, these speeds apply to the collision resolution too, and movement along the blue path above progresses twice as fast. What can I do in my collision resolution to make sure that the speedlimit for Y axis movement is obeyed in the latter case?
Collision Resolution for this case below (vecInput and velocity are the position and velocity vectors of the game object):
// y = mx+c
// solve for y. M = 2, x = input's x coord, c = rightYIntercept
lowY = 2*vecInput.x + parag.rightYIntercept ;
...
else
{
// y = mx+c
// vecInput.y = 2(x) + RightYIntercept
// (vecInput.y - RightYIntercept) / 2 = x;
//if velocity.Y (positive) greater than velocity.X (negative)
//pushing from bottom, so push right.
if(velocity.y > -1*velocity.x)
{
//change the input vector's x position to match the
//y position on the shape's edge. Formula for line: Y = MX+C
// M is 2, C is rightYIntercept, y is the input y, solve for X.
vecInput = new Vector2((vecInput.y - parag.rightYIntercept)/2, vecInput.y);
Debug.Log("adjusted rightwards");
}
else
{
vecInput = new Vector2( vecInput.x, lowY);
Debug.Log("adjusted downwards");
}
}