XNA Sprite Rotation Point - c#

I'm busy with a little topdown shooter in XNA. Now I have a little Mathematical problem:
I have a sprite, a human that's holding a 9mm. Now the sprite looks at the mouse cursor.
When I shoot, I want to show a little muzzle flash # the end of the gun barrel. However, the coordinates of the end of the barrel will change when you rotate the character.
How can I get the correct coordinates in a sprite that is the end of the barrel when for example the end of the barrel is 14px above a players head?(topdown)
So basicly I need to know how to get coordinates of a certain point in a circle that has an certain angle with the orgin.
Thanks!

x=orig.x+cos(alpha)*r;
y=orig.y+sin(alpha)*r;
Where alpha is the angle between the x axis and the line extending the barrel; r is the radius of the circle (the lenght of the barrel).
One or both of the +s might have to be replaced with '-', depending on the orientation of the coordinate system (or play around with adding multiples of 90 degrees (up to 270) to alpha until you get it right).

Related

How do I make the player move towards the cursor Unity3D

I have this 3D game I'm developing
In this game I am a roll of toilet paper
If I press space, I get yeeted towards the cursor
I started using raycasts to do this, but that didn't work out, AT ALL
So, how do I get the toilet paper to jump towards the cursor? (how do I get the direction for where the cursor is)
So from what I understood, you want your game object to move in 3D space to the direction of your cursor, which is in 2D space. You could try to get the game object's world location and use Camera.WolrdToScreenPoint(). This gives you Vector3 where x and y are screen coordinates and z is camera world z coordinate. Then you can get your cursor position with Input.mousePosition. Now you have two points that you can use to calculate direction. (You decide if you want to use camera z position or set it to zero.)
Vector3 payerScreenPos = cam.WorldToScreenPoint(player.transform.position);
Vector3 direction = (playerScreenPos - Input.mousePosition).normalized;
You need to have a reference to the current camera (cam) and to your player.

Unity 3D/C#: Rotate Camera around Object Given the Object's Height

Given the following diagram, I am trying to accomplish the following. Does anyone know how to do this?
Notes:
The black line represents a sample trajectory of how the ball will (randomly) fly.
The camera should always remain the same distance away from the ball, indicated by the orange lines.
The Camera's height should be relative to the ball's height, but where the angle of the camera changes. (e.g. if the ball is high, the camera is above it, and if the ball is lower, the camera should have a lower angle.)
Extra Credit: Lowest possible angle should be 5 degrees, and highest possible angle should be 80 degrees.

How to calculate whether the point in cube is Visibility in WPF 3D

I want to show a Perspective cube, so I should calculate whether the point in cube is Visibility.
Like the picture, the red vertex should be invisible, while the others should be visible.
Can anyone give me a formula to calculate it?
The cube may be Rotated that I have no way to do it.
I try use HitTest to do it but it have a Poor performance.
I want to know a formula to calculate whether the point and face and line is visibility.
Edit:
The point is any point on the line.
I build it with Media3D.
If any of the faces connected to the vertex has a normal facing the camera (see back face culling), the vertex is visible. This should be pretty quick to calculate.
Try taking the dot product of the "camera vector" (this is usually (0,0,1)) and all of the normal vectors of the cube faces that the vertex in question touches.
If any of the dot products return a negative value, then the angle between the camera vector and the normal vector of the respective cube face is greater that 90 degrees and is therefore "facing" the camera.
If the point of interest is a vertex, you'll be running three dot products. If the point of interest is along a line between vertices, you will only calculate two.

Tracking Rotating Sprite

I am messing about in XNA and have run into a problem. I have a 48 * 48 sprite that I can keep track of its location in the game world by the top left corner of the sprite.
I want to be able to rotate the square and still keep track of the same point. For instance if I rotate 90degrees clockwise and the orginal X position was 200 the new X position should be 200 + 48(the size of the width of the image). Its fine for 90 degrees I am able to work that out in my head but each one in between is the problem!
I know there is probably some kind of formula to work this out.
Any help would be great! Oh the square is rotating on its center.
I'm just using spriteBatch.Draw()
spriteBatch.Draw( animations[currentAnimation].Texture,
Camera.WorldToScreen(WorldRectangle),
animations[currentAnimation].FrameRectangle,
color, rotationScale , new Vector2((float)frameHeight/2, (float)frameWidth/2), effect, TileMap.characterDepth);
If you have to keep track of a moving rotating sprite you can't use the top left corner, but its centroid. You already draw your sprite using the centroid to rotate it.
The problem is that the second parameter of your Draw call is a Rectangle, you sholud use a Vector2 position, instead.
You're building your application on top of a 3D graphics library. 3D graphics libraries are very good at solving this kind of problem! Break it down into smaller operations and let the library do the work for you.
First: it's easiest to think about these kinds of questions when you're working in model space rather than world space. In other words: you don't need to worry about where the rotating point is in absolute terms, you only need to worry about where it is relative to the untransformed model (in this case, your sprite without any rotation or translation).
So where is that? Simple:
var pt = new Vector3(-frameWidth / 2f, -frameHeight / 2f, 0f);
Your point of origin is the center of your sprite, so the center of your sprite in model space is (0, 0). This means that the top left corner of your sprite is half the width of the sprite in the negative x direction, and half the height of the sprite along the negative y direction.
Now create an object that represents the desired transformation. You can do this by creating a rotation matrix using XNA's built-in methods:
var transformation = Matrix.CreateRotationZ(MathHelper.ToRadians(90f));
Now apply the transformation to your original point:
var transformedPt = Vector3.Transform(pt, transformation);
This is still in model space, remember, so to get world coordinates you'll need to transform it into world space:
var transformedWorldX = transformedPt.X + spritePosition.X;
var transformedWorldY = transformedPt.Y + spritePosition.Y;
And there you go.

C# maths, collision detection, xna

Im doing collision detection in a game.
All the surfaces are orthagonal so I only need to determine which face of an obstacle my moving object has collided with.
I have the rectangle defining the intersection of the two objects and the vector representing the moving objects speed and direction of movement.
I reckon I need to translate the intersection rectangle along my moving objects vector until the intersection becomes a line, then I'll know which face was collided with 1st.
I have no idea how to do this mathematically of programatically however
Calculate the vector from (the corner of the obstacle internal to your object) to (the corner of your object internal to the obstacle). Whichever "side" of this vector your movement vector is on gives the "side" that touched first. If they have the same angle, the corner touched first.
E.g. here your movement vector is at about 260 degrees and then calculated vector is at about 240 degrees. Anticlockwise from calculated to movement is 20 degrees, clockwise is 340 degrees. Thus the anticlockwise side (bottom) collided first.
You need able to tell which two sides are in question, and which is "clockwise" and "anticlockwise" - I hope this is trivial.
You can compare the aspect ratio of the velocity vector to the aspect ratio of the intersection rectangle.
For this particular example, if the velocity vector is steeper than the intersection rectangle (i.e. defines a taller and skinnier rectangle), then the collision was on the bottom face.
If the velocity vector is shallower, then the collision was on the left face of the moving rectangle.
If the velocity vector is the same aspect ratio as the intersection rectangle (i.e. the velocity lays on the diagonal of the intersection rectangle), then they collided on the corners.
Actually I may have figured it out...
Find the point on the intersection rectangle that isnt on the objects
rectangle
draw a line from there in the vectors direction
whatever side it intersects with is the side that collided 1st

Categories