# How can I resolve collisions at different speeds, depending on the direction?

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");
}
}

• I think you need to provide a little bit more information about what those variables mean. for example what is RightYIntercept? – Ali1S232 Jul 9 '12 at 0:20
• @Gajet Hm. In retrospect, you're probably right. – Raven Dreamer Jul 9 '12 at 0:22

## 1 Answer

assuming vecInput is your final result for this problem I suggest you add 4 lines of code at the end of your code. checking if you the results exceed in either x axis value or y axis value.

if (math.Abs(vecInput.x) > MaxXSpeed)
vecInput *= maxXSpeed / vecInput.x;
if (math.Abs(vecInput.y) > MaxYSpeed)
vecInput *= maxYSpeed / vecInput.y;