Parbolic Equation using 3 points - java

Alright so I need to make a parabola that stretches across the length of my World. (W)
I am creating this in a world where the top left corner is (0,0)
my 3 points are from left to right, (x,y)
(0,H)
(W/2,0) << vertex
(W,H)
This would be from the bottom left corner of the world, to the vertex in the top center of the world, to the bottom right corner of the world.
I am sure I made this so much more complicated then it needed to be, but I fried my brain attempting to figure it out.
Also the way this would work is I would want a graphic to travel the parabola over a given amount of time.
so I would make a function to get the Y, and I would send it the X which would range from 0 to W, depending on the time elapsed.
so i would call the function,
GetPathY((WorldWidth*Percentage));
private int getPathY(double X) {
int y = (int) ScreenHeight-((4 * ScreenHeight* X)/(WorldWidth^2))
return(y);
}
would this work i think?
So: y=(((-4*ScreenHeight)/(WorldWidth^2))(x-(WorldWidth/2)^2)) or: y = H-((4Hx)/(W^2));

What's the equation for a parabola?
y(x) = c0 + c1*x + c2*x^2
You have three points:
y(0) = c0 = H
And another:
y(W/2) = H + c1*(W/2) + c2*(W/2)^2 = 0
You can solve this for either c1 or c2. Let's do it for c2:
c2 = -4H/W^2 - 2c1/W
Then there's the last equation:
y(W) = H + c1*(W) + c2*(W^2) = H
Subtract H from both sides gives:
c1*W + c2*W^2 = = 0
Simplify this one to get c1:
c1 = -c2*W
Substitute the coefficient that you solved for in the second equation into this one to get the third and you're done.
It's just algebra.

I actually made a second way to do it.
1st way I got by rearranging y=a(x-h)^2+k, to solve for 'a'
y=(WindowHeight/((WindowWidth/2)^2)*(x-(WindowWidth/2))^2
2nd way, just make two percentiles, one for up, one for horizontal. As time goes scale the X-axis percentile from 0-100% and Y-axis from 0-100% halfway into the time and then back down from 100-0% all you had to do is for the Y-axis add in a modifier so that it starts fast and slows down as it reached 100%. This will provide the curve.
Of course, the equation is easier :)
I would appreciate if you did not delete this post, because My answer was fundamentally different then the other guys, and both my solutions are valid and also different. I used nothing of his answer to obtain my own, and both these solutions may help someone else who is attempting to solve something similar in the future. Also in the reason that the first answer did not help to solve the problem in the way which it was described.

Related

Saving branching possibilities easily (Java)

Since my last question got locked for beeing inprecise, one more try:
Lets say I start at
branch[0]
branch[0] only has an int value of 30.
Now I have two choices. Left or right.
Both have a random chance to multiply/be divided by up to 5.
So left could be 6,8,10,15,30,30,60,90,120,150. Same for right.
This choice happens for up to 30 times for every branch. so 2^30 times.
(disregard the chance of having an insanely large number, it is just an example that is easiest to understand)
Now I want to be able to know which ENDRESULT has the highest value.
AND once I know that, I also need to know the exact path it followed (l,r,l,l,l,r,l...) so I can tell myself at branch[0] which way to go.
HOW can I do this efficiently?
Because every method I thought of is either super complex or would take years to calculate and be super messy. And if possible, this way needs to work with more than just two choices.
edit
Example:
I start at a point. That point has gold = 0 as a value.
I have two options. Left and right.
Left has gold = 15; Right has gold = 25.
If I go left from the left again (LeftLeft) gold = 25;
If I go right from the left, (LeftRight) gold = 35;
If I go left from the right, (RightLeft) gold = 15;
If I go right from the right again (RightRight) gold = 45;
I need to compare the values of LL, LR, RL, RR to each other and once I know that RR has the highest gold, I need to know how I get there from my original starting position.

