Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by echofiend · Jan 09, 2013 at 08:45 PM · 2d-physics

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) alt text

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.

offsetcircle.jpg (8.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image echofiend · Jan 10, 2013 at 05:48 PM 0
Share

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.

avatar image echofiend · Jan 11, 2013 at 04:30 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges