COLLISION SHIT FINALLY WORKING WOOO

next:

-Find collision contact point.
This commit is contained in:
Marquitos
2016-05-16 18:56:43 -03:00
parent e7f07a6e53
commit f744a744b7
73 changed files with 274 additions and 505 deletions

View File

@@ -4,13 +4,50 @@ using System.Collections.Generic;
namespace CharacterEngine.Primitives
{
public class CEProjection
{
public Vector2 axis;
public float max;
public float min;
public CEProjection() { }
public CEProjection (CECollider coll, Vector2 axis)
{
Project(coll, axis);
}
public void Project(CECollider coll, Vector2 axis)
{
for (int i = 0; i < 4; i++)
{
Vector2 p = Vector3.Project(coll.GetPoint(i), axis);
float distance = p.magnitude * Mathf.Sign(Vector2.Dot(p, axis));
if (i == 0)
{
max = min = distance;
}
else
{
if (distance > max) max = distance;
else if (distance < min) min = distance;
}
}
}
}
public class CECollider
{
public static int[,] edges = new int[,]
{
{0, 2}, {0, 3}, {1, 2},{1, 3}
};
public static int[,] pointsNeighbours = new int[,]
{
{2, 3}, {3, 2}, {0, 1},{1, 0}
};
private BoxCollider2D collider;
public BoxCollider2D Coll
{
@@ -70,22 +107,30 @@ namespace CharacterEngine.Primitives
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public virtual Vector2 GetPoint (int index)
public Vector2 GetPoint (int index)
{
return position + (Vector2)(collider.transform.rotation * Vector2.Scale(localPoints[index], collider.transform.lossyScale));
}
public virtual Vector2 GetPoint(int index, Vector2 position)
public Vector2 GetPoint(int index, Vector2 position)
{
return position + (Vector2)(collider.transform.rotation * Vector2.Scale(localPoints[index], collider.transform.lossyScale));
}
public virtual Vector2 GetAABBPoint(int index)
public Vector2 GetAABBPoint(int index)
{
return position + (Vector2)(Vector2.Scale(localPoints[index], collider.transform.lossyScale));
}
public virtual Vector2 GetAABBPoint(int index, Vector2 position)
public Vector2 GetAABBPoint(int index, Vector2 position)
{
return position + (Vector2)(Vector2.Scale(localPoints[index], collider.transform.lossyScale));
}
public Vector2 GetNormal(int index)
{
return Vector3.Cross(GetPoint(edges[index, 0]) - GetPoint(edges[index, 1]), Vector3.forward).normalized;
}
public Vector2 GetNormal(int index, Vector2 position)
{
return Vector3.Cross(GetPoint(edges[index, 0], position) - GetPoint(edges[index, 1], position), Vector3.forward).normalized;
}
public bool ContainsPoint (Vector2 p)
{
@@ -96,6 +141,11 @@ namespace CharacterEngine.Primitives
return insideX && insideY;
}
public void Project(Vector2 normal, Vector2 p)
{
}
/// <summary>
/// Collision test along a path
/// </summary>

View File

@@ -20,6 +20,37 @@ namespace CharacterEngine.Primitives
{
Vector2 closestPoint = Vector2.zero;
CECollision collision = new CECollision();
///IS NOT MTV THO (music television) is MINIMUN TRANSLATION VECTOR (funny joke uh)
Vector2 MTV = Vector2.zero;
float minimunOverlap = 0.0f;
Vector2[] axes = new Vector2[8];
for (int i = 0; i < 8; i++)
{
axes[i] = i < 4 ? a.GetNormal(i) : b.GetNormal(i - 4);
}
for (int i = 0; i < axes.Length; i++)
{
CEProjection projectionA = new CEProjection(a, axes[i]);
CEProjection projectionB = new CEProjection(b, axes[i]);
float overlapValue = Mathf.Abs(projectionA.max - projectionB.min);
if (i == 0 || overlapValue < minimunOverlap)
{
minimunOverlap = overlapValue;
MTV = axes[i] * minimunOverlap;
}
}
collision.impulse = MTV;
collision.normal = collision.impulse.normalized;
///COULDNT DETRMINE YET HOW TO FIND POINT ILL DO IT ASFASDGSD
collision.point = Vector2.zero;
/*
for (int i = 0; i < 4; i++)
{
Vector2 currentPoint = a.GetPoint(i);
@@ -49,9 +80,10 @@ namespace CharacterEngine.Primitives
return collision;
}
}
*/
//IF REACH HERE DIDNT RESOLVE SHIT
Debug.Log("Could not resolve collision");
//Debug.Log("Could not resolve collision");
return collision;
}
@@ -88,6 +120,12 @@ namespace CharacterEngine.Primitives
return p + (Vector2)Vector3.Project(distanceToPointA, segmentNormal);
}
public static Vector2 ClosestVectorToPlane(Vector2 p, Vector2 normal, Vector2 position)
{
Vector2 plane = Vector3.Cross(normal, Vector3.forward);
Vector2 distanceToPosition = position - p;
return p + (Vector2)Vector3.Project(distanceToPosition, plane);
}
}
}

