Skip to main content
deleted 8 characters in body
Source Link
dimitris93
  • 1.3k
  • 2
  • 16
  • 27

I will give you a different approach you might wanna try.

How i would do this is I would haveuse shapes for the objects, a. A rectangle for the terrain and then a, a rectangle for the bottom half of the worm and a circle for the top half of the worm. So the worm is a combination of 2 shapes. Now, if the user presses left worm gets a (-5,0) velocity if the user pressed up the worm gets a (0,-5) velocity. After the velocity is applied, you check for collisions using trigonometry, and if any of the 2 shapes of the worm collide with the terrain, then you move it outside of the terrain, by moving the worm up. So you basically force it not to be inside the terrain. This velocity would have to be calculated according to the angle of the terrain, otherwise your worm would move very quickly on terrains placed at high angles.

For example, according to the image in your question, if the user pressed left, the worm would get a (-5,0) velocity applied to it, so it would move 5 pixels to the left. But then it would be inside the terrain, so we would have to force it outside of the terrain by moving it k pixels up. You will have to calculate k using Euclid theorem. So not only the worm will never be inside the terrain. But, but it will also move as a result of forcing it outside of the terrain.

So, basically the worm goes inside the terrain, and then the terrain pushes it upwards (only). That way your worm is never drawn inside the terrain, and also it moves.

You will simply need collision detection of arotated rectangle with an angle with a rectangle/circle. Or instead of a "rectangle" you could check collisions with 4 lines. That way you would not only solve rectangle with an angle - rectangle/circle collision detection, but any given polygon - circle/rectangle intersection, which you might need in the future. Because a polygon is nothing other than N lines. Now, the problem with that is that if your worm has a big velocity, your worm might pass right through the terrain, because lines are thin. For that to happen you worm would need to have a velocity of greater than 2radius of the circle, or greater than 2width of the rectangle (but in reality that is really unlikely, your worm would have to move like 2550 pixels per secondframe for that to happen, which is too high for a worm anyway). The "circle" and "rectangle" i am talking about are the shapes of the worm. This problem is known as continuous collision detection.

Not that it is necessary, but if you had a physics engine with collision detection functions, you could check polygon collisions much easier. And probably a tiny bit more efficiently than any code you will write alone, unless you are familiar with mathematics.

I will give you a different approach you might wanna try.

How i would do this is I would have shapes for the objects, a rectangle for the terrain and then a rectangle for the bottom half of the worm and a circle for the top of the worm. So the worm is a combination of 2 shapes. Now, if the user presses left worm gets a (-5,0) velocity if the user pressed up the worm gets a (0,-5) velocity. After the velocity is applied, you check for collisions using trigonometry, and if any of the 2 shapes of the worm collide with the terrain, then you move it outside of the terrain, by moving the worm up. So you basically force it not to be inside the terrain. This velocity would have to be calculated according to the angle of the terrain, otherwise your worm would move very quickly on terrains placed at high angles.

For example, according to the image in your question, if the user pressed left, the worm would get a (-5,0) velocity applied to it, so it would move 5 pixels to the left. But then it would be inside the terrain, so we would have to force it outside of the terrain by moving it k pixels up. You will have to calculate k using Euclid theorem. So not only the worm will never be inside the terrain. But it will also move as a result of forcing it outside of the terrain.

So, basically the worm goes inside the terrain, and then the terrain pushes it upwards (only). That way your worm is never drawn inside the terrain, and also it moves.

You will simply need collision detection of a rectangle with an angle with a rectangle/circle. Or instead of a "rectangle" you could check collisions with 4 lines. That way you would not only solve rectangle with an angle - rectangle/circle collision detection, but any given polygon - circle/rectangle intersection, which you might need in the future. Because a polygon is nothing other than N lines. Now, the problem with that is that if your worm has a big velocity, your worm might pass right through the terrain, because lines are thin. For that to happen you worm would need to have a velocity of greater than 2radius of the circle, or greater than 2width of the rectangle (but in reality that is really unlikely, your worm would have to move like 25 pixels per second for that to happen, which is too high for a worm anyway). The "circle" and "rectangle" i am talking about are the shapes of the worm. This problem is known as continuous collision detection.

