How i can calculate 2d bounding box with 3d transformation - c#

I'm working on opengl project.
I set up perspective projection and render a transformed rectangle (rotated, scaled)
How i can calculate rectangle's bounding box (rectangle position,size)
Thank you

You'd run the rectangle through the same matrices that OpenGL does to transform the 3D points into 2D screen-space ones. Get your input vectors, multiply them by any that you want to apply to the object, ModelView matrix, Projection matrix, then you have screen-space coords. Then check whether the resulting coordinates are on-screen, then you can calculate the minimum/maximum X and Y coordinates, and you have your bounding rectangle.
See also here (9.100), if you've got the GLU utility library functions available:
http://www.opengl.org/resources/faq/technical/transformations.htm
Hope that helps.

Related

How to rotate a bounding box in XNA?

I'm trying to rotate a bounding box in xna, this is how I usually do it:
new Rectangle((int)position.X, (int)position.Y, (int)texture.Width, (int)texture.Height);
but It currently does not return the rotation that is in my draw code:
spriteBatch.Draw(texture, position, null, Color.White, rotation, origin, scale, SpriteEffects.None, 1f);
how do I rotate it with the player? I was trying to figure this out and stumbled across this:
2D BoundingRectangle rotation in XNA 4.0
but I'm not quite sure how to implement the Matrices he's talking about,and I'm making a 2d game as opposed to the 3d one here.
The Rectangle structure is an axis aligned bounding rectangle. The sides will always be parallel to the X and Y axes. If you want a rotated bounding rectangle you have to implement it yourself.
One algorithm for calculating collision of rotated rectangles is the Separating Axis Theorem.
Matrices are, as stated in the linked answer, essential in computer graphics. You can rotate a shape by rotating each of its vertices with the same rotation matrix. This is the general rotation matrix for two dimensions:
Matrices are then "applied" to vectors with matrix-vector multiplication:
which equals
I struggled with this problem for two days before finding the solution
Rotating bounding box
Hope this will help !

Get rotation of a 3D triangle

I have three Vector3 points in 3D space. I need to copy the rotation (the tangent?) of this triangle to the orientation of a 3D model. How can I calculate the triangles Vector3 tangent or create a rotation matrix out of those points?
Finding the angle of a triangle is described here: Find the normal angle of the face of a triangle in 3D, given the co-ordinates of its vertices
Suppose you find the normal and call it N'. It should be trivial for you to write the normal of the "unrotated" triangle, N, eg <1, 0, 0>. It should also be trivial to figure out how to rotate from N to N' and you can create a rotation matrix for it with Matrix.CreateFromAxisAngle in XNA. This matrix should rotate everything like you want.

create 3D rectangle using c#

