Skip to main content

Collisions with line intersections

Collisions can be determined by testing for line intersections. Imagine a line representing the ground and an object above, falling down. At time t1, take the center point p1 of the object. At time t2, move the object down and take the center point p2 of the object.

If the line formed by endpoints (p1, p2) intersect the ground line, a collision has occurred. On a collision, reset the object to its original position before the movement.

When a collision occurs, be sure to reset the object by the same point used to determine the line intersection. In this example, I use the center point, so the object should be reset by the center point, not its actual x, y (usually top left) coordinate. It is not always the case that resetting by the x, y coordinate is sufficient. This can be seen in the following picture.


It can be seen that even if the object isn't moving, but its size changes, a line intersection can occur, and resetting the object to its x, y position will leave the object's center point below the ground. This results in falling through the floor. Subsequent move operations will not detect a line intersection, and the object will continue to fall.

Resetting by the object's center point (the same point used to detect the line intersection) results in correct collision handling.

This can be extended to sloped lines. Move on x and test for a line intersection. If an intersection occurs, reset the object's position and determine the arctangent theta of the line. Move the object on x by the cosine of theta, and move on y by the sine of theta. Test again for a line intersection. If an intersection occurs, reset the object and stop.


Think about why this works. On flat ground, the entirety of our horizontal momentum is along the x-axis. If we hit a 45 degree slope, half of our momentum should be converted into upward movement in y, and the other half in x. This is exactly what sine and cosine do.

Cosine tells us the percentage of our energy that we apply in x, and sine tells us the percentage of energy applied in y.

Hitting a 90 degree wall converts our horizontal momentum entirely into the y-axis and we move upward. Cosine shows that 0% of our momentum is in x, and sine shows that 100% is in y.

This is how trigonometry lets us walk on slopes.

Comments

Popular posts from this blog

Water Pea Care and Instructions

Please treat your water pea like a real pet fish. Feed daily! Click here to get the Water Pea android app. You will receive a single water pea egg. Drop the egg in the water and your baby pea will hatch. If your water pea is alive after 1 da y, it will produce an egg that you can harvest . Care and Instructions Birth When you place the water pea egg in water, it will take a few seconds for the egg to dissolve. Initially, the newborn water pea will be tiny and hard to see. Take care not to accidentally pick it up and drop it out of the tank. Water peas live for about 3 days if properly fed. Feeding Your water pea must be fed every day, or it will die. To be safe, feed it once in the morning and again at night. Water peas recycle vitamin B by producing caecal pellets . Wait for your water pea to produce the pellet and then collect it so you don't run out of food. This is their only source of nutrition! To collect the pellets, drag them to the top of the ...

Moving with Trigonometry

These are some thoughts on Trigonometry that I wanted to write down, and someone, somewhere may want to read. But probably not. If you're not into it, turn back immediately. In a top-down view on a two dimensional plane, you can face any direction and move in a forward direction by moving on the x-axis by the cosine of your rotation and moving on the y-axis by the sine of your rotation. This seems to require a physical orientation in the world, but this physical limitation is only on the surface. We can take one step back and use these abilities in more subtle ways. Imagine a side view on a plane in which you can move left and right, and jump up and fall back down to the ground. Let us first think of the left and right movement in terms of trigonometry. Facing right means 0 degrees and so we can move on x by the cosine of our rotation. Facing left is 180 degrees, and we move on x by the cosine of our rotation. Let's maintain a logical rotation factor called xf . When sta...