View File

@@ -17,6 +17,8 @@ namespace CharacterEngine.Controller
#endregion
protected Vector2 moveVelocity;
private CEPrimitives primitives;
/*Summary:
@@ -30,12 +32,6 @@ namespace CharacterEngine.Controller
primitives = new CEPrimitives(this);
}
// Use this for initialization
void Start()
{
}
/*Summary:
In Updates, we have both functions native from unity Monobehaviour: Update & FixedUpdate.
The difference of this both is that one (Update) excecute every frame, and the other one, is executed
@@ -43,15 +39,20 @@ namespace CharacterEngine.Controller
while "Fixed Update" dont.
*/
// Update is called once per frame
void Update()
{
}
void FixedUpdate ()
{
MoveStep();
primitives.Step();
}
void MoveStep ()
{
primitives.AddVelocity(moveVelocity);
}
public void Move (Vector2 velocity)
{
moveVelocity = velocity;
}
}
}

View File

@@ -59,6 +59,11 @@ namespace CharacterEngine.Primitives
PhysicsStep();
}
public void AddVelocity ( Vector2 velocity )
{
this.velocity += velocity;
}
void PhysicsStep()
{
GetVelocity();
@@ -78,7 +83,7 @@ namespace CharacterEngine.Primitives
void GravityStep()
{
velocity.y += gravity * controller.weight * Time.deltaTime * 1.2f;
velocity.x = -gravity * 0.5f;
velocity.x = 10.0f;
}
void CollisionStep()

View File

@@ -2,21 +2,34 @@
using System.Collections;
using CharacterEngine.Primitives;
public class test : MonoBehaviour {
public class Test : MonoBehaviour {
public BoxCollider2D coll;
protected CECollider ce;
// Use this for initialization
void Start () {
ce = new CECollider(coll);
}
// Update is called once per frame
void Update () {
if (ce.ContainsPoint(transform.position))
{
Vector2 p = CECollision.ClosestPointInBounds(ce, transform.position);
Debug.DrawLine(p, transform.position, Color.red);
}
}
public Transform a;
public Transform b;
public BoxCollider2D c;
public BoxCollider2D c1;
public BoxCollider2D c2;
private CECollider cec;
private CECollider cec1;
private CECollider cec2;
// Use this for initialization
void Start () {
cec = new CECollider(c);
cec1 = new CECollider(c1);
cec2 = new CECollider(c2);
}
// Update is called once per frame
void Update () {
Vector2 axis = (a.position - b.position).normalized;
CEProjection p = new CEProjection(cec, axis);
CEProjection p1 = new CEProjection(cec1, axis);
CEProjection p2 = new CEProjection(cec2, axis);
Debug.DrawLine(axis * p.min, axis * p.max, Color.red);
Debug.DrawLine(axis * p1.min, axis * p1.max, Color.blue);
Debug.DrawLine(axis * p2.min, axis * p2.max, Color.yellow);
}
}

View File

@@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: f0ed370640e457f4ca5447af56a3319c
timeCreated: 1463341313
guid: 7a07c3efc56913144be618cb56ec5107
timeCreated: 1463429200
licenseType: Free
MonoImporter:
serializedVersion: 2