Not that it is necessary, but if you had a physics engine with collision detection functions, you could check polygon collisions much easier. And probably a tiny bit more efficiently than any code you will write alone, unless you are familiar with mathematics.

I will give you a different approach you might wanna try.

How i would do this is I would use shapes for the objects. A rectangle for the terrain, a rectangle for the bottom half of the worm and a circle for the top half of the worm. So the worm is a combination of 2 shapes. Now, if the user presses left worm gets a (-5,0) velocity if the user pressed up the worm gets a (0,-5) velocity. After the velocity is applied, you check for collisions using trigonometry, and if any of the 2 shapes of the worm collide with the terrain, then you move it outside of the terrain, by moving the worm up. So you basically force it not to be inside the terrain. This velocity would have to be calculated according to the angle of the terrain, otherwise your worm would move very quickly on terrains placed at high angles.

For example, according to the image in your question, if the user pressed left, the worm would get a (-5,0) velocity applied to it, so it would move 5 pixels to the left. But then it would be inside the terrain, so we would have to force it outside of the terrain by moving it k pixels up. You will have to calculate k using Euclid theorem. So not only the worm will never be inside the terrain, but it will also move as a result of forcing it outside of the terrain.

So, basically the worm goes inside the terrain, and then the terrain pushes it upwards (only). That way your worm is never drawn inside the terrain, and also it moves.

You will simply need collision detection of rotated rectangle with a rectangle/circle. Or instead of a "rectangle" you could check collisions with 4 lines. That way you would not only solve rectangle with an angle - rectangle/circle collision detection, but any given polygon - circle/rectangle intersection, which you might need in the future. Because a polygon is nothing other than N lines. Now, the problem with that is that if your worm has a big velocity, your worm might pass right through the terrain, because lines are thin. For that to happen you worm would need to have a velocity of greater than 2radius of the circle, or greater than 2width of the rectangle (but in reality that is really unlikely, your worm would have to move like 50 pixels per frame for that to happen, which is too high for a worm anyway). The "circle" and "rectangle" i am talking about are the shapes of the worm. This problem is known as continuous collision detection.

Not that it is necessary, but if you had a physics engine with collision detection functions, you could check polygon collisions much easier. And probably a tiny bit more efficiently than any code you will write alone, unless you are familiar with mathematics.

added 6 characters in body
Source Link
dimitris93
  • 1.3k
  • 2
  • 16
  • 27

I will give you a different approach you might wanna try.

How i would do this is I would have shapes for the objects, a rectangle for the terrain and then a rectangle for the bottom half of the worm and a circle for the top of the worm. So the worm is a combination of 2 shapes. Now, if the user presses left worm gets a (-5,0) velocity if the user pressed up the worm gets a (0,-5) velocity. After the velocity is applied, you check for collisions using trigonometry, and if any of the 2 shapes of the worm collide with the terrain, then you move it outside of the terrain, by moving the worm up. So you basically force it not to be inside the terrain. This velocity would have to be calculated according to the angle of the terrain, otherwise your worm would move very quickly on terrains placed at high angles.

For example, according to the image in your question, if the user pressed left, the worm would get a (-5,0) velocity applied to it, so it would move 5 pixels to the left. But then it would be inside the terrain, so we would have to force it outside of the terrain by moving it k pixels up. You will have to calculate k using Euclid theorem. So not only the worm will never be inside the terrain. But it will also move as a result of forcing it outside of the terrain.

So, basically the worm goes inside the terrain, and then the terrain pushes it upwards (only). That way your worm is never drawn inside the terrain, and also it moves.

