Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent player object from clipping through walls in a 2d environment
    primarykey
    data
    text
    <p>The player box is continuing through walls in an undesired fashion, I have tried making it so that the player moves in 0.1f(u) increments at a time, but this severely drops the performance of the game. Is there any way I can detect if the player is hitting a wall, what side they hit it on and how can I prevent them from clipping into the wall?</p> <p>Here is the code that I am running (this is minimalistic of course)</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; namespace Platformer { public class Player { double terminalVelocity; //AnimatedTexture texture; public Texture2D texture; public Vector2 Position, Velocity; public Rectangle boundingBox; public Player(Texture2D tex, Vector2 pos, Vector2 vel, Rectangle bb) { texture = tex; Position = pos; Velocity = vel; boundingBox = bb; terminalVelocity = Math.Sqrt((2*bb.Width*bb.Height*Game1.gravity)/-9.8*2); } public void updateBoundingBoxes() { boundingBox.X = (int)Position.X; boundingBox.Y = (int)Position.Y; } public void onUpdate() { updateBoundingBoxes(); Position.X += Velocity.X; Position.Y += Velocity.Y; //Velocity = Vector2.Zero; Velocity.Y += Game1.gravity / 60; Velocity.X /= 1.2f; } public void Draw(SpriteBatch sb) { updateBoundingBoxes(); sb.Begin(); sb.Draw(texture,boundingBox,GameLighting.currentColour()); sb.End(); } } public enum GameLightingState { Red, Dark, Orange, Blue, White } public class Platform : Object { Texture2D text; public Rectangle rect; public Platform(Texture2D t, Vector2 p, int sizeX, int sizeY) { text = t; rect = new Rectangle((int)p.X, (int)p.Y, sizeX, sizeY); } public void onPlayerCollision(Player p) { p.Velocity.X = -p.Velocity.X / 2; p.Velocity.Y = -p.Velocity.Y / 2; } public void Draw(SpriteBatch sb) { sb.Begin(); sb.Draw(text, rect, GameLighting.currentColour()); sb.End(); } public void onUpdate() { } } public class GameLighting { public static Color currentColour() { return eToColour(Game1.currentLightingState); } public static Color eToColour(GameLightingState gls) { switch (gls) { case(GameLightingState.Red): return Color.Red; case (GameLightingState.Blue): return Color.Blue; case (GameLightingState.Orange): return Color.Orange; case (GameLightingState.Dark): return Color.DarkGray; case (GameLightingState.White): return Color.White; } return Color.White; } } /// &lt;summary&gt; /// This is the main type for your game /// &lt;/summary&gt; public class Game1 : Microsoft.Xna.Framework.Game { public static float gravity = 9.80665f; public static GameLightingState currentLightingState; GraphicsDeviceManager graphics; SpriteBatch spriteBatch; List&lt;Platform&gt; platforms; List&lt;Player&gt; players; int controlledPlayerIndex = 0; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } /// &lt;summary&gt; /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// &lt;/summary&gt; protected override void Initialize() { // TODO: Add your initialization logic here currentLightingState = GameLightingState.White; platforms = new List&lt;Platform&gt;(); players = new List&lt;Player&gt;(); players.Add(new Player(this.Content.Load&lt;Texture2D&gt;("Images/dirt"), new Vector2(300,0), new Vector2(0,0), new Rectangle(300,0,20,20))); platforms.Add(new Platform(this.Content.Load&lt;Texture2D&gt;("Images/dirt"),new Vector2(300,450),200,20)); platforms.Add(new Platform(this.Content.Load&lt;Texture2D&gt;("Images/dirt"), new Vector2(20,20), 20, 200)); base.Initialize(); } /// &lt;summary&gt; /// LoadContent will be called once per game and is the place to load /// all of your content. /// &lt;/summary&gt; protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // TODO: use this.Content to load your game content here } /// &lt;summary&gt; /// UnloadContent will be called once per game and is the place to unload /// all content. /// &lt;/summary&gt; protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// &lt;summary&gt; /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// &lt;/summary&gt; /// &lt;param name="gameTime"&gt;Provides a snapshot of timing values.&lt;/param&gt; protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); foreach (Player p in players) { Boolean intersects = false; Rectangle tempRectangle = new Rectangle((int)(p.Position.X + p.Velocity.X),(int) (p.Position.Y + p.Velocity.Y), p.boundingBox.Width, p.boundingBox.Height); foreach (Platform pl in platforms) { intersects = intersects || tempRectangle.Intersects(pl.rect); } if (!intersects) { p.onUpdate(); } } if (Keyboard.GetState().IsKeyDown(Keys.Space)) { players[controlledPlayerIndex].Velocity.Y -= 0.75f; } if (Keyboard.GetState().IsKeyDown(Keys.A)) { players[controlledPlayerIndex].Velocity.X -= 0.75f; } if (Keyboard.GetState().IsKeyDown(Keys.D)) { players[controlledPlayerIndex].Velocity.X += 0.75f; } // TODO: Add your update logic here base.Update(gameTime); } /// &lt;summary&gt; /// This is called when the game should draw itself. /// &lt;/summary&gt; /// &lt;param name="gameTime"&gt;Provides a snapshot of timing values.&lt;/param&gt; protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here foreach (Platform p in platforms) { p.Draw(spriteBatch); } foreach (Player p in players) { p.Draw(spriteBatch); } base.Draw(gameTime); } } } </code></pre> <p>*Updated Source Code based on first comments</p> <p>One note about this code, you need to run it in XNA and use an icon called dirt.png in a folder called Images, it doesn't matter what the picture looks like, you just need it to fully understand what is happening</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload