Jump to content
A 2021 backup has been restored. Forums are closed and work in progress. Join our Discord server for more updates! ×
SoaH City Message Board

Physics issues


justin123

Recommended Posts

I having trouble with curved slopes. The player makes it half way but gets stuck in the middle of the curve.

71865946.png32892255.png

The physics my engine uses is a direct port of the revival engine. I only had to change the way loops work since the Angel Script language doesn't have a "repeat()" loop or "do{}until()" loop.

but here's the physics function:

sorry about the formating

void Physics()
{
		// X Movement
		x += cos(degtorad(angle)) * x_speed;
		y += sin(degtorad(angle)) * x_speed;

		while (CollisionLeft( x, y, angle, maskMid) && x_speed < 0)
		{
			x += cos(degtorad(angle));
			y -= cos(degtorad(angle));
		}

		while (CollisionRight( x, y, angle, maskMid) && x_speed > 0)
		{
			x -= cos(degtorad(angle));
			y += cos(degtorad(angle));
		}

		//Y Movement
		if (!ground)
		{
			y += y_speed;

			// move the player outside in case he has got stuck into the floor or the ceiling           
            while (y_speed < 0 && CollisionTop( x, y, 0, maskMid ) == true )
            {
				x   -=  sin(degtorad(angle));
                y   +=  cos(degtorad(angle));

				//y += 1;
            }            

            while (y_speed > 0 && CollisionBottom( x, y, 0, maskMid ) == true )
            {
				x   +=  sin(degtorad(angle));
                y   -=  cos(degtorad(angle));

				//y -= 1;
            }

			if (y_speed >= 0 && CollisionBottom( x, y, 0, maskBig ) == true)
            {
                if ( CollisionLeftLimiter(0.0f) && CollisionRightLimiter(0.0f) )
                {
					ChangeAngle(GetAngle( x, y, 0 ), gravity_angle);
				}
                else
				{
					ChangeAngle(0, gravity_angle);
				}

                    x_speed -=  sin(degtorad(angle)) * y_speed;
                    y_speed =   0;
                    ground  =   true;
            }
		}

		while (CollisionLeft( x, y, angle, maskMid) && x_speed < 0)
		{
			x += cos(degtorad(angle));
			y -= cos(degtorad(angle));
		}

		while (CollisionRight( x, y, angle, maskMid) && x_speed > 0)
		{
			x -= cos(degtorad(angle));
			y += cos(degtorad(angle));
		}

		//Slopes
		if (ground == true)
        {
            if (CollisionMain( x, y ) )
            {
                while(CollisionMain( x, y))
                {
                    x   -=  sin(degtorad(angle));
                    y   -=  cos(degtorad(angle));
                }
            }

            if (CollisionSlope( x, y, angle, maskMid ) && !CollisionMain( x, y ) )
            {
                while(!CollisionMain( x, y ))
                {
                    x   +=  sin(degtorad(angle));
                    y   +=  cos(degtorad(angle));
                }
            }
        }

		//Other

		//fall if there isn't enough speed
		if (angle > 80 && angle < 280 && ground == true && abs(x_speed) < 3)
        {
            y_speed =   -sin(degtorad(angle))*x_speed;
            x_speed =   cos(degtorad(angle))*x_speed;
            ground  =   false;
        }

        // fall off the ground if the edges aren't colliding
        if (ground == true && angle != 0 &&
            (CollisionLeftLimiter(angle ) == false || CollisionRightLimiter(angle ) == false  ))
        {
            y_speed =   -sin(degtorad(angle))*x_speed;
            x_speed =   cos(degtorad(angle))*x_speed;
            ground  =   false;
        }        

        // get new angle
        if (ground == true && CollisionLeftLimiter(angle) && CollisionRightLimiter(angle))
        {
            // Store the new angle
            angle_holder   =   GetAngle( x, y, angle );

            // Check if difference is less than 45. If it is, linear interpolate the angle, so it results on smoother rotation.
            // Otherwise, set the new angle normally. Remember that linear interpolation formula is the next:
            //
            // final = a*t + b*(1-t); where t is the interpolation value, wich goes from 0 to 1
            //
            // There's also a shorter and faster method, wich is the one we're going to use:
            //
            // final = a + (b-a)*t;

            if (abs(angle-angle_holder)<45) ChangeAngle(angle + (angle_holder-angle)*0.5, gravity_angle);
            else                            ChangeAngle(angle_holder, gravity_angle);
        } else {
            ChangeAngle(0, gravity_angle);
        }
}

For some reason I can get the player to get unstuck and traverse the curve if I first get stuck for a while and press the opposite arrow key (releasing the other). Then thinking that it will go in the opposite direction, instead it makes it through the other half of the curve.

Does anyone have any ideas?

Link to comment
Share on other sites

The images wasn't showing in my first post but its fixed now.

What seems to be happening is somewhere around angles like 45 or 315, sonic begins to move in the opposite direction. So eventually he gets stuck in the middle. I can't figure out what could be causing this. My math functions output the same values as Game Maker's and collision is pretty much the same (but slower). I even went as far as testing the Revival Engine in debug mode and my own engine side-by-side in a identical map. the speed variables seemed to react the same and angles looked the same up to the point where you get stuck. This driving me crazy!!!

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...