You will simply need collision detection of a rectangle with an angle with a rectangle/circle. Or instead of a "rectangle" you could check collisions with 4 lines. That way you would not only solve rectangle with an angle - rectangle/circle collision detection, but any given polygon - circle/rectangle intersection, which you might need in the future. Because a polygon is nothing other than N lines. Now, the problem with that is that if your worm has a big velocity, your worm might pass right through the terrain, because lines are thin. For that to happen you worm would need to have a velocity of greater than 2*radius2radius of the circle, or greater than 2width of the circle, or greater than the rectangle's smallest siderectangle (but in reality that is really unlikely, your worm would have to move like 25 pixels per second for that to happen, which is too high for a worm anyway). The "circle" and "rectangle" i am talking about are the shapes of the worm. This problem is known as continuous collision detection.

Not that it is necessary, but if you had a physics engine with collision detection functions, you could check polygon collisions much easier. And probably a tiny bit more efficiently than any code you will write alone, unless you are familiar with mathematics.

I will give you a different approach you might wanna try.

How i would do this is I would have shapes for the objects, a rectangle for the terrain and then a rectangle for the bottom half of the worm and a circle for the top of the worm. So the worm is a combination of 2 shapes. Now, if the user presses left worm gets a (-5,0) velocity if the user pressed up the worm gets a (0,-5) velocity. After the velocity is applied, you check for collisions using trigonometry, and if any of the 2 shapes of the worm collide with the terrain, then you move it outside of the terrain, by moving the worm up. So you basically force it not to be inside the terrain.

For example, according to the image in your question, if the user pressed left, the worm would get a (-5,0) velocity applied to it, so it would move 5 pixels to the left. But then it would be inside the terrain, so we would have to force it outside of the terrain by moving it k pixels up. You will have to calculate k using Euclid theorem. So not only the worm will never be inside the terrain. But it will also move as a result of forcing it outside of the terrain.

So, basically the worm goes inside the terrain, and then the terrain pushes it upwards (only). That way your worm is never drawn inside the terrain, and also it moves.

You will simply need collision detection of a rectangle with an angle with a rectangle/circle. Or instead of a "rectangle" you could check collisions with 4 lines. That way you would not only solve rectangle with an angle - rectangle/circle collision detection, but any given polygon - circle/rectangle intersection, which you might need in the future. Because a polygon is nothing other than N lines. Now, the problem with that is that if your worm has a big velocity, your worm might pass right through the terrain, because lines are thin. For that to happen you worm would need to have a velocity of greater than 2*radius of the circle, or greater than the rectangle's smallest side (but in reality that is really unlikely, your worm would have to move like 25 pixels per second for that to happen, which is too high for a worm anyway). The "circle" and "rectangle" i am talking about are the shapes of the worm. This problem is known as continuous collision detection.

Not that it is necessary, but if you had a physics engine with collision detection functions, you could check polygon collisions much easier. And probably a tiny bit more efficiently than any code you will write, unless you are familiar with mathematics.

I will give you a different approach you might wanna try.

How i would do this is I would have shapes for the objects, a rectangle for the terrain and then a rectangle for the bottom half of the worm and a circle for the top of the worm. So the worm is a combination of 2 shapes. Now, if the user presses left worm gets a (-5,0) velocity if the user pressed up the worm gets a (0,-5) velocity. After the velocity is applied, you check for collisions using trigonometry, and if any of the 2 shapes of the worm collide with the terrain, then you move it outside of the terrain, by moving the worm up. So you basically force it not to be inside the terrain. This velocity would have to be calculated according to the angle of the terrain, otherwise your worm would move very quickly on terrains placed at high angles.

For example, according to the image in your question, if the user pressed left, the worm would get a (-5,0) velocity applied to it, so it would move 5 pixels to the left. But then it would be inside the terrain, so we would have to force it outside of the terrain by moving it k pixels up. You will have to calculate k using Euclid theorem. So not only the worm will never be inside the terrain. But it will also move as a result of forcing it outside of the terrain.