I read about rectangle structure in c# and the intersection function in it
My Question is: how to custom it such that I can have a 3D rectanlge, have x,y,z coordinates
and get it intersection with another one ?
Any idea
Just create your own. Here are some ideas:
a 3D rectangle not only has a width and a height, but also a plane
planes can be described with a normal vector and a point (origin)
the origin would be similar to the (x, y) in the 2D rectangle, that is, the "upper left" point, but any would do
intersecting with another rectangle could be as easy as intersecting the two plains and then checking to see if the intersection line "cuts" any of the rectangles
there are tons of math related websites to check for the formulas on how to do this
chances are pretty good, that in your application you won't need to do this in an optimized manner. Really. Just code it already and try it out. You can optimize later.
EDIT:
Wait. On second thoughts: An origin, a height, a width and a normal vector won't really cut it, since you don't have a sense of "up" as you do in 2D.
So, scratch that. Thinking about it reveals that the width and the height in 2D are actually vectors two, except that their direction is implied: Width is the length of a vector in x direction, Height is the length of a vector in y direction.
So, model your rectangle like this:
a point (Origin)
a vector Width (this is often called u in maths)
a vector Height (this is often called v in maths)
the normal vector is not necessary anymore since it is can be calculated by the vectorial product of Width x Height
The three other points of your rectangle can then be calculated as:
Origin + Width
Origin + Width + Height
Origin + Height
The rectangle class you have linked to models a 2D rectangle (I don't know what a 3D rectangle would be, BTW).
Pretty much the whole System.Drawing namespace deals with 2D, so you can't customise it that way.
The System.Drawing parent namespace contains types that support basic GDI+ graphics functionality. Child namespaces support advanced two-dimensional and vector graphics functionality, advanced imaging functionality, and print-related and typographical services.
(emphasis mine)
(about the intersection function)
You cannot create such a function.
The intersecting function of 2 rectangles in 2D is interesting because it returns you a third rectangle (than can be empty).
Intersection of 2 "3D rectangles" in space is not always a 3D rectange!
(for example take 2 identical rectangles and rotate one, then take the intersection...)
So you cannot just create a rectangle object, then an intersection function that returns a rectangle object.
You need more complete 3D object management library.
remark:
A 3D rectangle is delimited by 6 planes.
so you can identify it by 6 constraints on x,y,z
Then the intersection of 2 3D rectangles will just be a 3D object identified by 12 contraints.
If these 12 constraints can be simplfied to 6 ones it can be a rectange (but it's not always the case)
and if it cannot then it's not a rectangle.

How to translate a sprite and a vertex geometry by the same distance?

I'm trying to build up a 2.5 engine with XNA. Basically, I want to display a 2D sprites (the main hero and other monsters) in a 3D background. The game will be a platform.
Now, using a translation matrix on a sprite doesn't yield the same result of translate a vertex geometry in world space.
I mean, if I apply
Matrix.CreateTranslation(new Vector3(viewportWidth / 2, viewportHeight / 2, 0));
the sprite will be translate at the middle of screen (starting from the display upper left origin). But, if I apply the same transform to a cube in world space, it will translate very far. This doesn't suprising me, but I wonder of to translate a sprite and a 3D object by the same distance, ignoring all the project/unproject coord stuffs.
Thanks!
There are traditionally three matrices: World, View and Project.
BasicEffect, and most other 3D Effects, simply have those matrices. You use Project to define how points are projected from the 3D world onto the 2D viewport ((-1,-1) in the bottom-left of the viewport to (1,1) in the top-right). You set View to move your camera around in world space. And you use World to move your models around in world space.
SpriteBatch is a bit different. It has an implicit Project matrix that causes your world space to match the viewport's client space ((0,0) in the top-left and (width,height) in the bottom-right). You can pass a transformMatrix matrix to Begin which you can generally think of like the View matrix. And then the parameters you pass to Draw (position, rotation, scale, etc) work like the World matrix would.
If you need to do "weird" things to your World or Project matrices in SpriteBatch, you can just build those transforms into your transformMatrix. It may just involve some maths to "undo" the built-in transformations.
In XNA 4 you can also use an Effect (like BasicEffect) directly in SpriteBatch, which you can provide with arbitrary matrices (details).

DirectX Z order

I have to draw a map with Managed DirectX. The map arrived in MapInfo format (lines, polylines, regions/polygons). Polygons are already triangulated (done with GLUtesselator).
The idea:
GPS Coordinates are converted to x,y points (Mercator Projection)
I use PositionColored VertexFormat
Center of the view is [x,y] (can modify by mouse move)
Camera is always positioned to [x,y,z] where z is the zoom (-100 by default, can modify by mouse wheel)
Camera target is: [x,y,0], camera up: [0,1,0]
The layers of the map are positioned by Z (+1.0, 0.99, 0.98, 0.97...etc)
I can already do:
Draw lines and polylines
Draw one layer of polygons
My problem is: when I want to draw all layers I see only one of them. I think there is some problem with z ordering. What should I do to solve this? Modify the RenderState? The best would be if I could draw as in GDI (first in the back, last in the front).
Other question: how can I get the coordinate of a pixel under the mouse cursor? (in the GDI version of the map I could do it because I used my own viewport for rendering, but now directx do everything)
Thanks!
If your map is purely 2D, make sure that Z buffering is turned off. Once it is, things will display in the order you draw them in.

Categories

Resources