- Home /
Circle Collision class not positioning itself correctly
Ok I am using unity3D and the Futile framework. I have everything drawing and working correctly with Futile, but I am trying to attach a circle collision class to the FSprite that I have. I must be missing something but I have tried everything I can think of.
Right now, it positions itself offset from the center of the texture. The picture below shows what I am talking about. (The blue circle filled circle is my round Planet and the red circle is the circle class)
Below is my Planet class constructor
public Planet(string texture, Vector2 position, Team team, Level level = Level.One)
: base(texture)
{
Debug.Log("Texture Rect width: " + this.textureRect.width.ToString());
//this.anchorX = .5f;
//this.anchorY = .5f;
this.Position = position;
collision = new Circle(this.textureRect.width / 2, this.Position);
collision.Position = this.Position;
FAtlasManager am = Futile.atlasManager;
levelOneTexture = am.GetElementWithName("Circle.png");
levelOneTexture = am.GetElementWithName("Planet_1.png");
levelTwoTexture = am.GetElementWithName("Planet_2.png");
levelThreeTexture = am.GetElementWithName("Planet_3.png");
ChangePlanetTeam(team);
ChangePlanetLevel(level);
SetPlanetLevelTexture(level);
float rot = RXRandom.Range(0, 2 * Mathf.PI);
this.rotation = rot;
}
And here is my Circle class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
public class Circle
{
private float radius = 1f;
private Vector2 position = Vector2.zero;
private Vector2 origin = Vector2.zero;
public float Radius
{
get { return radius; }
set { radius = value; }
}
public Vector2 Position
{
get { return position; }
set
{
float x = value.x + Radius;
float y = value.y + Radius;
position = new Vector2(x, y);
//position = value;
}
}
public float X { get { return Position.x; } }
public float Y { get { return Position.y; } }
public Circle(float radius, Vector2 position)
{
Debug.Log("Circle position: " + position.ToString());
this.Position = position;
this.radius = radius;
Debug.Log("Radius: " + radius.ToString());
}
public void DrawRadius()
{
Debug.DrawLine(new Vector3(this.X, this.Y, 0), new Vector3(this.X + Radius, this.Y + radius,0));
}
/// <summary>
/// Test if two circles are currently colliding.
/// </summary>
/// <param name="a">One of the circles to test</param>
/// <param name="b">The other circle to test</param>
/// <returns>True if collision is present</returns>
public static bool CircleToCircleCollision(Circle a, Circle b)
{
return ((a.Radius + b.Radius) > Vector2.Distance(a.Position, b.Position));
}
/// <summary>
/// Test if a certain circle contains a point
/// </summary>
/// <param name="a">The circle to test</param>
/// <param name="point">The point to test</param>
/// <returns>Returns true if circle contains the point</returns>
public static bool CircleContainsPoint(Circle a, Vector2 point)
{
return a.Radius > Vector2.Distance(new Vector2(a.X, a.Y), point);
}
public bool CircleContainsPoint(Vector2 point)
{
return Circle.CircleContainsPoint(this, point);
}
}
I guess my collision code could be wrong but I dont believe so. I am passing in a point to determine if the click was inside of the circle.
Perhaps my math is wrong, or i dont understand the Futile coordinate system like I should, but I am at a loss.
I have checked to ensure that the texture I use for the planet is square(128x128), and that the actual circle is perfectly centered.
I can get the circle close but not perfect. There is always a registered click where it shouldnt be.
Answer by MattRix · Jan 10, 2013 at 03:18 AM
This section stood out to me immediately as being a bit weird:
set
{
float x = value.x + Radius;
float y = value.y + Radius;
position = new Vector2(x, y);
//position = value;
}
That could explain why your circle is always at the edge of the planet rather than at the center like you want it to be. In general it's not a good idea to do relative movement in a setter like that anyways.
You are right about the bad practice for the setter. I have fixed it some what, but also posted in the reddit post. It appears that everything IS in the correct place, but when I click the touch.position appears to be off. Still unsure what im doing, but my debug.drawline is drawing everything in the right area.
Ok so, I noticed that even in your $$anonymous$$adness game that you created for ludum dare, when you click, the touch is registered "up" and to the "left". This coincides with where I have to click to register a collision detection. Do you know what might be wrong?
Your answer
Follow this Question
Related Questions
Preventing 2D Vehicle Slip 1 Answer
Is there a standardized way for creating a 2D ball physics movement method? 0 Answers
Physics2D Overlapbox not working as aspected 0 Answers
How do I get the character to jump in a 2d game, which is compatible with joysticks and keyboards? 0 Answers
Bumper physics not working, 1 Answer