How to solve equation system by using Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Here is the problem, I have this equation system (as an example) that I need to solve and find x and y values:
(x-0)^2+(y-5)^2=12,25
(x-0)^2+(y-0)^2=2,25
Don't worry about 0'os, this changes according to detected point and this is just an example.
As I understand, I am not able to use Craner's rule here, so I am lost and I have no idea how to program this. It is simple to do it by hand, but how to write algorithm for this?
Any tips?
Edit: here is a picture of how equations look like and how I solve them by hand: http://i.imgur.com/Gm29Cfw.jpg (step by step solution: http://i.imgur.com/ZvraQoZ.jpg)
The process of solving it by hand is pretty simple: I have quadratic equation system. Then, I take the second equation and find what x is equal to in that equation. So, by doing this now I know what x is equal to. After this step I take that x value and put it in the first value. By doing so I make sure that first equation has only one missing variable. I solve it normally and get what y is equal to. Then, I put y value to the x value and I get my answer.
Okay, here is an idea, but I have yet to implement it and check if this actually works.
Mathematically, the two equations describe circles.
Let (a,b) be the center of the first circle and sqrt(r_1) its radius, and let (c,d) be the center of the second circle and sqrt(r_2) its radius. Then, in cartesian coordinates, the points on the circle fullfull respectivelly
(x - a)^2 + (y-b)^2 = r_1
or
(x - c)^2 + (y-d)^2 = r_2
We describe the circle now with two functions: The upper part and the lower part. These are functions involving square roots. So if we have
(x - a)^2 + (y-b)^2 = r_1
Then solving for y gives (via wolfram alpha)
(y-b)^2 = r_1 - (x-a)²
y = b + sqrt(-a²+2ax+r_1-x²)
or
y = b - sqrt(-a²+2ax+r_1-x²)
We can also express the lower and upper part of the other circle with these two equations by exchanging (a,b) with (c,d) and r_1 with r_2.
The point is, once we have two graphs with y_1 = f(x) and y_2 = g(x), then we can find their intersection with f(x) = g(x) or equivalently f(x) - g(x) = 0. For this, we can use approximate solutions found by the Newton's iteration method! We can also compute the needed derivatives:
So, the whole idea is that we split each circle in two functions: Upper andl lower part. Then, we check if the upper part of the first circle intersects the function describing the upper part of the second circle or the lower part of the second circle. Same with the lower part, we check it agains the upper and lower part of the other function. And for finding the intersection, we can use the approximate Newton method.
So, for the above example:
(x-0)^2+(y-5)^2=12,25
We get the upper and lower functions as
y = 5 + sqrt(12.25-x^2)
y = 5 - sqrt(12.25 -x^2)
And we can plot them nicely
Conversely, the second circle ((x-0)^2+(y-0)^2=2,25) is described by the equations
y = 0 + sqrt(2.25-x^2)
y = 0 - sqrt(2.25-x^2)
Now, if we look at all these graphs at once:
We find that there is an intersection! :). Between the lower part of the first circle and the upper part of the second circle. If we now form the difference between these two functions, we get the following graph for the functions:
f(x) = 5 - sqrt(12.25 -x^2)
g(x) = 0 + sqrt(2.25-x^2)
f(x)-g(x) = 5 - sqrt(12.25 -x^2) - sqrt(2.25-x^2)
And if we plot that
We can see that if we find the zeroes of that graph, we will get the correct solution x = 0! :)
Once we have that solution, we can eliminate one variable in either of the equations
(x-a)^2 + (y-b)^2 = r_1
And we will receive an equation ONLY quadratic in y, which can be solved by the general solution formula (pq-formula or abc-formula).
Hope this gives you some ideas.

Collision between a line and a face

Ok, so on the internet, I have seen equations for solving this, but they require the normal of the plane, and are a lot higher math than I know.
Basically, if I have an x,y,z position (as well as x,y,z rotations) for my ray, and x,y,z for three points that represent my plane, How would I solve for the point of collision?
I have done 2D collisions before, but I am clueless on how this would work in 3D. Also, I work in java, though I understand C# well enough.
Thanks to the answer below, I was able to find the normal of my face. This then allowed me to, through trial and error and http://geomalgorithms.com/a05-_intersect-1.html, come up with the following code (hand made vector math excluded):
Vertice Vertice1 = faces.get(f).getV1();
Vertice Vertice2 = faces.get(f).getV2();
Vertice Vertice3 = faces.get(f).getV3();
Vector v1 = vt.subtractVertices(Vertice2, Vertice1);
Vector v2 = vt.subtractVertices(Vertice3, Vertice1);
Vector normal = vt.dotProduct(v1, v2);
//formula = -(ax + by + cz + d)/n * u where a,b,c = normal(x,y,z) and where u = the vector of the ray from camX,camY,camZ,
// with a rotation of localRotX,localRotY,localRotZ
double Collision =
-(normal.x*camX + normal.y*camY + normal.z*camZ) / vt.dotProduct(normal, vt.subtractVertices(camX,camY,camZ,
camX + Math.sin(localRotY)*Math.cos(localRotX),camY + Math.cos(localRotY)*Math.cos(localRotX),camZ + Math.sin(localRotX)));
This code, mathimatically should work, but I have yet to properly test the code. Tough I will continue working on this, I consider this topic finished. Thank you.
It would be very helpful to post one of the equations that you think would work for your situation. Without more information, I can only suggest using basic linear algebra to get the normal vector for the plane from the data you have.
In R3 (a.k.a. 3d math), the cross product of two vectors will yield a vector that is perpendicular to the two vectors. A plane normal vector is a vector that is perpendicular to the plane.
You can get two vectors that lie in your plane from the three points you mentioned. Let's call them A, B, and C.
v1 = B - A
v2 = C - A
normal = v1 x v2
Stackoverflow doesn't have Mathjax formatting so that's a little ugly, but you should get the idea: construct two vectors from your three points in the plane, take the cross product of your two vectors, and then you have a normal vector. You should then be closer to adapting the equation to your needs.

Fix collision detection penetration

