Finding vectors at right angles to heading of an entity - vector

I'm looking for a way to find the vectors at right angles to the game entity's heading. One to the left and one to the right.
I'm using XNA if this affects the answer in any way.
Edit: this is a 2D operation. I saw on another site that the clockwise vector is simply [-y, x] and the counter-clockwise [y, -x]. This seems to work out on paper.
Thanks.

vector product (aka cross product)

The vector cross product will give you another vector that is perpendicular to the two input vectors.
The dot product can be used to tell what the angle between 2 vectors is.
However the problem description you've given only specifies one input vector, the direction of the entity. Therefore the solution is all the vectors in the plane that the direction of the entity is normal to.

I think you should look into the Vector3.Cross function, I know you're looking to do this for 2D vectors but it shouldn't matter, just set your z component of the Vector3 to 0.
You should also probably read up on Cross Products and Dot Products as they are both very relevant to graphics programming and even games programming in genrel, and will also help you beter understand how to solve many similar problems you'll encounter with your programming :)

Related

how to determine OpenGL winding based on normals?

I am trying to understand how to manually generate objects.
I have a mesh, part of which I delete and create a new geometry in its place. I have information about the normals of deleted vertices. On the basis of which I have to build new faces (in a different size and quantity) looking in the same direction.
But I don’t understand how to choose the correct winding. It sounds easy when the lessons talk about CCW winding in screen space. But what if I have a bunch of almost chaotic points in the model space? How then to determine this CCW, which axis is used for this? I suggest that the nearest old normals might help. But what is the cheapest method to determine the correct order?
It turned out to be easier than I thought. It is necessary to find the cross product of the first two vectors from the vertices of a triangle, then find the dot of the resulting vector and the normal vector, if the result is negative, then during generation it is necessary to change the order of vertices.

Visual Understanding of Vector3.Angle

I don't understand the concept of Vector3.Angle in Unity.
Can someone please explain in detail of what it does and how it works?
It would also be really awesome if you could provide some diagrams for me understand it more, visually.
It's very straight forward, Vector3.angle. takes 2 parameters, to and from. Essentially returns the angle that's generated from one position, to another. An example would be var characterDirection would be the "from" parameter, and lets say var enemyPosition would be the "to" parameter, and it will generate an acute angle. Hope this helps, also Unity has a great scripting API you should check it out.
The first thing to understand is what a vector is. A vector is a mathematical quantity that has both a magnitude and direction. For the purpose of this question, we don't care about the magnitude but you can think of a vector as an arrow pointing in some direction.
Now, you might be confused how we draw an arrow using only Vector3(x, y, z). While you might more commonly use a Vector3 to represent a point in 3D space, it is of course also used as a vector, as the name suggests. The thing is, if you try to call Vector3.Angle(transform1.position, transform2.position) you're going to get some weird results because it's expecting vectors, not positions even though they use the same object type.
Therefore, you should instead do something like
Vector3 direction = transform2.position - transform1.position;
float angle = Vector3.Angle(direction, transform1.forward);
forward is just a shorthand for a vector along the z-axis, so this would be like looking at an angle in 2D space.

Does 3d vector lay in 4 sided pyramid?

I have a question I didn't find an answer to on google or the forum and decided to ask here for help.
I am a fairly seasoned programmer and have had many successes on various platforms but I didn't use/need a lot of mathematics until now.
Now I need to know how to build a function which receives an array of 5 points (4 sided pyramid) and a single vector. The Question is whether this 3d vector lays inside of the pyramid.
The function would ultimately be written in (Mono) C# but if you have hints or code for other languages or you can help with plain mathematics that would be absolutly fine, too.
A vector never lays inside anything. I guess you meant that you have a 3D point, not a 3D vector.
In that case, a simple solution (that works for any convex polyhedron) is to check whether your point is on the correct half spaces when considering each face of your pyramid.
Specifically, take two vectors in the first face of your pyramid (e.g., two edges) and form a third vector with one point on this face (e.g., one of the vertices) and the point to be tested. Using the sign of the mixed product (i.e., take the cross product of the two edges, which results in a vector orthogonal to the pyramid face, and check with a dot product whether this normal is in the same direction as your third vector), you can determine on which side your point is.
Repeating the procedure for all faces allows you to conclude.

Linear Algebra in Games in a 2D space

