Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

enter image description here

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");
    }
}
share|improve this question
    
I think you need to provide a little bit more information about what those variables mean. for example what is RightYIntercept? –  Ali.S Jul 9 '12 at 0:20
    
@Gajet Hm. In retrospect, you're probably right. –  Raven Dreamer Jul 9 '12 at 0:22

1 Answer 1

up vote 2 down vote accepted

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;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.