0
votes

Im trying to make a collision rectangle work with a list.

So here is the code :

  List<Planet> planets;
Planet planet;
Texture2D levelSelectTexture;
Texture2D star;
Vector2 position;

Rectangle planetRectangle;

public PlanetSelect(EventHandler screenEvent, GraphicsDevice graphics, ContentManager content, SpriteBatch batch)
    : base(screenEvent, graphics, content, batch)
{

    star = content.Load<Texture2D>("ursaeMajorisStar");

    position = new Vector2((Graphics.Viewport.Width - star.Width) / 2, (Graphics.Viewport.Height - star.Height) / 2);

    planets = new List<Planet>();

    Planet planet = new Planet(Content, Batch);
    planet.Load("planet0");
    planet.velocity = 0.04f;
    planet.radius = 80;
    planet.angle = MathHelper.ToRadians(90);
    planets.Add(planet);

    planet = new Planet(Content, Batch);
    planet.Load("planet2");
    planet.velocity = 0.02f;
    planet.radius = 135;
    planet.angle = MathHelper.ToRadians(120);
    planets.Add(planet);

    planet = new Planet(Content, Batch);
    planet.Load("planet3");
    planet.velocity = 0.009f;
    planet.radius = 180;
    planet.angle = MathHelper.ToRadians(160);
    planets.Add(planet);


    **planetRectangle = new Rectangle(planet.position.X, planet.position.Y, planet.image.Width, planet.image.Height);**

    levelSelectTexture = content.Load<Texture2D>("levelSelectMenu");

}

public override void Update(GameTime gameTime)
{

    TouchCollection touchCollection = TouchPanel.GetState();

        foreach (TouchLocation tl in touchCollection)
        {
            if (tl.State==TouchLocationState.Moved)
            {
                if (planetRectangle.Contains((int)tl.Position.X,(int)tl.Position.Y))
                {
                    screenEvent.Invoke(this, new EventArgs());
                }


            }

    foreach (Planet planet in planets)
    {
        planet.angle += planet.velocity;
        float orbitx = (float)(Math.Cos(planet.angle) * planet.radius);
        float orbity = (float)(Math.Sin(planet.angle) * planet.radius);

        float x = (Graphics.Viewport.Width - planet.image.Width) / 2 + orbitx;
        float y = (Graphics.Viewport.Height - planet.image.Height) / 2 + orbity;

        planet.position = new Vector2(x, y);


    }

    base.Update(gameTime);
}

public override void Draw(SpriteBatch spriteBatch)
{


    spriteBatch.Draw(levelSelectTexture, Vector2.Zero, Color.White);
    foreach (Planet planet in planets)
    {
        planet.Draw();
    }

    spriteBatch.Draw(star, position, Color.White);

    base.Draw(spriteBatch);
}

Every time i try to create a collision rectangle and pass in the planet.position values as well as the image width and height it throws back an error and wont let me

so i declare : Rectangle planetRectangle;

and then when i initalise i do planetRectangle = new Rectangle ( planet.position.X, planet.Position.Y, planet.image.Width, planet.image.Height);

it just does not want to pass the values onto the rectangle, how do i do collision with a list of objectS? Should i somehow create it in the base class or maybe in the sprite class?

errors i get are :

cannot convert float to an int ( regarding planet.position.X&Y ) and best overloaded match has arguements. Ive highlighted the line it has a problem with.

Normally these planet objects move around in concentric circles, i want to be able to select them with touch but to even do that i need to create a rectangle first.

2

2 Answers

1
votes

You need to convert planetRectangle to a method. You've written a single statement that is taking the 3rd "planet" object you create and creating a rectangle that is stored once in the variable planetRectangle and never changes.

Try removing that line you have highlighted, and putting it in a method like so:

static Rectangle GetPlanetRectangle(Planet planet)
{
    planetRectangle = new Rectangle(planet.position.X,
            planet.position.Y, planet.image.Width, planet.image.Height);
}

Or better yet, if Planet is your own class, add it as a property to Planet like so:

public Rectangle Bounds
{
    get
    {
        return new Rectangle(this.position.X,
                this.position.Y, this.image.Width, this.image.Height);
    }
}

Then you need to call this method or access this property for each planet, like so:

foreach(TouchLocation tl in touchCollection)
{
    if(tl.State == TouchLocationState.Moved) // Moved? Is that right?
    {
        foreach(Planet planet in planets)
        {
            // Alternately: if(GetPlanetRectangle(planet).Contains(...))
            if(planet.Bounds.Contains((int)tl.Position.X, (int)tl.Position.Y))
            {
                // Do stuff...
            }
        }
    }
}
0
votes

You need to cast the planet values to int to pass them into the Rectangle:

planetRectangle = new Rectangle((int)planet.position.X, (int)planet.position.Y, (int)planet.image.Width, (int)planet.image.Height);