Vectors are useful in collision detection and game development in general. A 3d cube can also be represented by a vector in a similar manner. For example, a 2d rectangle can be represented by a vector from the lower left corner to the upper right corner of the box. The use of vectors for collision detection can even be used on other things that aren’t two lines. Note that if we project using the axis perpendicular to the red line it would seem that they are intersecting, that’s why it’s important to use the two normal axis for checking. The image below shows a projection onto the axis perpendicular to the blue line.
This check works for any case and is sufficient on its own (no need to check other axis). The correct axis to use is the two normal axis perpendicular to any of the two vectors. It seems testing for x and y axis isn’t sufficient. Case B passes both x and y axis tests even though they do not intersect. If we projected case A along the y-axis we would detect that indeed they aren’t intersecting. This method does not handle all cases of collisions though, for example in case A below if we use the x-axis as a projection it would say that they intersect even though they aren’t. If the projections do not intersect, then we are sure that the original lines do not intersect. Once the vectors are projected into an axis, it’s easier to compare whether the projection lines intersect or not (the details on how exactly would depend on what you are using). In this case since we will be projecting each vector to the x-axis, we will get the dot product of each vector with the (1,0) unit vector. This can be easily done by getting the dot product of the vector and a unit vector in the same direction as the axis you want to project on. We can represent each line as a vector with one end as the the tail and the other head as a tail.įrom here we can get the “projection” of each vector on a certain axis, like the x-axis. One way to do line-to-line collision is through the use of vectors. Line From ( v: Vector 2D) Line From ( x1: Double y1: Double x2: Double y2: Double) Line From. One of these things is checking whether two lines intersect or not.Īt one glance it is easy for a human to check if two lines intersect or not, but how would you make a computer do it? This problem seems simple and doesn’t seem to be useful for anything, however it has its use in some areas like collision detection in video game development. While computers are good at calculating lots and lots of numbers that would be impossible for a human to do, amusingly some trivial things for humans are much more complex when it is done by a computer. Step 3 : Before finding the intersection point.
Step 2 : Compute both the equations in form of ax + by + c d. Give one sample application and describe/illustrate how the vector can be used. Step 1 : Input four coordinates of two lines.As we’ve seen in class, vectors can be used to represent several objects such as physical quantities like velocity or force 2-dimensional images and even the bag-of-words model.Topics are-u-telling-me-im-aliya Intro Leonardo Da Vinci Sports Symmetry Synthesis synthesus Transform Vector Vitruvian ManĢd line-to-line collision detection using vectors (Week 2: Vectors) consistent vector/matrix/line representation is data-oriented is purely functional collisions only - no gravity, rigid body handling, or complex solvers.Return vector - 2 * Vector3.Dot(vector, normal) * normal Īnd for 2D you could just do: public static Vector2 Reflect(Vector2 vector, Vector2 normal) Here's the formula XNA uses: public static Vector3 Reflect(Vector3 vector, Vector3 normal) It's really just a small formula that takes the wall's normal and the incidence vector, and returns the reflected vector for you. In case you ever need to handle this for arbitrary walls, you'll need to look into how to reflect a vector. Here's a picture that should make it clear: This angle is measured in relation to the wall's normal. Example: if( /* hit vertical wall */ )īut if you really care about knowing the angle, then in general terms, the angle of reflection is the same as the angle of incidence. Simply negate the X-component of the object's velocity when hitting a vertical wall, or the Y-component of the object's velocity when hitting an horizontal wall. But if it's a 2D game and your walls are guaranteed to be horizontal or vertical and you just want to bounce the object off them, there's a much easier way than having to deal with reflections. You didn't specify if it was a 2D or 3D game.