Get a parabola/arc based on vertex and a point - c#

I'm trying to get a character to throw something in an arc at a target.
I know the vertex(x,y) and the target(x,y) and I want to get an arc from the origin(x,y) to the target with a max height of vertex.y
What I have is based off the vertex form of y = a(x-h)^2 + k
public static Vector3 parabola(Vector2 origin, Vector2 target, float height)
{
float dist = target.x - origin.x;
Vector2 vertex = new Vector2(origin.x + (dist / 2), origin.y + height);
//a = (y-k) / (x-h)^2
float a = (target.y - vertex.y) / ((target.x - vertex.x) * (target.x - vertex.x));
//b = (-h + -h) * a
float b = (-vertex.x + -vertex.x) * a;
//c = (h * h) * a + k
float c = (vertex.x * vertex.x) * a + vertex.y;
return new Vector3(a, b, c);
}
x += Time.DeltaTime;
float yPos = a * ((x - h) * (x - h)) + k;
This doesn't produce the correct arc. It's usually much too steep or much too shallow. Is my algebra wrong, or am I using the wrong approach?
Thanks

Here is a good solution: Wiki:Trajectory of a projectile.

Related

Shoot moving objects in 2D

I have a 2d space with multiple objects(Lets call them B). Lets say object A our automated actor, he moves in a specific path and he has to shoot only the objects it can destroy. The other objects might or might not move.
I need to find the direction that I should fire the bullet that will collide with the object B. The bullet is moving with a different speed that object A and it has a specific lifetime.
I've tried to solve it with Quadratic but I always get infinity, is this a wrong approach?
Vector3 vectorFromVictim = bullet.Position - victim.Position;
float distanceToVictim = vectorFromVictim.Length();
double victimSpeed = victim.Position.Length();
double a = bulletSpeed * bulletSpeed - victimSpeed * victimSpeed;
double b = 2 * vectorFromVictim.Dot(victim.LinearVelocity);
double c = -distanceToVictim * distanceToVictim;
float t = (QuadraticSolver(a, b, c));
if (float.IsInfinity(t))
{
return;
}
interceptionPosition = victim.Position + victim.LinearVelocity * t;
if (t <= bulletLifetime)
{
ShootAtDirection(interceptionPosition);
}
Edit: My QuadraticSolver is this
double d = Math.Pow(b, 2) - (4 * a * c);
if (d < 0)
{
return float.PositiveInfinity;
}
float t;
if (d == 0)
{
t = (float) (-b / (2 * a));
if (float.IsNaN(t))
{
return float.PositiveInfinity;
}
return t;
}
t = (float) ((-b - Math.Sqrt(d)) / (2 * a));
float t2 = (float) ((-b + Math.Sqrt(d)) / (2 * a));
if (t < t2)
{
return t < 0 ? float.PositiveInfinity : t;
}
return t2 < 0 ? float.PositiveInfinity : t2;
B (target) coordinates are
bx + ux * t, by + uy * t
where ux, uy are components of B velocity vector
Bullet coordinates are
ax + v * cos(f) * t, ay + v * sin(f) * t
where v is bullet speed, f is directional angle (unknown yet)
ax + v * cos(f) * t = bx + ux * t
ay + v * sin(f) * t = y + uy * t
t * (v * cos(f) - ux) = bx - ax = dx
t * (v * sin(f) - uy) = bx - ax = dy
dx, dy is position difference, negated your vectorFromVictim
exclude t
dy * (v * cos(f) - ux) = dx * (v * sin(f) - uy)
dy * v * cos(f) - dy * ux = dx * v * sin(f) - dx * uy
v * (dy*cos(f) - dx*sin(f)) = dy * ux - dx * uy
let
g = atan2(dy, dx)
L = vectorFromVictim.Length
so
v * sin(g - f) = L * (dy * ux - dx * uy)
sin(g - f) = L/v * (dy * ux - dx * uy)
g - f = arcsin(L/v * (dy * ux - dx * uy) )
and finally
f = g - arcsin(L/v * (dy * ux - dx * uy) )
Quiclk Python test
import math
def aiming(ax, ay, bx, by, ux, uy, v):
dx = bx - ax
dy = by - ay
g = math.atan2(dy, dx)
L = math.hypot(dy, dx)
if (v * math.cos(ang) - ux):
t = dx / (v * math.cos(ang) - ux)
elif (v * math.sin(ang) - uy):
t = dy / (v * math.sin(ang) - uy)
else:
return None
coll_x = bx + ux * t
coll_y = by + uy * t
return ang, coll_x, coll_y
print(aiming(0, 0, 0, 1, 1, 0, 1.4142))
gives correct value 0.7854 = Pi/4 radians = 45 degrees, and point (1,1)