I am implementing collision detection in my game, and am having a bit of trouble understanding how to calculate the vector to fix my shape overlap upon collision.
Say for example, I have two squares. squareA and squareB. For both of them, I know their xCo, yCo, width and height. squareA is moving however, so he has a velocity magnitude, and a velocity angle. Let's pretend I update the game once a second. I have illustrated the situation below.
Now, I need a formula to get the vector to fix the overlap. If I apply this vector onto the red square (squareA), they should not be overlapping anymore. This is what I am looking to achieve.
Can anyone help me figure out the formula to calculate the vector?
Bonus points if constructed in Java.
Bonus bonus points if you type out the answer instead of linking to a collision detection tutorial.
Thanks guys!
Also, how do I calculate the new velocity magnitude and angle? I would like sqaureA to continue moving along the x axis (sliding along the top of the blue square)
I had an function that looked something like this:
Position calculateValidPosition(Position start, Position end)
Position middlePoint = (start + end) /2
if (middlePoint == start || middlePoint == end)
return start
if( isColliding(middlePont) )
return calculateValidPosition(start, middlePoint)
else
return calculate(middlePoint, end)
I just made this code on the fly, so there would be a lot of room for improvements... starting by not making it recursive.
This function would be called when a collision is detected, passing as a parameter the last valid position of the object, and the current invalid position.
On each iteration, the first parameter is always valid (no collition), and the second one is invalid (there is collition).
But I think this can give you an idea of a possible solution, so you can adapt it to your needs.
Your question as stated requires an answer that is your entire application. But a simple answer is easy enough to provide.
You need to partition your space with quad-trees
You seem to indicate that only one object will be displaced when a collision is detected. (For the case of multiple interpenetrating objects, simply correct each pair of objects in the set until none are overlapping.)
Neither an elastic nor an inelastic collision is the desired behavior. You want a simple projection of the horizontal velocity (of square A), so Vx(t-1) = Vx(t+1), Vy(t-1) is irrelevant and Vy(t+1)=0. Here t is the time of collision.
The repositioning of Square A is simple.
Define Cn as the vector from the centroid of A to the vertex n (where the labeling of the vertices is arbitrary).
Define A(t-1) as the former direction of A.
Define Dn as the dot product of A(t-1) and the vector Cn
Define Rn as the width of A measured along Cn (and extending past the centroid in the opposite direction).
Define Sn as the dilation of B by a radius of Rn.
Let j be the vertex of B with highest y-value.
Let k be the vertex that is most nearly the front corner of A [in the intuitive sense, where the value of Dn indicates that Ck is most nearly parallel to A(t-1)].
Let K be the antipodal edge or vertex of A, relative to k.
Finally, translate A so that k and j are coincident and K is coincident with Sk.

Nearest point on a quadratic bezier curve

I am having some issues calculating the nearest point on a quadratic curve to the mouse position. I have tried a handful of APIs, but have not had any luck finding a function for this that works. I have found an implementation that works for 5th degree cubic bezier curves, but I do not have the math skills to convert it to a quadratic curve. I have found some methods that will help me solve the problem if I have a t value, but I have no idea how to begin finding t. If someone could point me to an algorithm for finding t, or some example code for finding the nearest point on a quadratic curve to an arbitrary point, I'd be very grateful.
Thanks
I can get you started on the math.
I'm not sure how quadratic Bezier is defined, but it must be equivalent
to:
(x(t), y(t)) = (a_x + b_x t + c_x t^2, a_y + b_y t + c_y t^2),
where 0 < t < 1. The a, b, c's are the 6 constants that define the curve.
You want the distance to (X, Y):
sqrt( (X - x(t))^2 + (Y - y(t))^2 )
Since you want to find t that minimizes the above quantity, you take its
first derivative relative to t and set that equal to 0.
This gives you (dropping the sqrt and a factor of 2):
0 = (a_x - X + b_x t + c_x t^2) (b_x + 2 c-x t) + (a_y - Y + b_y t + c_y t^2) ( b_y + 2 c_y t)
which is a cubic equation in t. The analytical solution is known and you can find it on the web; you will probably need to do a bit of algebra to get the coefficients of the powers of t together (i.e. 0 = a + b t + c t^2 + d t^3). You could also solve this equation numerically instead, using for example Newton-Raphson.
Note however that if none of the 3 solutions might be in your range 0 < t < 1. In that case just compute the values of the distance to (X, Y) (the first equation) at t = 0 and t = 1 and take the smallest distance of the two.
EDIT:
actually, when you solve the first derivative = 0, the solutions you get can be maximum distances as well as minimum. So you should compute the distance (first equation) for the solutions you get (at most 3 values of t), as well as the distances at t=0 and t=1 and pick the actual minimum of all those values.
There's an ActionScript implementation that could easily be adapted to Java available online here.
It's derived from an algorithm in the book "Graphics Gems" which you can peruse on Google Books here.
The dumb, naive way would be to iterate through every point on the curve and calculate the distance between that point and the mouse position, with the smallest one being the winner. You might have to go with something better than that though depending on your application.

Categories