Now for something rather simple a few days ago I explained a bit of moving in an array. But now lets say we want to move in a 2d plane and have any angle of direction. Well lets say the top left corner is position 0,0 and that looking directly right is rotation 0. So now that we have all our references lets make a formula that will let us move a unit in any direction. According to our references 0 is looking right, therefore if we are looking directly right we are going to move only in the x field therefore we need to use cosine in order to move since cosine of 0 is 1, also sine of 0 is 0. Now if we consider that the rotation is made clockwise, and that 90 is looking downward, then using cosine for x and cosine for y works well. Therefore we need to store a position variable and a rotation variable and each time we move forward the new position will be position + (cosine(angle),sine(angle))*unit and moving backwards would be position - (cosine(angle),sine(angle))*unit. Very easy. Now for collisions there are a bunch of different ways. the easiest is the collision between two dots. Basically a collision happens only if the two dots are in the same position. We could also have a collision between a dot and a circle. First you get the distance between the dot and the circle, then get the radius of the circle. If the distance is smaller than the radius then there is a collision. We can have collision between two circles, this time we also need the distance between the center of the two circles, then if the distance is smaller than the sum of both radius we have a collision.
There are many different collision tests each of them get more complex depending on geometry of the objects, but thats all for today.
back to the code
Sunday, March 30, 2008
easy 2d movement.
Subscribe to:
Post Comments (Atom)


























No comments:
Post a Comment