0
Reply

collision detection xna

Tilo Jung

Tilo Jung

Sep 7 2017 11:31 AM
230
Hello to all,
I made a simple program, like described here:
https://jamessadlier.wordpress.com/tutorials/xna-4-0-collision-detection/
to detect a collision in XNA and to bounce the ball off the block and of the wall.
(I have a "pics" Folder where I have the both pictures: the ball and the block.) But nothing happens. The ball doesn't move at all. What am I doing wrong?

What I have tried:

I tried this code and read related literature
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using Microsoft.Xna.Framework;  
  5. using Microsoft.Xna.Framework.Audio;  
  6. using Microsoft.Xna.Framework.Content;  
  7. using Microsoft.Xna.Framework.GamerServices;  
  8. using Microsoft.Xna.Framework.Graphics;  
  9. using Microsoft.Xna.Framework.Input;  
  10. using Microsoft.Xna.Framework.Media;  
  11.    
  12. namespace game19Addition  
  13. {  
  14.     // This is the main type for your game  
  15.     public class Game1 : Microsoft.Xna.Framework.Game  
  16.     {  
  17.         //this class has a property: GraphicsDevice that represents  the actual graphics device on your PC.  
  18.         //everything you do on screen in your XNA games will run through this object  
  19.         GraphicsDeviceManager graphics;  
  20.    
  21.         //this is the core object to to draw sprites(2d or 3d image inegrated into a large scene)  
  22.         SpriteBatch spriteBatch;  
  23.    
  24.         Texture2D spriteTexture1;  
  25.         Rectangle ballBounds;  
  26.         Vector2 ballPosition;  
  27.         Vector2 ballVelocity;  
  28.    
  29.         Texture2D spriteTexture2;  
  30.         Rectangle blockBounds;  
  31.         Vector2 blockPosition;  
  32.    
  33.         public Game1()  
  34.         {  
  35.             graphics = new GraphicsDeviceManager(this);  
  36.             Content.RootDirectory = "Content";  
  37.         }  
  38.    
  39.         /// <summary>  
  40.         /// Allows the game to perform any initialization it needs to before starting to run.  
  41.         /// This is where it can query for any required services and load any non-graphic  
  42.         /// related content.  Calling base.Initialize will enumerate through any components  
  43.         /// and initialize them as well.  
  44.         /// </summary>  
  45.         protected override void Initialize()  
  46.         {  
  47.             // TODO: Add your initialization logic here  
  48.    
  49.             base.Initialize();  
  50.         }  
  51.    
  52.         /// <summary>  
  53.         /// LoadContent will be called once per game and is the place to load  
  54.         /// all of your content.  
  55.         /// </summary>  
  56.         protected override void LoadContent()  
  57.         {  
  58.             // Create a new SpriteBatch, which can be used to draw textures.  
  59.             spriteBatch = new SpriteBatch(GraphicsDevice);  
  60.    
  61.             // TODO: use this.Content to load your game content here  
  62.    
  63.             ballPosition = new Vector2(this.GraphicsDevice.Viewport.Width / 2,  
  64.                                             this.GraphicsDevice.Viewport.Height * 0.25f);  
  65.             ballVelocity = new Vector2(0, 1);  
  66.             spriteTexture1 = Content.Load<Texture2D>(@"pics/ball");  
  67.             ballBounds = new Rectangle((int)(ballPosition.X - spriteTexture1.Width / 2),  
  68.                                 (int)(ballPosition.Y - spriteTexture1.Height / 2),  
  69.                                 spriteTexture1.Width,  
  70.                                 spriteTexture1.Height);  
  71.    
  72.             blockPosition = new Vector2(this.GraphicsDevice.Viewport.Width / 2,  
  73.                                               this.GraphicsDevice.Viewport.Height * 0.75f);  
  74.    
  75.             spriteTexture2 = Content.Load<Texture2D>(@"pics/block");  
  76.             //create rectangles based off the size of the textures  
  77.             blockBounds = new Rectangle((int)(blockPosition.X - spriteTexture2.Width / 2),  
  78.                                             (int)(blockPosition.Y - spriteTexture2.Height / 2),  
  79.                                             spriteTexture2.Width,  
  80.                                             spriteTexture2.Height);  
  81.         }  
  82.         /// <summary>  
  83.         /// UnloadContent will be called once per game and is the place to unload  
  84.         /// all content.  
  85.         /// </summary>  
  86.         protected override void UnloadContent()  
  87.         {  
  88.             // TODO: Unload any non ContentManager content here  
  89.         }  
  90.    
  91.         /// <summary>  
  92.         /// Allows the game to run logic such as updating the world,  
  93.         /// checking for collisions, gathering input, and playing audio.  
  94.         /// </summary>  
  95.         /// <param name="gameTime">Provides a snapshot of timing values.</param>  
  96.         protected override void Update(GameTime gameTime)  
  97.         {  
  98.             // Allows the game to exit  
  99.             if (Keyboard.GetState().IsKeyDown(Keys.F12))  
  100.                 this.Exit();  
  101.    
  102.             // TODO: Add your update logic here  
  103.             if (ballBounds.Intersects(blockBounds) || !GraphicsDevice.Viewport.Bounds.Contains(ballBounds))  
  104.             {  
  105.                 ballVelocity = -ballVelocity;  
  106.                 ballPosition += ballVelocity;  
  107.             }  
  108.             else  
  109.             {  
  110.                 ballPosition += ballVelocity;  
  111.             }  
  112.    
  113.             //update boundig boxes  
  114.             ballBounds.X = (int)ballPosition.X;  
  115.             blockBounds.Y = (int)ballPosition.Y;  
  116.    
  117.             blockPosition.X = (int)blockPosition.X;  
  118.             blockPosition.Y = (int)blockPosition.Y;  
  119.    
  120.             base.Update(gameTime);  
  121.         }  
  122.    
  123.         /// <summary>  
  124.         /// This is called when the game should draw itself.  
  125.         /// </summary>  
  126.         /// <param name="gameTime">Provides a snapshot of timing values.</param>  
  127.         protected override void Draw(GameTime gameTime)  
  128.         {  
  129.             GraphicsDevice.Clear(Color.CornflowerBlue);  
  130.    
  131.             spriteBatch.Begin();  
  132.             spriteBatch.Draw(spriteTexture1, ballPosition, Color.White);  
  133.             spriteBatch.Draw(spriteTexture2, blockPosition, Color.White);  
  134.             spriteBatch.End();  
  135.    
  136.             base.Draw(gameTime);  
  137.         }  
  138.     }  

 

Upload Source Code  Select only zip and rar file.