1. Forum
    1. Unerledigte Themen
  2. Artikel
  3. Mitglieder
    1. Letzte Aktivitäten
    2. Benutzer online
    3. Team
    4. Mitgliedersuche
  4. Blog
    1. Artikel
  • Anmelden
  • Registrieren
  • Suche
Alles
  • Alles
  • Artikel
  • Forum
  • Blog-Artikel
  • Seiten
  • Erweiterte Suche
  1. StageTwo - Deine Wohlfühloase
  2. Mitglieder
  3. Animal9

Beiträge von Animal9

  • [XNA] Präzise Kollisionsabfrage (2D)

    • Animal9
    • 25. Mai 2012 um 21:59

    Bei mir funktioniert der Code leider nicht. Ich bekomme ab dem ersten { im Update-Teil eine Fehlermeldung. Was ist denn falsch? Habe ich etwas vergessen?


    namespace WindowsGame


    {


    public class Game1 : Microsoft.Xna.Framework.Game


    {



    GraphicsDeviceManager graphics;



    SpriteBatch spriteBatch;



    Texture2D texture;



    Vector2 enemyPos;



    Color[] col1d;



    Color[,] col2d;



    public Game1()



    {



    graphics = new GraphicsDeviceManager(this);



    Content.RootDirectory = "Content";



    }



    protected override
    void Initialize()



    {



    base.Initialize();



    }



    protected override
    void LoadContent()



    {



    spriteBatch = new SpriteBatch(GraphicsDevice);



    texture = Content.Load<Texture2D>("Resetti");



    }



    protected override
    void UnloadContent()



    {



    }



    protected override
    void Update(GameTime
    gameTime)



    {



    Color[,] Tex2colArr(Texture2D
    texture)



    {



    Color[] col1d = new Color[texture.Width
    * texture.Height];



    texture.GetData(col1d);



    Color[,] col2d = new Color[texture.Width,
    texture.Height];



    for (int
    x = 0; x < texture.Width; x++)


    for
    (int y = 0; y < texture.Height; y++)


    col2d[x, y] = col1d[x + y *
    texture.Width];



    return col2d;



    }



    bool CheckCollision(Color[,] col)



    {



    MouseState ms = Mouse.GetState();



    Matrix mat = Matrix.CreateTranslation(enemyPos.X,
    enemyPos.Y, 0) * Matrix.Identity;



    if (ms.X >= enemyPos.X &&
    ms.X <= enemyPos.X + texture.Width &&


    ms.Y >=
    enemyPos.Y && ms.Y <= enemyPos.Y + texture.Height)



    {


    for
    (int x = 0; x < col2d.GetLength(0); x++)


    for
    (int y = 0; y < col2d.GetLength(1); y++)


    {


    Vector2 pos = Vector2.Transform(new Vector2(x, y),
    mat);


    if (pos == new Vector2(ms.X, ms.Y))


    if (col[x, y].A > 0)


    {


    return true;


    }


    else


    {


    return false;


    }


    }



    }



    return false;



    }



    base.Update(gameTime);



    }



    protected override
    void Draw(GameTime gameTime)



    {



    GraphicsDevice.Clear(Color.CornflowerBlue);



    base.Draw(gameTime);



    }


    }


    }

  1. Datenschutzerklärung
  2. Impressum