Microsoft.Xna.Framework.Graphics.Viewport.Project C# (CSharp) Method

Project() public method

Projects a Vector3 from world space into screen space.
public Project ( System.Vector3 source, System.Matrix projection, System.Matrix view, System.Matrix world ) : System.Vector3
source System.Vector3 The to project.
projection System.Matrix The projection .
view System.Matrix The view .
world System.Matrix The world .
return System.Vector3
        public Vector3 Project(Vector3 source, Matrix projection, Matrix view, Matrix world)
        {
            Matrix matrix = Matrix.Multiply(Matrix.Multiply(world, view), projection);
		    Vector3 vector = Vector3.Transform(source, matrix);
		    float a = (((source.X * matrix.M14) + (source.Y * matrix.M24)) + (source.Z * matrix.M34)) + matrix.M44;
		    if (!WithinEpsilon(a, 1f))
		    {
		        vector.X = vector.X / a;
		        vector.Y = vector.Y / a;
		        vector.Z = vector.Z / a;
		    }
		    vector.X = (((vector.X + 1f) * 0.5f) * this.width) + this.x;
		    vector.Y = (((-vector.Y + 1f) * 0.5f) * this.height) + this.y;
		    vector.Z = (vector.Z * (this.maxDepth - this.minDepth)) + this.minDepth;
		    return vector;
        }

Usage Example

Example #1
0
 public Butterfly(Vector3 Position, float rotation, Viewport ViewPort, Matrix View, Matrix Projection, ButterflyColors color)
 {
     world = Matrix.CreateFromAxisAngle(Vector3.Right, -1 * (float)(Math.PI / 2f)) * Matrix.CreateTranslation(Position);
     Vector3 ScreenProjection = ViewPort.Project(Position, Projection, View, Matrix.CreateScale(0.1f) * Matrix.CreateFromAxisAngle(Vector3.Right, -1 * (float)(Math.PI / 2f)) * Matrix.CreateTranslation(Position));
     hitbox = new Hitbox(new Rectangle((int)(ScreenProjection.X - padX), (int)(ScreenProjection.Y - padY), 2 * padX, 2 * padY));
     viewPort = ViewPort;
     projection = Projection;
     view = View;
     hitbox.Entered += new Hitbox.EnteredEventHandler(hitbox_Entered);
     hitbox.Exited += new Hitbox.ExitedEventHandler(hitbox_Exited);
     timeSelected = new Stopwatch();
     myColor = color;
     hidden = false;
     position = Position;
     startPosition = Position * 20;
     msOffset = rand.Next(0, 500);
     wanderDirection = new Vector2();
     tether = Vector2.Transform(new Vector2(Position.X, Position.Y), Matrix.CreateScale(0.1f));
 }
All Usage Examples Of Microsoft.Xna.Framework.Graphics.Viewport::Project