Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change structs back to classes #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions SharpBox2D/Common/Mat22.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace SharpBox2D.Common
/**
* A 2-by-2 matrix. Stored in column-major order.
*/
public struct Mat22 : IEquatable<Mat22>
public class Mat22 : IEquatable<Mat22>
{
public Vec2 ex, ey;

Expand All @@ -51,6 +51,12 @@ public override string ToString()
* @param c1 Column 1 of matrix
* @param c2 Column 2 of matrix
*/

public Mat22()
{
ex = new Vec2(1, 0);
ey = new Vec2(0, 1);
}

public Mat22(Vec2 c1, Vec2 c2)
{
Expand Down Expand Up @@ -308,7 +314,7 @@ public Mat22 mul(Mat22 R)

public void mulLocal(Mat22 R)
{
mulToOut(R, ref this);
mulToOut(R, this);
}

public void mulToOut(Mat22 R, ref Mat22 m)
Expand All @@ -323,6 +329,18 @@ public void mulToOut(Mat22 R, ref Mat22 m)
m.ey.y = tempy2;
}

public void mulToOut(Mat22 R, Mat22 m)
{
float tempy1 = this.ex.y*R.ex.x + this.ey.y*R.ex.y;
float tempx1 = this.ex.x*R.ex.x + this.ey.x*R.ex.y;
m.ex.x = tempx1;
m.ex.y = tempy1;
float tempy2 = this.ex.y*R.ey.x + this.ey.y*R.ey.y;
float tempx2 = this.ex.x*R.ey.x + this.ey.x*R.ey.y;
m.ey.x = tempx2;
m.ey.y = tempy2;
}

public void mulToOutUnsafe(Mat22 R, ref Mat22 m)
{
Debug.Assert(m != R);
Expand Down Expand Up @@ -360,7 +378,7 @@ public Mat22 mulTrans(Mat22 B)

public void mulTransLocal(Mat22 B)
{
mulTransToOut(B, ref this);
mulTransToOut(B, this);
}

public void mulTransToOut(Mat22 B, ref Mat22 m)
Expand All @@ -379,6 +397,22 @@ public void mulTransToOut(Mat22 B, ref Mat22 m)
m.ey.y = y2;
}

public void mulTransToOut(Mat22 B, Mat22 m)
{
/*
* ref.ex.x = Vec2.dot(this.ex, B.ex); ref.ex.y = Vec2.dot(this.ey, B.ex); ref.ey.x =
* Vec2.dot(this.ex, B.ey); ref.ey.y = Vec2.dot(this.ey, B.ey);
*/
float x1 = this.ex.x*B.ex.x + this.ex.y*B.ex.y;
float y1 = this.ey.x*B.ex.x + this.ey.y*B.ex.y;
float x2 = this.ex.x*B.ey.x + this.ex.y*B.ey.y;
float y2 = this.ey.x*B.ey.x + this.ey.y*B.ey.y;
m.ex.x = x1;
m.ey.x = x2;
m.ex.y = y1;
m.ey.y = y2;
}

public void mulTransToOutUnsafe(Mat22 B, ref Mat22 m)
{
Debug.Assert(B != m);
Expand Down
9 changes: 8 additions & 1 deletion SharpBox2D/Common/Mat33.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace SharpBox2D.Common
* @author Daniel Murphy
*/

public struct Mat33
public class Mat33
{
public static readonly Mat33 IDENTITY = new Mat33(new Vec3(1, 0, 0), new Vec3(0, 1, 0), new Vec3(0,
0, 1));
Expand All @@ -54,6 +54,13 @@ public Mat33(Vec3 argCol1, Vec3 argCol2, Vec3 argCol3)
ey = argCol2.clone();
ez = argCol3.clone();
}

public Mat33()
{
ex = IDENTITY.ex.clone();
ey = IDENTITY.ex.clone();
ez = IDENTITY.ex.clone();
}

public void setZero()
{
Expand Down
7 changes: 6 additions & 1 deletion SharpBox2D/Common/Rot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

namespace SharpBox2D.Common
{
public struct Rot : IEquatable<Rot>
public class Rot : IEquatable<Rot>
{
public float s;
public float c; // sin and cos
Expand All @@ -42,6 +42,11 @@ public Rot(float angle) : this()
{
set(angle);
}

public Rot()
{
set(0);
}

public float getSin()
{
Expand Down
8 changes: 7 additions & 1 deletion SharpBox2D/Common/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace SharpBox2D.Common
* orientation of rigid frames.
*/

public struct Transform : IEquatable<Transform>
public class Transform : IEquatable<Transform>
{
/** The translation caused by the transform */
public Vec2 p;
Expand All @@ -58,6 +58,12 @@ public Transform(Vec2 _position, Rot _R)
p = _position.clone();
q = _R.clone();
}

public Transform()
{
p = new Vec2();
q = new Rot();
}

/** Set this to equal another transform. */

Expand Down
18 changes: 17 additions & 1 deletion SharpBox2D/Common/Vec2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace SharpBox2D.Common
* A 2D column vector
*/

public struct Vec2 : IEquatable<Vec2>
public class Vec2 : IEquatable<Vec2>
{
public float x, y;

Expand All @@ -40,6 +40,12 @@ public Vec2(float x, float y)
this.x = x;
this.y = y;
}

public Vec2()
{
this.x = 0;
this.y = 0;
}

public Vec2(Vec2 toCopy) : this(toCopy.x, toCopy.y)
{
Expand Down Expand Up @@ -316,11 +322,21 @@ public override int GetHashCode()

public static bool operator ==(Vec2 left, Vec2 right)
{
if (ReferenceEquals(null, left))
return false;
if (ReferenceEquals(null, right))
return false;

return left.Equals(right);
}

public static bool operator !=(Vec2 left, Vec2 right)
{
if (ReferenceEquals(null, left))
return false;
if (ReferenceEquals(null, right))
return false;

return !left.Equals(right);
}

Expand Down
11 changes: 9 additions & 2 deletions SharpBox2D/Common/Vec3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace SharpBox2D.Common
* @author Daniel Murphy
*/

public struct Vec3 : IEquatable<Vec3>
public class Vec3 : IEquatable<Vec3>
{
public float x, y, z;

Expand All @@ -42,6 +42,13 @@ public Vec3(float argX, float argY, float argZ)
y = argY;
z = argZ;
}

public Vec3()
{
x = 0;
y = 0;
z = 0;
}

public Vec3(Vec3 copy)
{
Expand All @@ -57,7 +64,7 @@ public Vec3 set(Vec3 vec)
z = vec.z;
return this;
}

public Vec3 set(float argX, float argY, float argZ)
{
x = argX;
Expand Down
2 changes: 1 addition & 1 deletion SharpBox2D/Particle/ParticleDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ParticleDef
* chained by logical sums, for example: pd.flags = ParticleType.b2_elasticParticle |
* ParticleType.b2_viscousParticle.
*/
internal int flags;
public int flags;

/** The world position of the particle. */
public Vec2 position = new Vec2();
Expand Down