How to create a 3D mesh from a heightmap represented as a float array

I am attempting to understand how a 3D mesh has been constructed from a heightmap stored as a one dimensional float array. The only examples I have seen previously have made use of a 2D float array, and I am struggling to wrap my head around the math involved here. Any insight into it would be appreciated. I have commented the code which I do not quite understand yet for your convenience.
Source of code: https://github.com/SebLague/Hydraulic-Erosion
public void ContructMesh () {
Vector3[] verts = new Vector3[mapSize * mapSize];
int[] triangles = new int[(mapSize - 1) * (mapSize - 1) * 6];
int t = 0;
//Note that default mapSize is 255
for (int i = 0; i < mapSize * mapSize; i++) {
//Following code is not properly understood
int x = i % mapSize;
int y = i / mapSize;
int meshMapIndex = y * mapSize + x;
Vector2 percent = new Vector2 (x / (mapSize - 1f), y / (mapSize - 1f));
Vector3 pos = new Vector3 (percent.x * 2 - 1, 0, percent.y * 2 - 1) * scale;
pos += Vector3.up * map[meshMapIndex] * elevationScale; //Elevation scale is 20 by default
verts[meshMapIndex] = pos;
//End of misunderstood code
if (x != mapSize - 1 && y != mapSize - 1) {
t = (y * (mapSize - 1) + x) * 3 * 2;
triangles[t + 0] = meshMapIndex + mapSize;
triangles[t + 1] = meshMapIndex + mapSize + 1;
triangles[t + 2] = meshMapIndex;
triangles[t + 3] = meshMapIndex + mapSize + 1;
triangles[t + 4] = meshMapIndex + 1;
triangles[t + 5] = meshMapIndex;
t += 6;
}
}
Mesh mesh = new Mesh();
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
mesh.vertices = verts;
mesh.triangles = triangles;
mesh.RecalculateNormals ();
What specifically do you not understand?
int x = i % mapSize; // Get the x location of the current point
int y = i / mapSize; // Get the y location of the current point
// This should be equal to i, IDK why this is even calculated
int meshMapIndex = y * mapSize + x;
// How far along either direction are we?
Vector2 percent = new Vector2 (x / (mapSize - 1f), y / (mapSize - 1f));
// Make a new vector that scales the X and Y coordinates up.
// The Y coordinate is set to the Z element in this vector
// Presumably because whatever you use to render uses the Y element as "up"
// And the X-Z plane is the horizontal plane
// Also normalize X and Z to lie between -1*scale and 1*scale
Vector3 pos = new Vector3 (percent.x * 2 - 1, 0, percent.y * 2 - 1) * scale;
// Add the value at the current index, times the scale, as the Y element of pos
pos += Vector3.up * map[meshMapIndex] * elevationScale; //Elevation scale is 20 by default
// The X-Z values of pos give you the location of the vertex in the horizontal plane
// The Y value of pos gives you the height
// save the newly calculated pos in verts
verts[meshMapIndex] = pos;

draw an equilateral triangle C#

I have tried to adapt some code I came across to draw an equilateral triangle in c#
public void drawTriangle(PaintEventArgs e, int x, int y, int distance)
{
float angle = 0;
SolidBrush brs = new SolidBrush(Color.Green);
PointF[] p = new PointF[3];
p[0].X = x;
p[0].Y = y;
p[1].Y = (float)( x + distance * Math.Cos(angle + Math.PI / 3));
p[1].X = (float)( y + distance * Math.Sin(angle + Math.PI / 3));
p[2].Y = (float)( x + distance * Math.Cos(angle - Math.PI / 3));
p[2].X = (float)( y + distance * Math.Sin(angle - Math.PI / 3));
e.Graphics.FillPolygon(brs, p);
}
Unfortunately, this doesn't even come close. I have drawn equilateral triangles, but the points were always based on the centers of congruent circles. I am trying to find a simpler way. I am sure there must be an obvious problem with this code, but I am trying to learn the math needed as I go, so I don't know what it is. Thanks for your time.
Try this approach. I assume that for zero angle p[0] is left bottom vertex, p[1] is right bottom (the same horizontal).
(BTW, you have got strange mangling of Y/X)
p[0].X = x;
p[0].Y = y;
p[1].X = (float)( x + distance * Math.Cos(angle));
p[1].Y = (float)( y + distance * Math.Sin(angle));
p[2].X = (float)( x + distance * Math.Cos(angle + Math.PI / 3));
p[2].Y = (float)( y + distance * Math.Sin(angle + Math.PI / 3));

C# DirectX Circle Drawing not working correctly

I've been trying to recode a C++ DirectX code to C# that would help me with Drawing a perfect circle. Currently I have this code that i translated by myself:
private void Circle(int X, int Y, int radius, int numSides, Color color)
{
Vector2[] Line = new Vector2[128];
float Step = (float)(Math.PI * 2.0 / numSides);
int Count = 0;
for (float a = 0; a < Math.PI * 2.0; a += Step)
{
float X1 = (float)(radius * Math.Cos(a) + X);
float Y1 = (float)(radius * Math.Sin(a) + Y);
float X2 = (float)(radius * Math.Cos(a + Step) + X);
float Y2 = (float)(radius * Math.Sin(a + Step) + Y);
Line[Count].X = X1;
Line[Count].Y = Y1;
Line[Count + 1].X = X2;
Line[Count + 1].Y = Y2;
Count += 2;
}
line.Begin();
line.Draw(Line, color);
line.End();
}
The problem is that the circle is drawn but also a Line from a point in the circle to the left top corner, like this.
Don't iterate with a floating point variable. They might get imprecise during the iteration. In your case, the last step is probably very close behind the upper bound (instead of hitting it exactly). So it won't get calculated and left as the default (0, 0).
So use an integer iteration variable:
for (int i = 0; i < numSides; ++i)
{
float a = i * Step;
...
}
Then, you can also get rid of Count.
Furthermore, you should make your coordinate buffer dynamic:
Vector2[] Line = new Vector2[2 * numSides];

WP7 XNA - Only drawing half of my sphere model

I'm having a very strange problem with XNA/OpenGL on Windows Phone 7. I'm drawing a sphere using the following code:
if (Radius < 0f)
Radius = -Radius;
if (Radius == 0f)
throw new DivideByZeroException("DrawSphere: Radius cannot be 0f.");
if (Precision == 0)
throw new DivideByZeroException("DrawSphere: Precision of 8 or greater is required.");
const float HalfPI = (float)(Math.PI * 0.5);
float OneThroughPrecision = 1.0f / Precision;
float TwoPIThroughPrecision = (float)(Math.PI * 2.0 * OneThroughPrecision);
float theta1, theta2, theta3;
Vector3 Normal = new Vector3(0,0,0), Position = new Vector3();
for (uint j = 0; j < Precision / 2; j++)
{
theta1 = (j * TwoPIThroughPrecision) - HalfPI;
theta2 = ((j + 1) * TwoPIThroughPrecision) - HalfPI;
GL.Begin(BeginMode.TriangleStrip);
for (uint i = 0; i <= Precision; i++)
{
theta3 = i * TwoPIThroughPrecision;
Normal.X = (float)(Math.Cos(theta2) * Math.Cos(theta3));
Normal.Y = (float)Math.Sin(theta2);
Normal.Z = (float)(Math.Cos(theta2) * Math.Sin(theta3));
Position.X = Center.X + Radius * Normal.X;
Position.Y = Center.Y + Radius * Normal.Y;
Position.Z = Center.Z + Radius * Normal.Z;
GL.Normal3(Normal);
GL.TexCoord2(i * OneThroughPrecision, 2.0f * (j + 1) * OneThroughPrecision);
GL.Vertex3(Position);
Normal.X = (float)(Math.Cos(theta1) * Math.Cos(theta3));
Normal.Y = (float)Math.Sin(theta1);
Normal.Z = (float)(Math.Cos(theta1) * Math.Sin(theta3));
Position.X = Center.X + Radius * Normal.X;
Position.Y = Center.Y + Radius * Normal.Y;
Position.Z = Center.Z + Radius * Normal.Z;
GL.Normal3(Normal);
GL.TexCoord2(i * OneThroughPrecision, 2.0f * j * OneThroughPrecision);
GL.Vertex3(Position);
}
GL.End();
}
The sphere ends up looking like this (on BOTH the emulator AND the device (HTC HD7):
Any suggestions?
Try using this. Since it's more of an OpenGL issue than Android, try looking up an algorithm for manual sphere generation. It might be worthwhile to check if vertex order is correct as well.

Categories