So, basically the worm goes inside the terrain, and then the terrain pushes it upwards (only). That way your worm is never drawn inside the terrain, and also it moves.

You will simply need collision detection of a rectangle with an angle with a rectangle/circle. Or instead of a "rectangle" you could check collisions with 4 lines. That way you would not only solve rectangle with an angle - rectangle/circle collision detection, but any given polygon - circle/rectangle intersection, which you might need in the future. Because a polygon is nothing other than N lines. Now, the problem with that is that if your worm has a big velocity, your worm might pass right through the terrain, because lines are thin. For that to happen you worm would need to have a velocity of greater than 2radius of the circle, or greater than 2width of the rectangle (but in reality that is really unlikely, your worm would have to move like 25 pixels per second for that to happen, which is too high for a worm anyway). The "circle" and "rectangle" i am talking about are the shapes of the worm. This problem is known as continuous collision detection.

Not that it is necessary, but if you had a physics engine with collision detection functions, you could check polygon collisions much easier. And probably a tiny bit more efficiently than any code you will write alone, unless you are familiar with mathematics.

deleted 15 characters in body
Source Link
dimitris93
  • 1.3k
  • 2
  • 16
  • 27

I will give you a different approach you might wanna try.

How i would do this is I would have shapes for the objects, a rectangle for the terrain and then a rectangle for the bottom half of the worm and a circle for the top of the worm. So the worm is a combination of 2 shapes. Now, if the user presses left worm gets a (-5,0) velocity if the user pressed up the worm gets a (0,-5) velocity. After the velocity is applied, you check for collisions using trigonometry, and if any of the 2 shapes of the worm collide with the terrain, then you move it outside of the terrain, by moving the worm up. So you basically force it not to be inside the terrain.

For example, if the worm was inaccording to the image you have in your question and, if the user clickedpressed left, the worm would get a (-5,0) velocity applied to it, so it would move 5 pixels to the left. But then it would be inside the terrain, so we would have to force it outside of the terrain by moving it k pixels up. You will have to calculate k using Euclid theorem. So not only the worm will never be inside the terrain. But it will also move as a result of forcing it outside of the terrain.

So, basically the worm goes inside the terrain, and then the terrain pushes it upwards (only). That way your worm is never drawn inside the terrain, and also it moves.

You will simply need collision detection of a rectangle with an angle with a rectangle/circle. Or instead of a "rectangle" you could check collisions with 4 lines. That way you would not only solve rectangle with an angle - rectangle/circle collision detection, but any given polygon - circle/rectangle intersection, which you might need in the future. Because a polygon is nothing other than N lines. Now, the problem with that is that if your worm has a big velocity, your worm might pass right through the terrain, because lines are thin. For that to happen you worm would need to have a velocity of greater than 2*radius of the circle, or greater than the rectangle's smallest side (but in reality that is really unlikely, youyour worm would have to move like 25 pixels per second for that to happen, which is too high for a worm anyway). The "circle" and "rectangle" i am talking about are the shapes of the worm. That is aThis problem is known as continuous collision detection.

Not that it is necessary, but if you had a physics engine with collision detection functions, you could check polygon collisions much easier. And probably a tiny bit more efficiently than any code you will write, unless you are familiar with mathematics.

I will give you a different approach you might wanna try.

How i would do this is I would have shapes for the objects, a rectangle for the terrain and then a rectangle for the bottom half of the worm and a circle for the top of the worm. So the worm is a combination of 2 shapes. Now, if the user presses left worm gets a (-5,0) velocity if the user pressed up the worm gets a (0,-5) velocity. After the velocity is applied, you check for collisions using trigonometry, and if any of the 2 shapes of the worm collide with the terrain, then you move it outside of the terrain, by moving the worm up. So you basically force it not to be inside the terrain.

For example, if the worm was in the image you have in your question and the user clicked left, the worm would get a (-5,0) velocity applied to it, so it would move 5 pixels to the left. But then it would be inside the terrain, so we would have to force it outside of the terrain by moving it k pixels up. You will have to calculate k using Euclid theorem. So not only the worm will never be inside the terrain. But it will also move as a result of forcing it outside of the terrain.

