Currently working on creating a VR head tracking using Wii remotes have faced a error.
The class *** can be designed, but is not the first class in the file.Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again.
I have split the code in different pages however I receive the same error. This is the code I'm working on:
namespace WiiDesktopVR
{
class Point2D
{
public float x = 0.0f;
public float y = 0.0f;
public void set(float x, float y)
{
this.x = x;
this.y = y;
}
}
public class WiiDesktopVR : Form
{
struct Vertex
{
float x, y, z;
float tu, tv;
public Vertex(float _x, float _y, float _z, float _tu, float _tv)
{
x = _x; y = _y; z = _z;
tu = _tu; tv = _tv;
}
public static readonly VertexFormats FVF_Flags = VertexFormats.Position | VertexFormats.Texture1;
};
Vertex[] targetVertices =
{
new Vertex(-1.0f, 1.0f,.0f, 0.0f,0.0f ),
new Vertex( 1.0f, 1.0f,.0f, 1.0f,0.0f ),
new Vertex(-1.0f,-1.0f,.0f, 0.0f,1.0f ),
new Vertex( 1.0f,-1.0f,.0f, 1.0f,1.0f ),
};
}
}
Thanks
Move Point2D to the bottom of the file. Best practices state you should have only one class per file, so taking Stuart's advice and moving it to another file would be best.
namespace WiiDesktopVR
{
public class WiiDesktopVR : Form
{
struct Vertex
{
float x, y, z;
float tu, tv;
public Vertex(float _x, float _y, float _z, float _tu, float _tv)
{
x = _x; y = _y; z = _z;
tu = _tu; tv = _tv;
}
public static readonly VertexFormats FVF_Flags = VertexFormats.Position | VertexFormats.Texture1;
};
Vertex[] targetVertices =
{
new Vertex(-1.0f, 1.0f,.0f, 0.0f,0.0f ),
new Vertex( 1.0f, 1.0f,.0f, 1.0f,0.0f ),
new Vertex(-1.0f,-1.0f,.0f, 0.0f,1.0f ),
new Vertex( 1.0f,-1.0f,.0f, 1.0f,1.0f ),
};
}
class Point2D
{
public float x = 0.0f;
public float y = 0.0f;
public void set(float x, float y)
{
this.x = x;
this.y = y;
}
}
}
Related
I want to instantiate GameObjects(specifically hexagonal tiles) at the hexagonalCoodinates(hexcoordinates).
For this I wrote a custom coordinate system.
But I found out that unity doesn't accept anything other than Vector3 or transform.
How do I make it do that?
Or is there a easier way to do this?
This is the method to generate the gameObjects
private void TouchCell(Vector3 point)//This method instantiates cubes
{
point = transform.InverseTransformPoint(point);
HexCoordinates coordinates = HexCoordinates.FromPosition(point);
Instantiate(cubes, coordinates, Quaternion.identity);//<-The coordinate variable here is a hex coordinate.
Debug.Log("Touched at:" + coordinates);
}
And This is the Hex coordinate generator:
public struct HexCoordinates
{
public int X { get; private set; }
public int Z { get; private set; }
public int Y { get
{
return -X - Z;
} }
public HexCoordinates(int x,int z)
{
X = x;
Z = z;
}
public static HexCoordinates FromOffsetCoordinates(int x,int z)
{
return new HexCoordinates(x-z/2, z);
}
public override string ToString()
{
return "("+X.ToString()+","+Y.ToString()+","+Z.ToString()+")";
}
public string ToStringOnSeperateLines()
{
return X.ToString() + "\n" +Y.ToString()+ "\n" + Z.ToString();
}
public static HexCoordinates FromPosition(Vector3 point)//This converts the Vector3 to Hex coords
{
float x = point.x / (HexMetrics.InnerRadius * 2f);
float y = -x;
float offset = point.z / (HexMetrics.OuterRadius * 3f);
x -= offset;
y -= offset;
int iX = Mathf.RoundToInt(x);
int iY = Mathf.RoundToInt(y);
int iZ = Mathf.RoundToInt(-x - y);
if (iX + iY + iZ != 0)
{
float dX = Mathf.Abs(x-iX);
float dY = Mathf.Abs(y - iY);
float dZ = Mathf.Abs(-x-y-iZ);
if(dX>dY&&dX>dZ)
{
iX = -iY - iZ;
}
else if(dZ>dY)
{
iZ = -iX - iY;
}
}
return new HexCoordinates(iX,iZ);
}
}
Just convert from your HexCoordinates to Vector3 using any way:
create method for HexCoordinates, something like public Vector3 ToVector3() {...}
create implicit operator for implicit cast to Vector3
public struct HexCoordinates
{
public int X { get; private set; }
public int Z { get; private set; }
public int Y => -X - Z;
public HexCoordinates(int x,int z)
{
X = x;
Z = z;
}
...
public static implicit operator Vector3(HexCoordinates coords)
{
Vector3 result = // convert to Vector3
// Vector3 result = ToVector3() -- or like this for code reuse
return result;
}
public Vector3 ToVector3()
{
Vector3 result = //convert to Vector3
return result;
}
}
And then you can extend Unity's Object class and add overloading for Instantiate() method that will accept HexCoordinates, convert to Vector3 and call Instantiate()
public static class ObjectExtension
{
public static void Instantiate(this Object obj, Object origin, HexCoordinates coords, Quaternion q)
{
Vector3 position = coords.ToVector3();
obj.Instantiate(origin, position, q);
}
}
Also if you create implicit cast for your HexCoordinates to Vector3 you don't need to create overloading for Instantiate() method because converting will be implicitly
You are using using catlike coding's code. (This would have been helpful to mention in the question ;) ). In part 3 of the tutorial featuring this hex coordinate system, you can see how they would do something like below, accessing a hex inside of an array by calculating an index:
public HexCell GetCell (Vector3 position) {
position = transform.InverseTransformPoint(position);
HexCoordinates coordinates = HexCoordinates.FromPosition(position);
int index = coordinates.X + coordinates.Z * width + coordinates.Z / 2;
return cells[index];
}
So, since a HexCell has a transform.position, you can use that to get its center (making sure you don't access out of bounds):
private void TouchCell(Vector3 point)
{
point = transform.InverseTransformPoint(point);
HexCoordinates coordinates = HexCoordinates.FromPosition(point);
int index = coordinates.X + coordinates.Z * width + coordinates.Z / 2;
if (index >=0 && index < cells.Length)
{
Vector3 worldPos = cells[index].transform.position;
Instantiate(cubes, worldPos, Quaternion.identity);
Debug.Log("Touched at:" + coordinates);
}
}
Better yet, it may be worthwhile to make a method to retrieve this index, for the sake of code reuse:
private bool IsValidCellIndex(Vector3 point, out int index)
{
point = transform.InverseTransformPoint(point);
HexCoordinates coordinates = HexCoordinates.FromPosition(point);
index = coordinates.X + coordinates.Z * width + coordinates.Z / 2;
return index >=0 && index < cells.Length;
}
private void TouchCell(Vector3 point)
{
if (IsValidCellIndex(point, out int index))
{
Vector3 worldPos = cells[index].transform.position;
Instantiate(cubes, worldPos, Quaternion.identity);
Debug.Log("Touched at:" + worldPos);
}
}
Or, just use GetCell as the tutorial does :)
I am trying to learn c# and in c# I am trying to implement a factory design pattern to draw basic shapes like circle, rectangle, triangle, etc. I created a IShape Interface class. And other three Circle, Rectangle, and Triangle class. I also created a ShapeFactory static method. These classes sould be called after from compiler class and draw in another window i.e Output window. Below is the code.
//IShape Class
namespace CPaint.Class
{
public interface IShape
{
void Draw();
}
}
//Cicle Class
namespace CPaint.Class
{
internal class Circle : IShape
{
//public Output outputWindow;
private float x;
private float y;
private Color color;
private float radius;
private Graphics graphics;
public Circle(Color color, float x, float y, float radius)
{
this.color = color;
this.x = x;
this.y = y;
this.radius = radius;
}
public void Draw()
{
Pen pen = new Pen(Color.Black, 4);
SolidBrush brush = new SolidBrush(color);
graphics.FillEllipse(brush, x, y, radius, radius);
graphics.DrawEllipse(pen, x, y, radius, radius);
}
}
}
//Triangle class
namespace CPaint.Class
{
internal class Triangle : IShape
{
private float x;
private float y;
private Color color;
private float side1;
private float side2;
private float side3;
public Triangle(Color color, float x, float y, float side1, float side2, float side3)
{
this.color = color;
this.x = x;
this.y = y;
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public void Draw()
{
//will write drawing code here...
}
}
}
//Shape Factory Class
namespace CPaint.Class
{
public class ShapeFactory
{
public static IShape GetShape(string shapeType, Color color, float x, float y, float height, float width, float radius, float side1, float side2, float side3)
{
if (shapeType == null)
{
return null;
}
if (shapeType.Equals("circle"))
{
return new Circle(color, x, y, radius);
}
else if (shapeType.Equals("rectangle"))
{
return new Rectangle(color, x, y, height, width); ;
}
else if (shapeType.Equals("triangle"))
{
return new Triangle(color, x, y, side1, side2, side3);
}
else
{
return null;
}
}
}
}
//Compiler Class
if (code[0].Trim().ToLower().Equals("circle"))
{
try
{
int parameter1 = Int32.Parse(code[1]);
IShape shape = ShapeFactory.GetShape("circle", color, initX, initY, 0, 0, parameter1, 0, 0, 0);
outputWindow.Show();
//outputWindow.outputArea.Image=new ShapeFactory.GetShape("circle", color, initX, initY, 0, 0, parameter1, 0, 0, 0);
//outputWindow.outputArea.Image = shape;
output = "command executed successfully: parameter is" + parameter1;
//output = "is numeric ";
}
catch (Exception ex)
{
Console.WriteLine("Exception Message: " + ex.Message);
output = "\n Parameter Error : Invalid/Insufficient Parameter. \n";
}
}
Here in Compiler class, I want to open output window by outputWindow.show(); and then draw the shape in window by outputWindow.OutputArea.Image=...
I got stuck. Please help.
This post explains differences between
Factory,
Factory Method, and
Abstract Factory
design patterns.
Since you want to be able to create a whole family of objects that need to be of "the same kind", this can be done with Abstract Factory pattern.
The factory can have multiple creator methods. You can add parameters to the creator method(s) of the factory.
IShapesFactory.cs is the interface of the Abstract Factory:
public interface IShapesFactory
{
Circle MakeCircle(Color color, float x, float y, float radius);
Triangle MakeTriangle(Color color, float x, float y, float side1, float side2, float side3);
}
The implementation of the factory would handle passing the Graphics object to the shapes it creates.
ShapeFactory.cs
public class ShapeFactory: IShapeFactory
{
private Graphics _graphics;
public ShapeFactory(Graphics graphics)
{
_graphics = graphics;
}
public Circle MakeCircle(Color color, float x, float y, float radius)
{
// pass the Graphics object in the constructor of Circle
return new Circle(_graphics, color, x, y, radius);
}
[..other methods of the factory interface]
}
The factory in use:
IShapeFactory factory = new ShapeFactory(graphics);
var circle = factory.MakeCircle(Color.Blue, 10, 10, 20);
circle.Draw();
I'm doing all my expensive calculations in a background thread and need to be able to assign pictureBox1.Image after each cycle of calculations. I use to do this using delegates, but its been a long time and things don't seem to work that way anymore.
I tried putting the assignment in a WorkerCompleted event handler, but then I can't figure out how to restart backgroundWorker1. I've been at this all day and feel enormously frustrated, this use to be so easy to do.
This is the most relevant part of the code:
using System;
using System.Globalization;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using KnownColorsPalette;
namespace Color_Visualizer
{
public partial class Form1 : Form
{
static CultureInfo m_culture = CultureInfo.CurrentCulture;
double[,][] distances = new double[3000, 3000][];
FastPixel m_fp;
public Form1()
{
InitializeComponent();
WindowState = FormWindowState.Maximized;
this.pictureBox1.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(PictureBox1_LoadCompleted);
CoordinateSystem.AssignMe(this);
}
private void Form1_Load(object sender, EventArgs e)
{
ReadColors(); // Reads text file alternating name and RGB hex of
// Wikipedia's 1200+ named colors.
Point3D p = new Point3D(127.5, 127.5, 127.5);
foreach (FoundColors fc in m_lColors.Values)
fc.pt = fc.color - p;
Coord = new CoordinateSystem(new Plane(new Point3D(-127.5, -127.5, -127.5), new Point3D(-1, 0, 0)));
backgroundWorker1.RunWorkerAsync();
}
double fSpeed = 5;
Point3D m_pNormal = new Point3D();
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
double fAngle, fRadius, d;
Point3D vU, vV;
Point pLocation = new Point();
m_pNormal = Coord.Plane.Normal;
fAngle = Math.Atan2(m_pNormal.y, m_pNormal.x);
fRadius = Math.Sqrt(m_pNormal.x * m_pNormal.x + m_pNormal.y * m_pNormal.y);
fAngle += fSpeed * 180 / Math.PI;
m_pNormal.x = Math.Cos(fAngle) * fRadius;
m_pNormal.y = Math.Sin(fAngle) * fRadius;
m_fp.Lock();
m_fp.Clear(Color.Black);
foreach (FoundColors fc in m_lColors.Values)
{
vU = new Point3D(Coord.U);
d = dist(fc.pt, ref vU);
vV = Coord.V;
vV.mult(d);
pLocation.X = (int)(m_midHoriz + vU.norm());
pLocation.Y = (int)(m_midVert + vV.norm());
m_fp.SetPixel(pLocation, fc.color);
}
m_fp.Unlock();
}
double m_fDist, m_fDot;
public double dist(Point3D pt, ref Point3D vU)
{
double c1 = pt.dot(vU);
double c2 = vU.dot(vU);
double b = c1 / c2;
vU.mult(b);
m_fDot = pt.dot(Coord.Normal);
m_fDist = pt.norm(pt - vU);
return m_fDist;
}
double m_midHoriz, m_midVert;
private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
pictureBox1.Image = m_fp.Bitmap;
}
private void Form1_Resize(object sender, EventArgs e)
{
m_midHoriz = pictureBox1.Width / 2;
m_midVert = pictureBox1.Height / 2;
m_fp = new FastPixel(new Bitmap(pictureBox1.Width, pictureBox1.Height));
}
}
}
And this is part of my support code:
using System;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
namespace Color_Visualizer
{
public partial class Form1 : Form
{
class CoordinateSystem
{
const int MAX = 256;
const double PlaneWidth = 600;
static Form1 Me;
static Point3D axisZ = new Point3D(0, 0, 1);
static Point3D axisY = new Point3D(0, 1, 0);
private Plane m_plane = new Plane(new Point3D(128, 128, 128), new Point3D(-128, 0, 0));
private Point3D m_pV = new Point3D(0, 0, 0);
private Point3D m_pU = new Point3D(0, 0, 0);
private double m_fInc;
public CoordinateSystem(Plane axAxis)
{
m_fInc = PlaneWidth / Me.ClientSize.Height;
Plane = axAxis;
}
public static void AssignMe(Form1 form) { Me = form; }
public Point3D U { get { return m_pU; } protected set { m_pU = value; } }
public Point3D V { get { return m_pV; } protected set { m_pV = value; } }
public Point3D Normal { get { return m_plane.Normal; } set { m_plane.Normal = value; } }
static double COSerror = 0.99619469809174553229501040247389;
public Plane Plane
{
get { return m_plane; }
set {
m_plane = value;
if (m_plane.dot(axisZ) > COSerror)
U = U.cross(m_plane, axisY);
else
U = U.cross(m_plane, axisZ);
U.div(U.norm());
V = U.cross(U, m_plane);
V.div(V.norm());
}
}
}
[DebuggerDisplayAttribute("x = {x}, y = {y}, z = {z}")]
public class Point3D
{
public double x, y, z;
public Point3D(double _x, double _y, double _z) { x = _x; y = _y; z = _z; }
public Point3D(Point3D p) { x = p.x; y = p.y; z = p.z; }
public Point3D() { x = 0; y = 0; z = 0; }
public bool Equals(Point3D p) { return x == p.x & y == p.y & z == p.z; }
public override bool Equals(object obj) { return Equals((Point3D)obj); }
public static bool operator ==(Point3D p1, Point3D p2) { return p1.Equals(p2); }
public static bool operator !=(Point3D p1, Point3D p2) { return !p1.Equals(p2); }
public static Point3D operator -(Point3D e, Point3D s) { return new Point3D(e.x - s.x, e.y - s.y, e.z - s.z); }
public static Point3D operator +(Point3D e, Point3D s) { return new Point3D(e.x + s.x, e.y + s.y, e.z + s.z); }
public static Point3D operator *(double m, Point3D v) { return new Point3D(m * v.x, m * v.y, m * v.z); }
public static Point3D operator *(Point3D v, double m) { return new Point3D(v.x / m, v.y / m, v.z / m); }
public static Point3D operator /(double m, Point3D v) { return new Point3D(m * v.x, m * v.y, m * v.z); }
public static Point3D operator /(Point3D v, double m) { return new Point3D(v.x / m, v.y / m, v.z / m); }
public static implicit operator Color(Point3D p) { return Color.FromArgb((int)p.x, (int)p.y, (int)p.z); }
public static implicit operator Point3D(Color c) { return new Point3D(c.R, c.G, c.B); }
//public override int GetHashCode()
//{
// unchecked
// {
// var hash = new SpookyHash();
// hash.Update(x);
// hash.Update(y);
// hash.Update(z);
// return hash.Final().GetHashCode();
// }
//}
// dot product (3D) which allows vector operations in arguments
public double dot(Point3D u, Point3D v) { return u.x * v.x + u.y * v.y + u.z * v.z; }
public double dot(Point3D u) { return u.x * x + u.y * y + u.z * z; }
public double norm(Point3D v) { return Math.Sqrt(dot(v, v)); } // norm = length of vector
public double norm() { return Math.Sqrt(dot(this, this)); } // norm = length of vector
public double dist(Point3D u, Point3D v) { return norm(u - v); } // distance = norm of difference
public double dist(Point3D u) { return norm(this - u); }
public Point3D cross(Point3D u, Point3D v) { return new Point3D(u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x); }
public Point3D cross(Point3D u) { return new Point3D(u.y * z - u.z * y, u.z * x - u.x * z, u.x * y - u.y * x); }
public void add(Point3D p) { x += p.x; y += p.y; z += p.z; }
public void mult(double m) { x *= m; y *= m; z *= m; }
public void div(double m) { x /= m; y /= m; z /= m; }
}
class Plane : Point3D
{
Point3D m_pNormal;
public Plane(Point3D pOrigin, Point3D pNormal) : base(pOrigin) { m_pNormal = pNormal; }
public Plane(Point3D p) : base(p) { }
public Plane(double x, double y, double z) : base(x, y, z) { }
public Point3D Normal { get { return m_pNormal; } set { m_pNormal = value; } }
public double PointToPlane(Point3D p) { return p.dot(Normal); }
}
private CoordinateSystem m_coordSys;
private CoordinateSystem Coord
{
get { return m_coordSys; }
set { m_coordSys = value; }
}
}
}
And more support code:
[DebuggerDisplayAttribute("{name}, R={color.R}, G={color.G}, B={color.B}")]
class FoundColors
{
public Color color = Color.Empty;
public string name = "";
public CIELab_Color cLab;
public Point3D pt;
public List<int> lClosest = new List<int>();
public int nFarthest;
public FoundColors(FoundColors fc)
{
color = fc.color;
name = fc.name;
cLab = new CIELab_Color(fc.cLab.CIE_L, fc.cLab.CIE_a, fc.cLab.CIE_b);
lClosest.AddRange(fc.lClosest);
nFarthest = fc.nFarthest;
}
public FoundColors() { }
}
The Invoke method does work if I immediately Thread.Sleep() for 200ms.
m_fp.Unlock();
this.Invoke((MethodInvoker)delegate () { pictureBox1.Image = m_fp.Bitmap; });
Thread.Sleep(200);
} while (true);
Haven't tested the lower limit, but it won't be consistent across machines, so reporting it won't be informative.
Open a new thread and create a while in of the function
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);
workerThread.Start();
public void DoWork()
{
while (!_shouldStop)
{
//REFRESH YOUR IMAGE
}
}
I fight with that for 2 days and searching internet but i don't have any answer for my problem. In few words i want to have orbiting camera on screen and objects which I want also rotate independently from my camera. I implement Arcball rotation controller and I use it to rotate all object in the scene (maybe this cause problem). It looks ok when i have camera in starting point but when i start moving it and I want to rotate objects on the scene its looks like all object rotate like im in the camera starting point. Maybe here someone can help :D
My Arcball class:
public class ArcBallGL{
private bool isBeingDragged;
private Vector3 downPoint;
private Vector3 currentPoint;
public Vector3 BallCenter { get; set; }
private Quaternion down;
public Quaternion Now { get; set; }
private float width, height;
public ArcBallGL()
{
Reset();
width = height = 0.0f;
}
public void Reset()
{
isBeingDragged = false;
downPoint = Vector3.Zero;
currentPoint = Vector3.Zero;
down = Quaternion.Identity;
Now = Quaternion.Identity;
}
public void SetWindow(float width, float height)
{
this.width = width;
this.height = height;
}
public void OnBeginDrag(float x, float y)
{
isBeingDragged = true;
down = Now;
downPoint = ScreenToVector(x, y);
}
public void OnMove(float x, float y)
{
if (isBeingDragged == true)
{
currentPoint = ScreenToVector(x, y);
Now = down * QuatFromBallPoints(downPoint, currentPoint);
}
}
public void OnStopDrag()
{
isBeingDragged = false;
}
public Matrix4 GetRotationMatrix()
{
return Matrix4.CreateFromQuaternion(Now);
}
public Matrix4 GetRotationMatrixFormViewPort(Matrix4 camera)
{
var viewMatrix = Matrix4.CreateFromQuaternion(Now).Inverted();
viewMatrix.Normalize();
return viewMatrix * Matrix4.CreateFromQuaternion( camera.ExtractRotation()).Inverted();
}
private Vector3 ScreenToVector(float screentPointX, float screenPointY)
{
float x = (screentPointX - width * 0.5f) / (width * 0.5f);
float y = (screenPointY - height * 0.5f) / height;
float z = 0.0f;
float mag = x * x + y * y;
if (mag > 1.0f)
{
float scale = 1.0f / (float)Math.Sqrt(mag);
x *= scale;
y *= scale;
}
else
{
z = (float)Math.Sqrt(1.0f - mag);
}
return new Vector3(-x, y, -z);
}
private Quaternion QuatFromBallPoints(Vector3 from, Vector3 to)
{
float dot = Vector3.Dot(from, to);
Vector3 part = Vector3.Cross(from, to);
return new Quaternion(part.X, part.Y, part.Z, dot);
}
}
My Camera class:
public class Camera{
public Vector3 Position = Vector3.Zero;
private Vector3 Up = Vector3.UnitY;
public float MoveSpeed = 0.2f;
public float RotationSpeed = 0.01f;
public Quaternion RotQuar { get; set; }
public Camera(Vector3 startPos = new Vector3())
{
Position = startPos;
RotQuar = Quaternion.Identity;
}
public Matrix4 GetViewMatrix(Vector3 target)
{
//RotQuar.Conjugate();
var rotMatrix = Matrix4.CreateFromQuaternion(RotQuar);
var camPosition = Vector3.Transform(Position, rotMatrix);
var camDir = target - camPosition;
camDir.Normalize();
var camRight = Vector3.Cross(camDir, Vector3.UnitY);
camRight.Normalize();
var camUp = Vector3.Cross(camRight, camDir);
camUp.Normalize();
var cameraMat = Matrix4.LookAt(camPosition, target , camUp) ;
return cameraMat;
}
}
My mouse move methood
private void MouseMoved(object sender, MouseMoveEventArgs e)
{
if (e.Mouse.IsButtonDown(MouseButton.Left) && RIsDown)
{
foreach (Shape s in _shapes)
{
if (s.IsSelected)
{
Rotation.OnMove(e.Position.X, e.Position.Y);
s.RoatationQuat = Rotation.Now;
s.RotationMatrix = Rotation.GetRotationMatrixFormViewPort(_camera.GetViewMatrix(Vector3.Zero));
}
}
}
if (e.Mouse.IsButtonDown(MouseButton.Left) && CIsDown)
{
Rotation.OnMove(e.Position.X, e.Position.Y);
_camera.RotQuar = Rotation.Now;
}
}
I use index buffer to draw my objects on the scene. I think the problem lies in calculating the camera coords to objects coords but i don't know where i must calculate it. Thanks for help
Ok,
It is not possible to store a struct instance inside a struct of the same type. So can anyone help me find a workaround please?
I need to store a vector3 inside a vector3 like this:
public struct Vector3{
float x,y,z;
Vector3 normalized;
}
Obviously, it creates an endless cycle as one would create a new one that creates a new one and so on...
So how would one do that? I would need my normalized to be a Vector3 since it needs to be recognized as such and cannot be any other naming.
Finally, I know this can be achieved with classes but I would not want.
Thanks
Well, a struct is a value type. Declaring a recursive struct would create an infinitely big struct! A workaround would be to declare it as class instead. But here I would simply declare Normalized as a property.
public struct Vector3 {
public Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
public float X { get; private set; }
public float Y { get; private set; }
public float Z { get; private set; }
public float Length {
get { return (float)Math.Sqrt(X * X + Y * Y + Z * Z); }
}
Vector3 Normalized {
get {
float l = Length;
return new Vector3(X / l, Y / l, Z / l);
}
}
}
You cannot store a struct of type X inside a struct of type X. However, you probably don't want to do this anyway, because in general, structs (and classes, for that matter) should only store the data they need to be complete. Instead, you can have a function that builds and returns the 'normalized' version of the struct:
public struct Vector3
{
float x,y,z;
public Vector3 Normalized
{
get
{
... build the normalized struct and return it ...
}
}
}