Jump to content

rectangle collision

seaturtleftw's Photo
Posted Dec 01 2010 10:53 AM
2579 Views

hey everyone, I've written a class that makes a planet float around the map. however i'm having a problem getting the rectangles to collide. I've made the class rectangle a property and i see that the value isnt changing from the default value. Even though i do see my planets move around the screen.

//these are my fields
        public Rectangle astroidRect;

        private int rectangleX = 10;
        private int rectangleY = 10;

//constructor
            astroidRect = new Rectangle(rectangleX,rectangleY,50,50);
//update this line basicly tells the planet where to go when his random number is 0
           if (way == 0)
           {
               astroidRect.Y -= (int)_speed;
           }
//and finally the draw
        public void draw(SpriteBatch spritebatch)
        {           
          spritebatch.Draw(astroidText, astroidRect, Color.White);
        }

hope someone can help me out or push me in the right direction

Tags:
1 Subscribe


1 Reply

0
  seaturtleftw's Photo
Posted Dec 01 2010 03:03 PM

well i've figured out whats wrong apparently my rectangle is never increased in value even though i do see them moving about....will be back with more details

Edit OK more details right here so my rectangles arent moving like i think they are...well their moving around the screen so one can assume they are.
   //in my fields i make a vector2 position to rotate the ship around. 
private Vector2 position; 
private Rectangle _playerRect; 
 
//in my constructor i give position as a parameter 
        public Player(Vector2 playerPosition , ContentManager content) 
        { 
            this._playerText = _idleTexture; 
            this._idleTexture = content.Load<Texture2D>("pictures\\Grounded2"); 
            this._playerTextureFly = content.Load<Texture2D>("pictures\\player");     
            this._playerRect = new Rectangle(0, 0, _playerTextureFly.Width, _playerTextureFly.Height); 
            this.position = playerPosition; 
//trying to get a bounding sphere going but that's something for later 
            this._playerSphere = new BoundingSphere(new Vector3(_playerRect.Height /2, _playerRect.Width /2, 0), 17f); 
        } 
 
//in my update i'm rotating the player around using the position vector 
if (keyboardState.IsKeyDown(Keys.Right)) 
            { 
                this.rotation += 3f; 
            } 
this.position.X += (float)(Math.Cos(this.rotation * Math.PI / 180) * this._speed); 
this.position.Y += (float)(Math.Sin(this.rotation * Math.PI / 180) * this._speed); 
 
//draw 
spritebatch.Draw(this._idleTexture, this.position, this._playerRect, Color.White, (float)((this.rotation + 0) * Math.PI / 180), new Vector2(this._playerTextureFly.Width / 2, this._playerTextureFly.Height / 2), 1.0f, SpriteEffects.None, 1.0f); 

i checked out the value of playerRectangle and it's on a constant position i tried rewritting _playerect like: this._playerRect = new Rectangle((int)position.X, (int)position.Y, _playerTextureFly.Width, _playerTextureFly.Height); but sadly that didnt work out.