Is there a code formula for this? I am using C# and XNA, and in my class I have an array of three vectors (representing the vertices of the triangle), as well as a separate vector coordinate.
I plan to update these positions in the loop as it escalates further towards the top of the screen.
I'd ask maths.stackexchange.com, but seeing as how this applies to programming (and I personally am better at reading code as opposed to math itself - I'm still taking Algebra in school), I think it would make more sense for me to ask it here.
Edit
Yes, I am looking for an equilateral triangle. Or any triangle, for that matter. It doesn't matter what it is. All I am looking for is a formula; is that so hard to ask for?
Teh Problem
Basically, the problem I am trying to solve is to shoot a triangle out of my player (think Space Invaders; i.e., the triangle acts as a ray from the ray gun). What I need is a formula of code which will allow the triangle to be rendered based on its center position and radius, as the triangle will move upwards on its Y coordinate. I have the draw calls, and they work, but the problem is that the triangle when put in a for loop draw iteration (where the center vector position - on the Y coordinate - is incremented by N) simply sits next to the player's position when being drawn.
I think this is what you are looking for...
the angle is the orientation of the triangle...
this build a triangle....
void BuildTriangle(Vector2 Center, float Radius, float Angle, Vector2[] tri)
{
for (int i=0; i<3; i++)
{
t[i].X = Center.X + Radius * (float) Math.Cos(Angle + i * 2 * MathHelper.PI/3);
t[i].Y = Center.Y + Radius * (float) Math.Sin(Angle + i * 2 * MathHelper.PI/3);
}
}
if you want to move it, add to the center a velocity vector and rebuild it...
Related
This question already has answers here:
In Unity Calculate Points a Given Distance Perpendicular to a Line at Angles on a Circle
(2 answers)
Closed 1 year ago.
I have been searching for solutions for some time because I just can't wrap my head around complex math formulas.
So, I placed a vector in 3D space, with a position and a direction. Then now I want to place n amount of points equally spaced around this vector with the axis being the direction of the vector.
I'm working in Unity with C#, but pseudocode or even just an explanation is fine by me.
Edit:
Here is a picture that I made to show you what I mean.
The sphere is the vector, the arrow is the direction/axis and the
torus is where I want my points to go. I know how to place points around a circle equally spaced, my code for it is here:
for (int i = 0; i < detail; i++) {
float angle = i * Mathf.PI * 2f / detail;
Vector3 vert = new Vector3(0, Mathf.Sin(angle) * size, Mathf.Cos(angle) * size);
verticies[i] = vert + position;
}
My main problem is that I don't know 'rotate' the points to the new axis.
If you want the points to be around the axis in a circle, you can use the following method:
Calculate the gap between the points in degrees by dividing 360 by the number of points.
Then calculate the position of a point on the circle using its position on the circle (ex: for the 5th point out of 10 points, it is 360/10*5) and the radius of the circle, from this you can simply figure out the position in space using some trigonometry (using Mathf or Math classes).
I hope this helped.
Im trying to rotate a texture in Monogame ! It schould rotate around another object like doing a circle (not routate the texture it self ), it schould do a circle rotate. It schould not rotate with the mouse but continosly rotating in that radius itself. Im new in MOnogame and a tried everything for two last days and nothing worked. Some said that i schould do somethink with Sin and Cos but i didnt get it ! Its for a project and im very lost ! I would be very very greatfull if someone could help me ! [So this logo schould routate around the background ][2]
Link to picture https://www.dropbox.com/s/gywi7teun8lqfp1/Unbenannt.png?dl=0
This is a pure math problem. From what i can understand, what you want is to make your texture orbit around a point, see this formula :
newX = centerX + ( cosX * (pointX-centerX) + sinX * (pointY -centerY))
newY = centerY + ( -sinX * (pointX-centerX) + cosX * (pointY -centerY))
With : centerX and centerY being the point around you want to orbit
cosX and sinX being respectively the cosinus of the angle and the sinus of the angle
pointX and pointY being the position you want to apply rotation from (the texture position in your case)
Note that the angle should be in radians, and not in degrees.
Answers to this question can be found here:
Have an object circle an object
There are also examples of how to use Math in MonoGames
If you want to use a matrix and let the api rotate for you you could try something like this
public Vector2 RotateAboutOrigin(Vector2 point, Vector2 origin, float rotation)
{
return Vector2.Transform(pointorigin,Matrix.CreateRotationZ(rotation))+origin;
}
I have an object with a Vector2 Position, and a cursor with Vector2 Position.
When I hold a certain key, I want the object to circle around the object, but I'm having trouble calculating the correct coordinates.
I've managed to make the object circle around the cursor (but it's not going in a perfect circle, but more of a spiral) with this code:
Vector2 diff= Vector2.Normalize(cursor.Location - this.Location);
float angle = (float)Math.Atan2(diff.Y, diff.X) + (float)(90 * (Math.PI / 180));
this.Position += new Vector2((float)(speed * Math.Cos(angle)), (float)(speed* Math.Sin(angle)));
I calculate the angle between cursor's and object's locations, and add 90° (in radians) to that value, which, by my logic, should make the object travel in a perfect circle. However, the distance between the cursor and the object quickly spreads.
What am I calculating wrong here?
Usually when you want something to circle around a point, you define the distance to an amount and you incrementally change the angle in your Update method. THEN in your draw method you can draw it where you should by calculating the position from the cursor.Location, the distance from the cursor and the desired distance.
In most situations like these you want your orbiter to have the same loation like your cursor, so calculating the new position in the Draw method works best, given that these calculations are cheap and super fast (you generally do not want to hog down your Draw method).
I am not able to check it right now, but what you should be doing is something in these lines:
Given that your object should rotate D distance away from your cursor with an angular velocity of AngularVelocity (per second), then when this initially happens, set a variable angle to zero. Then in your update do:
angle += (gameTime.ElapsedGameTime.TotalSeconds * AngularVelocity)
and in your Draw method do:
var displacedPosition = new Vector2(D * Math.Sin(angle), D * Math.Cos(angle));
and render your orbiter using the displacedPosition instead of the normal position if it is currently orbiting.
I have a circle with the up vector direction is Vector(0, -1) and a point, B which could be anywhere on that circle. I know its exact position inside the circle. The (0,0) point is located on the top left of the image.
How can I find the angle x between the point B and the up vector, relative to center of the circle?
I am developing a game in XNA and C#.
Update: I do not understand why this question is marked as not about programming. Anyway here's what I have done so far.
I can find the radian between two vectors
private float radianBetweenVectors(Vector2 a, Vector2 b)
{
return (float)Math.Atan2(b.Y - a.Y, b.X - a.X);
}
But I do not want to know about the location of the up vector on the sprite image if possible. The location here is the point at which it starts at 0 degree on the circle's circumference .
Update 2:
Once I have the angle, I want to obtain the rotation matrix:
Matrix rotMatrix = Matrix.CreateRotationZ(angle);
Now I have this matrix containing the rotation, I can ask XNA to transform the Up vector with this rotation:
moveDirection = Vector2.Transform(up, rotMatrix);
This I hope will take the original up direction, transform it with the rotation around the Z axis. I am still trying to figure out if this is the correct approach.
Find relative x and y coordinates of point B from center of the circle. (You can do this if you know the radius of the circle). If the circle diameter is the width of the sprite, just divide width by two to get radius. Then use arc tangent.
I'll try to help you out since I started XNA when I was relatively young and before I had any experience with vectors and you might be in the same position I was in 5 years ago.
If you don't understand the math behind it, you just treat point B as a point on a triangle that is formed from the 3 lines. I'll use (0,0) as the center of the circle, you can calculate the relative position of the center of the circle from the absolute position given radius. (If you can't do this part, you'll need to pay more attention in math class).
(0,0) to (0, B.y) will be the vertical leg.
(0, B.y) to (B.x, B.y) will be the horizontal leg
(B.x, B.y) to (0,0) will be the hypotenuse.
Then you use tangent with the rule tangent = opposite/adjacent. So you will know the distance of the opposite leg from the angle X is the distance from (0, B.y) to (B.x, B.y), the horizontal leg. The adjacent leg distance is (0,0) to (0, B.y). No need for distance formula, just subtract the coordinates. Now you have the value of tangent and want to find the value of X, so we'll use arctan. Math.Atan2(opposite/adjacent). That will give you your answer in radians.
Edit: Oh and I remember you can draw the sprite so the center of the sprite will be rendered at the center of the sprite bitmap using the origin argument of draw. That way you don't have to worry about calcuating the center of the circle.
Speaking mathematically, if you have two vectors, lets supose A(0, -1) and B(222, 90) the angle X between the two vectors can be computed as cos(X)=(A.B)/(||A||.||B||).
A x B = (a1 x b1) + (a2 x b2) = 0x222 + (-1x90) = 0 - 90 = -90.
2.I. ||A|| = squareroot[0^2+(-1)^2] = sqrt[(-1)^2] = 1
2.II. ||B|| = squareroot[222^2+90^2] = sqrt(57384) ~= 239.55 (~= is approximately)
2.III. ||A||.||B|| = 1 x 239.55 ~= 239.55
Result: cos(X) ~= (-90)/(239.55) ~= X ~= cos^-1(X) ~= 1.955 radian.
For the vectors you gave above, the angle you looking for is approximately 1.955 radian.
Note: using a calculator, you will obtain the exact value, which is very close to the aproximate value.
Can anyone help me with this please
I want to be able to rotate a 3D object around a stationary 3D object. Well there will be no movement involved as I just want to draw the objects at their locations once the game starts and then they will remain there for the remainder of the game.
Say for instance I have a object X that is stationary in 3D space. I then have 2 other objects, Y1 and Y2. Both of these objects are stationary as well and cant be moved. All 3 objects are on the same x and y axis. Lets say X is at (0,0,0) and Y1 is at (0,0,-50). I want to draw Y2 at a 45 degree angle from Y1 around the Y-axis but keep it the same distance from X.
Can anyone please suggest the best way of doing this please?
I have tried the following but that just rotates the object around its origin. So I guess I have to rotate it around the world origin? How is this done?
Matrix.CreateRotationY(Rotation)
I'm not sure what you want, but this is one method for rotate one object around another:
Vector3 Origin; // Stationary Object
float Yaw, Pitch; // Angles
float Distance;
Vector3 OrbitOffset = Vector3.UnitX * Distance;
// Other approach that consider the initial pos of the object to rotate
// Vector3 OrbitOffset = OrbitPos - Origin;
Matrix Rotation = Matrix.CreateFromYawPitchRoll(Yaw, Pitch, 0);
Vector3.Transform(ref OrbitOffset, ref Rotation, out OrbitOffset);
Vector3 OrbitPos = Origin + OrbitOffset; // Final position of the rotated object
if you dont need rotation about more than 2 angles at once, you can use basic Euler method.
see :
http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
thats a mathematical approach tough... but it works..
Just if you want a rotation around multiple axes, you will have serious problems with gimbal lock