2 Replies - 1132 Views - Last Post: 14 March 2009 - 04:27 AM Rate Topic: -----

#1 chris_l  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-November 08

Need help with method and abstraction in C#

Posted 13 March 2009 - 02:32 PM

hey im making an Airhocky game in C#, and im really stuck on a method.

I have 3 classes with inheritance (GameObj <- Circle <- Puck) shown below. In my Circle i have a method called "colliding" - it takes two Circles as parameters and returns a boolean whether or not the two circles are intersecting. My problem is i need to feed it one instance of Puck and one instance of Paddle. I havnt made Paddle class yet but two Pucks will do for now. I dont know how to pass two pucks into the method :blink:

i can't say for example...

boolean intersect = colliding(this);



because it takes two parameters...

and it will get more complicated when i have two paddles and one puck :crazy:

any help please??


  abstract class GameObj
	{
		protected Game1 main;
		protected Texture2D texture;
		protected String spriteLoc;
		protected Vector2 position;
		protected Rectangle rect;

		public GameObj(Game1 m, Vector2 pos, Texture2D t)
		{
			main = m;
			position = pos;
			texture = t;
		}

		public virtual Rectangle getRectangle()
		{
			rect = new Rectangle(
				(int)position.X,
				(int)position.Y,
				rect.Width,
				rect.Height);
			return rect;
		}

		public virtual Vector2 getPosition()
		{
			Vector2 midPoint;
			midPoint.X = position.X + texture.Width / 2;
			midPoint.Y = position.Y + texture.Height / 2;
			return midPoint;
		}

		public virtual void loadGraphicsContent(ContentManager content)
		{
			texture = content.Load<Texture2D>(spriteLoc);
		}


		public virtual void draw()
		{
			main.spriteBatch.Draw(texture, position, Color.White);
		}

		public virtual void draw(Rectangle rect)
		{
			main.spriteBatch.Draw(texture, rect, Color.White);
		}
	}


abstract class Circle : GameObj
	{	   
		protected Vector2 origin;
		protected float radius;

		public Circle(Game1 m, Vector2 pos, Texture2D t ) : base(m, pos, t)
		{
			radius = rect.Width;		   
		}

		public Vector2 getOrigin()
		{
			origin = new Vector2(rect.Width / 2, rect.Height / 2) + position;
			return origin;
		}

		public float getRadius()
		{
			radius = rect.Width/2;
			return radius;
		}
		
		public bool colliding(Circle c1, Circle c2)
		{
			float xd = c1.position.X - c2.position.X;  
			float yd = c1.position.Y - c2.position.Y;
  
			float sumRadius = c1.radius + c2.radius;  
			float sqrRadius = sumRadius * sumRadius;  
			
			float distSqr = (xd * xd) + (yd * yd);  
			
			if (distSqr <= sqrRadius)  
			{    
				return true;  
			}  
			
			return false;
		}

	}


 class Puck : Circle
	{
		protected float speed;
		protected Vector2 direction;

		public Puck(Game1 m, Texture2D t, Vector2 pos, float s) : base(m, pos, t) 
		{
			speed = s;
			direction = new Vector2(speed, speed);
			rect = new Rectangle(0,0,50,50);
			
		}

		public void update()
		{
			position += direction;
		}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with method and abstraction in C#

#2 Ashiqa  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 27
  • Joined: 27-February 09

Re: Need help with method and abstraction in C#

Posted 14 March 2009 - 01:26 AM

I don't see the point of using abstract classes this way.

But if you make a base class and inherit from that class you can
make a abstract function

public abstract bool colliding(object objec1 , object object2)



Now in your Circle, Puck or Paddle class you can cast it to the right object.
Was This Post Helpful? 0
  • +
  • -

#3 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: Need help with method and abstraction in C#

Posted 14 March 2009 - 04:27 AM

Hey,

I am having some problem understanding completely your problem.

Here is suggestion.
Get the colliding logic out of those classes, put it into static class.
Make a static class "CollideChecker" (sth similar).

public static bool Collide(GameObj a,GameObj B)
{
}

public static bool Collide(Circle a, Circle B)
{
}

public static bool Collide(Circle a,Puck B)
{
}


... and so on, make overloads for anything more specific.

C# will match the most specific overload if not it will use the less specific one.

This post has been edited by Zammy: 14 March 2009 - 04:28 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1