Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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) Offset 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.

Also I tried to tag this post with "Futile" but it would not let me due to reputation.

share|improve this question
    
Also, That image I added is just an example, I can get the circle to move around the planet and can get it close but it never lines up the way it should. –  Echofiend Jan 9 '13 at 20:35

2 Answers 2

For some reason you are deliberately offsetting the collision circle’s centre:

       float x = value.x + Radius;
       float y = value.y + Radius;
share|improve this answer
    
I have tried it without the offset, however that places my circle at the top-left of the sprite. However, I will give it a try once I get home in about 2 hours. –  Echofiend Jan 9 '13 at 23:25

Ok so I dont really program in c#. However, I've had this problem once before and realised that I had added the radius of the circle to the x and y position which offsets the collision circle from the circle sprite.

 public void DrawRadius()
{
    Debug.DrawLine(new Vector3(this.X, this.Y, 0), new Vector3(this.X + Radius, this.Y + radius,0));
}

In your DrawRadius() method you "this.X + Radius"...

If I am not mistaken, this should be "this.X - Radius"

Like this:

 public void DrawRadius()
{
    Debug.DrawLine(new Vector3(this.X, this.Y, 0), new Vector3(this.X - Radius, this.Y - radius,0));
}

Make sure you apply the "- Radius".

Be aware, I have little to no experience in C#. However, I'm just trying to help :)

Give that a go.

share|improve this answer
    
I will give this a try when i get home as well, I have tried a bunch of combinations of subtracting and adding to the x,y. but maybe i messed it up once. –  Echofiend Jan 9 '13 at 23:25
    
Is C# the same as java in regards to x and y co-ordinate origins? Java's x and y co-oridinates start at the top left corner... If this is the case then "+ Radius" has no purpose at all... and to centre the x and y co-oridnates in the middle of the circle you must "- Radius" –  Savlon Jan 9 '13 at 23:32
    
Btw- You do realise you have 2 different "Radius" and "radius" and you call them BOTH in the drawRadius() method? Is it supposed to be like that? –  Savlon Jan 9 '13 at 23:36
    
No, its not supposed to be like that was a typo. but they both return the same value none the less. Radius is a property that returns radius –  Echofiend Jan 9 '13 at 23:45
    
Unity is a bit different with its coordinate system, and futile is supposed to change it over to a flash/cocos system(which i dont really know to be honest.) I will try the -Radius again when I get home in about 2 hours. and see if i forgot to check that one before. –  Echofiend Jan 9 '13 at 23:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.