So, basically the worm goes inside the terrain, and then the terrain pushes it upwards (only). That way your worm is never drawn inside the terrain, and also it moves.

You will simply need collision detection of a rectangle with an angle with a rectangle/circle. Or instead of a "rectangle" you could check collisions with 4 lines. That way you would not only solve rectangle with an angle - rectangle/circle collision detection, but any given polygon - circle/rectangle intersection, which you might need in the future. Because a polygon is nothing other than N lines. Now the problem with that is that if your worm has a big velocity, your worm might pass right through the terrain, because lines are thin. For that to happen you worm would need to have a velocity of greater than 2*radius of the circle, or greater than the rectangle's smallest side (but in reality that is really unlikely, you worm would have to move like 25 pixels per second for that to happen, which is too high for a worm anyway). The "circle" and "rectangle" i am talking about are the shapes of the worm. That is a problem known as continuous collision detection.

Not that it is necessary, but if you had a physics engine with collision detection functions, you could check polygon collisions much easier. And probably a tiny bit more efficiently than any code you will write, unless you are familiar with mathematics.

I will give you a different approach you might wanna try.

How i would do this is I would have shapes for the objects, a rectangle for the terrain and then a rectangle for the bottom half of the worm and a circle for the top of the worm. So the worm is a combination of 2 shapes. Now, if the user presses left worm gets a (-5,0) velocity if the user pressed up the worm gets a (0,-5) velocity. After the velocity is applied, you check for collisions using trigonometry, and if any of the 2 shapes of the worm collide with the terrain, then you move it outside of the terrain, by moving the worm up. So you basically force it not to be inside the terrain.

For example, according to the image in your question, if the user pressed left, the worm would get a (-5,0) velocity applied to it, so it would move 5 pixels to the left. But then it would be inside the terrain, so we would have to force it outside of the terrain by moving it k pixels up. You will have to calculate k using Euclid theorem. So not only the worm will never be inside the terrain. But it will also move as a result of forcing it outside of the terrain.

So, basically the worm goes inside the terrain, and then the terrain pushes it upwards (only). That way your worm is never drawn inside the terrain, and also it moves.

You will simply need collision detection of a rectangle with an angle with a rectangle/circle. Or instead of a "rectangle" you could check collisions with 4 lines. That way you would not only solve rectangle with an angle - rectangle/circle collision detection, but any given polygon - circle/rectangle intersection, which you might need in the future. Because a polygon is nothing other than N lines. Now, the problem with that is that if your worm has a big velocity, your worm might pass right through the terrain, because lines are thin. For that to happen you worm would need to have a velocity of greater than 2*radius of the circle, or greater than the rectangle's smallest side (but in reality that is really unlikely, your worm would have to move like 25 pixels per second for that to happen, which is too high for a worm anyway). The "circle" and "rectangle" i am talking about are the shapes of the worm. This problem is known as continuous collision detection.

Not that it is necessary, but if you had a physics engine with collision detection functions, you could check polygon collisions much easier. And probably a tiny bit more efficiently than any code you will write, unless you are familiar with mathematics.

added 23 characters in body
Source Link
dimitris93
  • 1.3k
  • 2
  • 16
  • 27
Loading
added 152 characters in body
Source Link
dimitris93
  • 1.3k
  • 2
  • 16
  • 27
Loading
deleted 19 characters in body
Source Link
dimitris93
  • 1.3k
  • 2
  • 16
  • 27
Loading
deleted 23 characters in body
Source Link
dimitris93
  • 1.3k
  • 2
  • 16
  • 27
Loading
added 327 characters in body
Source Link
dimitris93
  • 1.3k
  • 2
  • 16
  • 27
Loading
Source Link
dimitris93
  • 1.3k
  • 2
  • 16
  • 27
Loading