I am currently teaching myself linear algebra in games and I almost feel ready to use my new-found knowledge in a simple 2D space. I plan on using a math library, with vectors/matrices etc. to represent positions and direction unlike my last game, which was simple enough not to need it.
I just want some clarification on this issue. First, is it valid to express a position in 2D space in 4x4 homogeneous coordinates, like this:
[400, 300, 0, 1]
Here, I am assuming, for simplicity that we are working in a fixed resolution (and in screen space) of 800 x 600, so this should be a point in the middle of the screen.
Is this valid?
Suppose that this position represents the position of the player, if I used a vector, I could represent the direction the player is facing:
[400, 400, 0, 0]
So this vector would represent that the player is facing the bottom of the screen (if we are working in screen space.
Is this valid?
Lastly, if I wanted to rotate the player by 90 degrees, I know I would multiply the vector by a matrix/quarternion, but this is where I get confused. I know that quarternions are more efficient, but I'm not exactly sure how I would go about rotating the direction my player is facing.
Could someone explain the math behind constructing a quarternion and multiplying it by my face vector?
I also heard that OpenGL and D3D represent vectors in a different manner, how does that work? I don't exactly understand it.
I am trying to start getting a handle on basic linear algebra in games before I step into a 3D space in several months.
You can represent your position as a 4D coordinate, however, I would recommend using only the dimensions that are needed (i.e. a 2D vector).
The direction is mostly expressed as a vector that starts at the player's position and points in the according direction. So a direction vector of (0,1) would be much easier to handle.
Given that vector you can use a rotation matrix. Quaternions are not really necessary in that case because you don't want to rotate about arbitrary axes. You just want to rotate about the z-axis. You helper library should provide methods to create such matrix and transform the vector with it (transform as a normal).
I am not sure about the difference between the OpenGL's and D3D's representation of the vectors. But I think, it is all about memory usage which should be a thing you don't want to worry about.
I can not answer all of your questions, but in terms of what is 'valid' or not it all completely depends on if it contains all of the information that you need and it makes sense to you.
Furthermore it is a little strange to have the direction that an object is facing be a non-unit vector. Basically you do not need the information of how long the vector is to figure out the direction they are facing, You simply need to be able to figure out the radians or degrees that they have rotated from 0 degrees or radians. Therefore people usually simply encode the radians or degrees directly as many linear algebra libraries will allow you to do vector math using them.

Find X/Y/Z rotation angles from one position to another

I am using a 3D engine called Electro which is programmed using Lua. It's not a very good 3D engine, but I don't have any choice in the matter.
Anyway, I'm trying to take a flat quadrilateral and transform it to be in a specific location and orientation. I know exactly where it is supposed to go (i.e. I know the exact vertices where the corners should end up), but I'm hitting a snag in getting it rotated to the right place.
Electro does not allow you to apply transformation matrices. Instead, you must transform models by using built-in scale, position (that is, translate), and rotation functions. The rotation function takes an object and 3 angles (in degrees):
E.set_entity_rotation(entity, xangle, yangle, zangle)
The documentation does not speficy this, but after looking through Electro's source, I'm reasonably certain that the rotation is applied in order of X rotation -> Y rotation -> Z rotation.
My question is this: If my starting object is a flat quadrilateral lying on the X-Z plane centered at the origin, and the destination position is in a different location and orientation where the destination vertices are known, how could I use Electro's rotation function to rotate it into the correct orientation before I move it to the correct place?
I've been racking my brain for two days trying to figure this out, looking at math that I don't understand dealing with Euler angles and such, but I'm still lost. Can anyone help me out?
Can you tell us more about the problem? It sounds odd phrased in this way. What else do you know about the final orientation you have to hit? Is it completely arbitrary or user-specified or can you use more knowledge to help solve the problem? Is there any other Electro API you could use to help?
If you really must solve this general problem, then too bad, it's hard, and underspecified. Here's some guy's code that may work, from euclideanspace.com.
First do the translation to bring one corner of the quadrilateral to the point you'd like it to be, then apply the three rotational transformations in succession:
If you know where the quad is, and you know exactly where it needs to go, and you're certain that there are no distortions of the quad to fit it into the place where it needs to go, then you should be able to figure out the angles using the vector scalar product.
If you have two vectors, the angle between them can be calculated by taking the dot product.

Resources