C# maths, collision detection, xna - c#

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

Related

How to calculate the 3D vector perpendicular to a polygon surface in a specific direction

I'm creating an asteroid mining game in Unity 3D, where "down" is the center of the planet you are on (So I am mostly using local rotations when dealing with vectors). I have a working physics-based character controller, but it has problems dealing with slopes. This is because when I add a force to the character, currently it pushes along the player's forward vector (see picture). What I want to do is calculate the vector that is parallel to this terrain surface, but in the direction that the player is facing (so that the player can move up the slope).
I originally thought that i could just find the vector perpendicular to the normal, but then how do I know which direction it will be in. Also complicating matters is the fact that the player could be oriented in any way in relation to the global x, y, and z.
Either way, I have the surface normals of the terrain, I have all of the player's directional vectors, but I just can't figure out how to put them all together. I can upload code or screenshots of the editor if necessary. Thanks.
The usual way is to factor out the component of the forward vector that is parallel to the surface normal:
fp = f - dot(f, n) * n
See vector rejection.
This formulation will also make fp shorter the steeper the slope is. If you don't want that, you can re-scale fp afterwards to have the same length as f.

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.

Get 2D direction that represents tilt direction of 3D object

I have a 3D object in space. Here it is from a top-down perspective:
Y is towards us, so we can't see that axis. It's not tilting at all, so it's direction would be Vector2(0,0).
Now the object is tilting forward. It's tilting towards the positive X axis. The direction would now be Vector2(0,1)
Now it's tilting to the left, it's direction would be Vector(-1,0)
Now, finally, it's tilting forward and the left, it's direction would be around Vector(-0.7071, 0.7071)
I'm using Unity, so I have access to the object's Quaternion and Euler Angles as a Vector3. How would I calculate the object's tilt direction from the object's rotation?
As far as I understand your idea, you are looking for orthogonally casted parameters of normal vector (abstractly attached to the body). If it turns forward this vector after cast gets values (0,1) like you've said.
You can receive x and y components of that vector by decomposition - as a sum of two perpendicular vectors, each one represents one component (x,0) and (0,y).
Another way of achieving that result would be to have a point in 3D space P(0,0,1) which represents top of such vector. Now simply applying rotation matrix this point would be moved toward new position which is P(x,y,z) and here you can get x and y you are looking for.
I hope I didn't messed up something.

XNA Sprite Rotation Point

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).

Categories