Question: In processing please solve........Circle to Box Collision Circles are pretty straightforward, but something like a box ( and many other shapes ) can be represented
In processing please solve........Circle to Box Collision
Circles are pretty straightforward, but something like a box and many other shapes can be
represented in more than one way. One way of representing a box is with separate lines, like the
image below. The first parts of the project will use this representation, and later parts will switch to
another form.
KNOW: Or how can we PROVE it
For a box and a circle, the real question of whether or not they overlap micht be this:
Does the circle collide with any of the edgles of the box?
This is just one question that will solve this, you'll see another later.
Part : Circle to Line Collision
In order to test multiple lines, you have to test
one first.
In this part, you're going to implemant a Circle
to Line alforithm. The purpose of this section is
not only to write the algorithm, but to verify
that it works, before moving on to something:
more complex in the next part.
Terminology Note: Line vs Line Segment
Mathematically there is a difference between lines and line segments. A line extends forever,
whereas a line segment is a part of line that has two end points. For this project, line will
generally refer to a line segmentit's just easier to write line instead of line segment.
The general algorithm for a line to circle collision is this:
Get a vector in the direction of the line from one end of the line to the other
Get the magnitude of that vector youll need this later
Normalize this vector.
Get a vector from one end of the line to the circle's positionIMPORTANT: make sure you
use the same "start" point from step Also, don't normalize this vector.
Get the dot product between those two vectors.
Scale the line direction vector from step by the result of the dot product, and add that
vector to the "start" point you've used so far. The result is the location where the to circle"
vector is projected onto the "line direction vector. Draw a small circle at that location to
show where this is
All of that will give you a point that is on the lineand you should be able to see where that
point is The last thing to do is check to see if that point is in the circle. To do that:
Get the distance from the projected point to the center of the circle. You could use the
dist function, create a vector and pet its magnitude
If the distance is less than or equal to the radius plus some small marsin of error
for example then the circle is colliding with a line. Or it's colliding with a point projected
onto that line!
What does the dot product do
The dot product has several uses, but can be used to project a vector onto another.
Projecting Points "Off" a Line Segment
The projected point we calculate will fall somewhere on
the line, but it doesn't consider that we might want to
limit this to a line segment. The image on the right is a
positive line collision, but a negative line segment
collision. How can we limit this to a line segment?
PointtoLine Collision
Determining, whether or not a point is on a line segment
is pretty simple aOverview
In this project yourre going to implement a few basic collision algorithms, and also use vectors to
simulate moving objects.
Nate: Altematives to these algorithms
Many collision aleorithms have variations on them. You may have already seen or writtent some of
these yourself. The existence of alternatives doesn't make any of the other right or wrons.
Depending on what you need in your program, some algorithms may be a better fit. Or you might
start with one, and modify it to suit your needs.
User Interface
This project will have a simple interface, consisting, of:
Helpful functions for this project
Classes to write: Boxes and Moving Objects
Sorme parts of this project will use boxes, and havire a class to store the data for it makes much
easier to handle groups of them. The provided code file creates some Box objects using a
constructor like this:
Boxflloat top, float left, float width, float height;
Your class will need a constructor that takes in that data, and stores it in whatever way you wish,
such as:
Four individual floats
PVectors
PVector for the position, separate floats for widthheicht
A function to draw the shape is not necessary, but helpful in the grand acheme of things.
Moving Object for Part
A moving object is going to need a few pieces of data.
A position vectorultimately, where is this thing in D space?
A movementdirection vector. If this thing moved, what direction would it move in
Edge Cases
There are two edge cases here, and theyre essentially the same: Is the circle collidinge with either of
the end points? So you can add these two checks to the start of the aleorithms:
If the distance between one end of the line segment is less than or equal to the Part If there is a collision, the circle needs to get "bumped" back to a spot that is no loneer
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
