I'm trying to implement an animation of a ball bouncing between 4 perpendicular walls, being the speed of the ball constant. The problem is, the framework I'm using requires me to tell the origin and the destination of the ball each time it collides with a wall.
In the moment of the collision, I have access to both the ball's current and previous positions of contact with the walls. Given the coordinates x_min, x_max, y_min and y_max of the walls, and these two positions of the ball, what is the simplest way to calculate its next position?
All the algorithms I thought about followed kind of a brute force approach, with many if-else statements... I wonder if there is some elegant way of handling this.
[sorry, this is incomplete - i would have posted it as a comment, but it's too large and involves ascii art. i may delete it later.]
if you want a compact, elegant approach it's probably easier to think of the ball as continuing in a straight line, crossing over a repeated pattern of rectangles.
+------+------+--*---+
| | | * |
| | |* |
+------+------*------+
| | *| |
| | * | |
+------+---*--+------+
| | * | |
| | * | |
+------+*-----+------+
| * | |
| *| | |
+----*-+------+------+
| * | | |
| * | | |
+-*----+------+------+
(you need to reflect the rectangles, but then get the bounce "for free").
and i am pretty sure that you could then use something similar to bresenham's algorithm
to calculate the points of intersection.
[thanks for the vote, but i have to say i think this could be a world of pain to get right. tracking reflections, particularly if exactly hit a corner, is going to be tricky... sometimes it's easier to live with ugly code!]
Let's say the previous bounce was off the left wall at position (x_min, y_prev) and the current bounce is off the top wall at (x_curr, y_max). The next bounce should be off the right wall at (x_max, y_next) where:
y_next = y_max - (y_max - y_prev) * (x_max - x_curr) / (x_curr - x_min)
It's simple geometry that the triangle defined by (previous bounce, top left corner, current bounce) is similar (same shape but different size) to the triangle (current bounce, top right corner, next bounce). Like this:
+--*----+
| / \ |
|/ \ |
* \ |
| \|
| *
| |
+-------+
If y_next is less than y_min, that means the ball will hit the bottom wall before the right wall. The will happen at (x_next, y_min) where:
x_next = x_curr + (x_curr - x_min) * (y_max - y_min) / (y_max - y_prev)
Something like this:
+--*------+
| / \ |
|/ \ |
* \ |
| \ |
+-------*-+
I'm not sure whether this is similar to what you have in mind, but my idea is to basically calculate the position as if there's no wall, and then flip the applicable coordinate using the wall as the centre point.
So, first you calculate:
new
\
\
\
---------
\
\
old
Then you translate it to:
---------
/ \
/ \
/ old
new
Pseudo-code:
newX = oldX + xInc
newY = oldY + yInc
if newX < 0
newX = -newX
if newY < 0
newY = -newY
if newX > maxX
newX = maxX - (newX - maxX)
// or 2*maxX - newX
if newY > maxY
newY = maxY - (newY - maxY)
// or 2*maxY - newY
Related
I am new to PCL and I am sorry if it is a trivial question.
I was wondering if there is a "simple" way to do this:
I have a costume "point cloud" of 3dPoints (X, Y, Z). I have each 3dPoint's pixel index as demonstrated below:
(0) +----------------------------+ (101)
(102) | | (203)
| |
| |
| |
+----------------------------+ (611)
I would like to make a loop over all the points of the costume point cloud, do something to them, and fill the pcl::PointCloud<pcl::PointXYZ> pointcloud (initialized before the loop) with the same pixel index.
pcl::PointCloud<pcl::PointXYZ>::Ptr pcl_cloud(new pcl::PointCloud<pcl::PointXYZ>);
for (all 3dPoints of the costume point cloud)
{
// do somethin to them
// fill the pcl_cloud with the 3dPoint 's X, Y,and Z with the same pixel index
}
Thanks for your help
for (auto point : costume) {
pcl_cloud->push_back(pcl::PointXYZ(point.x, point.y, point.z));
}
Reference: https://pointclouds.org/documentation/singletonpcl_1_1_point_cloud.html#a0b4d7abee110e47d90635eb042488bb4
I have a set of rectangles of different size. Each rectangle has an anchor point on the 2 dimensional plane where the center of the rectangle should be placed. The rectangles should not overlap and the center of a rectangle should be as close as possible to its anchor point. So if the center of the rectangle_i is vector c_i and its anchor point is vector a_i then the target function would be min(sum_i((c_i - a_i)^2)). I am looking for an algorithm that approaches the optimal solution as much as possible in O(n).
The rectangles can not be rotated.
The inputs of the problem are:
the size of n rectangle
for each rectangle the coordinates of the associated anchor point.
The output should be the position of the rectangles i.e. coordinates of the center of each rectangle.
In the example below x signs the center of the rectangle and o the anchor points:
+-----------+
| |
+-----------+| x |
| || o |
| x |+-----------+
| o | +-------------+
+-----------+ | o |
+---------+| x |
| o || |
| x |+-------------+
| |
+---------+
I have a collection of 3D points which form an imperfect circle and are stored in the order in which they appear in the circle. I'm positioning an object in the centre of the ring by calculating the mean position of all of the points, which works fine. Now, what I want is for the object in the centre to be facing up/down relative to the rest of the points (i.e perpendicular to the ring).
I've included an image to help clarify what I mean. Does anyone know of an algorithm that would be suitable for this?
You have to compute the plane that your points form and get its normal.
If the points are perfectly coplanar, just get three of them, a, b, and c, and compute two vectors. The normal vector n is the cross product of them:
v1 = b - a;
v2 = c - a;
n = v1 x v2;
If the points are not perfectly coplanar, you can get the plane that best fits the points and then, its normal. You can get the plane by solving a linear equation system of the form Ax=0. Since the general equation of a plane is Ax + By + Cz + D = 0, you get one equation per 3D point, obtaining this system:
| x1 y1 z1 1 | | A | | 0 |
| x2 y2 z2 1 | x | B | = | 0 |
| x3 y3 z3 1 | | C | | 0 |
| ... | | D | | ... |
| xn yn zn 1 | | 0 |
The normal vector is (A, B, C).
Elaborating on a previous answer, you get a system of n equations in 4 unknowns when you solve for the best hyperplane normal vector with n points. You need to set one of the unknown coefficients (say D) to a constant like 1 and move the corresponding data column to the right hand side so that you don't get the trivial solution A=B=C=D=0. You can safely set one coefficient to 1 if it is non-zero because the solution A,B,C,D is still a solution if you scale it. So you get a system of n equations in 3 unknowns where the right hand side is a vector of all -1 instead of the zero vector, and your data matrix is simply your matrix of points and your unknown coefficients are A,B,C. In general such a system is overdetermined if you have more than 3 points so you need to solve it using linear regression. See http://en.wikipedia.org/wiki/Linear_regression to get the matrix formula for solving for the 3 coefficients using least squares best fit.
I know the transformation matrices for rotation, scaling, translation etc. I also know the matrix for shear transformation. Now, I need to have the shear matrix--
[1 Sx 0]
[0 1 0]
[0 0 1]
in the form of a combination of other aforesaid transformations. Tried searching, tried brainstorming, but unable to strike! Thanks!
The x-shear operation for a shearing angle thetareduces to rotations and scaling as
follows:
(a) Rotate by theta/2 counter-clockwise.
(b) Scale with x-scaling factor = sin(theta/2) and y-scaling factor = cos(theta/2).
(c) Rotate by 45 degree clockwise.
(d) Scale with x-scaling factor = sqrt(2)/sin(theta) , and y-scaling factor= sqrt(2).
Yup it can be done, a rotation followed by non uniform scaling and reverse rotation. You can find the details here in third question http://www.cs.cmu.edu/~djames/15-462/Fall03/assts/15-462-Fall03-wrAssign1-answer.pdf. you can try the following openGL code as well. It rotates a rectangle by 45 degree then scales in x-axis. and then rotates in -26 degree i.e. atan(0.5). 0.5 comes from finding the angle between x-axis and one side after scaling in x-direction.
glRotatef(-26.0, 0.0, 0.0, 1.0);
glScalef(2,1,1);
glRotatef(45.0, 0.0, 0.0, 1.0);
glRectf(0, 0, 25.0, 25.0);
In 3D Graphics we often use a 4x4 Matrix with 16 useful elements. The Identity 4x4 Matrix is as following:
Between those sixteen elements there are 6 different shearing coefficients:
shear XY
shear XZ
shear YX
shear YZ
shear ZX
shear ZY
In Shear Matrix they are as followings:
Because there are no Rotation coefficients at all in this Matrix, six Shear coefficients along with three Scale coefficients allow you rotate 3D objects about X, Y, and Z axis using magical trigonometry (sin and cos).
Here's an example how to rotate 3D object (CCW) about its Z axis using Shear and Scale elements:
Look at 3 different Rotation patterns using Shear and Scale elements:
Shears are an elementary matrix operation, so while you can express them as "a combination of other matrix operations", doing so is really weird. Shears take the two forms:
| 1 V | | 1 0 |
| 0 1 | , | V 1 |
Whereas a rotation matrix is much more involved; the idea of expressing a shear using rotations suggests you haven't actually written these things out yet to see what you need, so let's look at this. A rotation matrix is of the form:
| cos -sin |
| sin cos |
Which can be composed as a sequence of three particular shear matrices, R = Sx x Sy x Sx:
| cos(a) -sin(a) | | 1 0 | | 1 sin(a) | | 1 0 |
| | = | | x | | x | |
| sin(a) cos(a) | | -tan(a/2) 1 | | 0 1 | | -tan(a/2) 1 |
Now, we can do some trivial matrix manipulation to get Sy. First left-multiply:
R = Sx x Sy x Sx
Sx⁻¹ x R = Sx⁻¹ x Sy x Sx
Sx⁻¹ x R = I x Sy x Sx
Sx⁻¹ x R = Sy x Sx
And then right-multiply:
Sx⁻¹ x R x Sx⁻¹ = Sy x Sx x Sx⁻¹
Sx⁻¹ x R x Sx⁻¹ = Sy x I
Sx⁻¹ x R x Sx⁻¹ = Sy
As a trivial rewrite, one shear is now two shears and a rotation.
But the much more important question is: why do you need to express the shear matrix as something else? It's already an elementary matrix form, what bizare computing environment are you in, or what crazy thing are you trying to do, that requires you to express an elementary transform as a way more complex, way slower thing to compute? =)
This question is a little bit odd. I have to make sure of the distance between letters above and below. Now when calculating a letter height in Pixel, say font size = 14px does this mean my alloted pixel height for a letter is 14 pixels? What about the letters with tail? How are their tails alloted in Pixels?
------------
| ** |
| * * |
| ****** | = for example this is a 14px font size
| * * |
|* *|
------------
What about my lower case letters with tail
------------
| |
| ****** |
| * * | = this letter q for example
| * * |
| ****** |
--------*---
*
Is there a specific height for its tail that overflows the alloted pixel for a font?
the tail is included in the allotted pixels if I remember correctly. Check this with adding a border to a span with just a letter:
<span style="border: 1px solid #f00">q</span>
I would imagine that it depends largely on the system you are using to measure the height of the glyphs. If you're lucky, the font itself provides metrics for the height of a line of text. When height metrics are not available, generally speaking (and particularly with web technologies) the height is made consistent with that of twice the height of a lowercase "x" (the height of the lowercase "x" is known as the x-height). There's a section in the CSS specification on how the x-height of a font can be estimated that has more information.