greencrayon

Members
  • Content count

    29
  • Joined

  • Last visited

Community Reputation

138 Neutral

About greencrayon

  • Rank
    Member
  1. Seeking more help with tile collision in XNA C#

    Wow I done everything wrong. Sucks to be me :). Thanks for all the pointers Captain P, you have given me loads to work with. I will be back again with another mess tomorrow I am sure.
  2. Seeking more help with tile collision in XNA C#

    Okieodkie. I switched form using tile-based collision to using bounding boxes as Captain P advised. Some things seem to be working a lot better now but I am still very stuck. When I run the following code the collisions work fine, the player stops if it hits an obstacle in the X or Y direction. But. If the player is moving down and to the left or down and to the right the player is able to move through an obstacle. Looks kinda like Mario going down a tube. Can anyone see what I am doing wrong here? Than you for your time. public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content; SpriteBatch spriteBatch; Texture2D PlayerTexture; Block[] blocks; KeyboardState keyboardState; Vector2 playerLocation = new Vector2(0, 0); Vector2 playerVelocity = new Vector2(100, 100); int numberOfBlocks = 12; enum Directions { up, down, left, right, none } Directions currentDirection = Directions.none; public Game1() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } /// <summary> /// 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. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here spriteBatch = new SpriteBatch(graphics.GraphicsDevice); blocks = new Block[numberOfBlocks]; int blockDrawPositionX = 200; int blockDrawPositionY = 300; for (int i = 0; i < numberOfBlocks; i++) { blocks[i] = new Block(new Vector2(blockDrawPositionX, blockDrawPositionY)); blockDrawPositionX = blockDrawPositionX + 32; } base.Initialize(); } /// <summary> /// Load your graphics content. If loadAllContent is true, you should /// load content from both ResourceManagementMode pools. Otherwise, just /// load ResourceManagementMode.Manual content. /// </summary> /// <param name="loadAllContent">Which type of content to load.</param> protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { // TODO: Load any ResourceManagementMode.Automatic content Block.BlockTexture = content.Load<Texture2D>("blackTile"); PlayerTexture = content.Load<Texture2D>("redTIle"); } // TODO: Load any ResourceManagementMode.Manual content } /// <summary> /// Unload your graphics content. If unloadAllContent is true, you should /// unload content from both ResourceManagementMode pools. Otherwise, just /// unload ResourceManagementMode.Manual content. Manual content will get /// Disposed by the GraphicsDevice during a Reset. /// </summary> /// <param name="unloadAllContent">Which type of content to unload.</param> protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent) { // TODO: Unload any ResourceManagementMode.Automatic content content.Unload(); } // TODO: Unload any ResourceManagementMode.Manual content } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here keyboardState = Keyboard.GetState(); // Set the collidable corners of the player sprite Rectangle PlayerRect = new Rectangle((int)playerLocation.X, (int)playerLocation.Y, PlayerTexture.Width, PlayerTexture.Height); //Check for collisions for (int i = 0; i < numberOfBlocks; i++) { // Set the collidable crners of the bock sprite. Rectangle BlockRect = new Rectangle((int)blocks[i].blockLocation.X, (int)blocks[i].blockLocation.Y, Block.BlockTexture.Width, Block.BlockTexture.Height); if (PlayerRect.Intersects(BlockRect)) { if (currentDirection == Directions.up) { playerLocation.Y = blocks[i].blockLocation.Y + Block.BlockTexture.Height; } if (currentDirection == Directions.down) { playerLocation.Y = blocks[i].blockLocation.Y - PlayerTexture.Height; } if (currentDirection == Directions.left) { playerLocation.X = blocks[i].blockLocation.X + Block.BlockTexture.Width; } if (currentDirection == Directions.right) { playerLocation.X = blocks[i].blockLocation.X - PlayerTexture.Width; } } } // Move the player if (keyboardState.IsKeyDown(Keys.Up)) { playerLocation.Y -= playerVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; currentDirection = Directions.up; } if (keyboardState.IsKeyDown(Keys.Down)) { playerLocation.Y += playerVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; currentDirection = Directions.down; } if (keyboardState.IsKeyDown(Keys.Left)) { playerLocation.X -= playerVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds; currentDirection = Directions.left; } if (keyboardState.IsKeyDown(Keys.Right)) { playerLocation.X += playerVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds; currentDirection = Directions.right; } base.Update(gameTime); } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); spriteBatch.Draw(PlayerTexture, playerLocation, Color.White); for (int i = 0; i < numberOfBlocks; i++) { blocks[i].Draw(spriteBatch); } spriteBatch.End(); base.Draw(gameTime); } }
  3. Seeking more help with tile collision in XNA C#

    Thanks for the replies guys. I tried your suggestion Butterman but the program behaves in the same way. I think my problem is that when the character sprite stops moving upon hitting a 1 tile, it stops with its corners over the tile. This makes it impossible to move left or right after hitting a tile above it. But if I try to reset the location of the player sprite after a collision like this: if (keyboardState.IsKeyDown(Keys.Up)) { if (Walkable[PlayerTileY, PlayerTileX] == 0 && Walkable[PlayerTileY, PlayerTileX + 1] == 0) { playerLocation.Y = playerLocation.Y - 2; } else playerLocation.Y = (PlayerTileY + 1) * tileHeight; } The player sprite is not always set outside the unwalkable tile. I have no idea why this is the case.
  4. Seeking more help with tile collision in XNA C#

    Hi again folks. I am still having problems getting to grips with tile based collisions. I am using C# and XNA. Below is my complete Game1 class for the program. I managed to get the walkable/non-walkable areas working just fine. But I want to make it possible for my character to slide around the edges of the map and around obstacles. Could anyone give me some pointers on how to change my code to make sliding possible? Any help greatly appreciated. namespace OMGTILESAGAIN { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content; SpriteBatch spriteBatch; Texture2D[] tiles = new Texture2D[2]; Texture2D playerTexture; Vector2 playerLocation = new Vector2(32, 32); Vector2 playerVelocity = new Vector2(50, 50); KeyboardState keyboardState; const int mapHeight = 8; const int mapWidth = 8; int tileWidth = 32; int tileHeight = 32; // tiles the player is touching int PlayerTileX; int PlayerTileY; // map to be drawn int[,] Map = new int[mapHeight, mapWidth] { { 1, 1, 1, 1, 1, 1, 1, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 1, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 1, 1, 1, 1, 1, 1, 1}, }; // Walkable/ non-walkable areas int[,] Walkable = new int[mapHeight, mapWidth] { { 1, 1, 1, 1, 1, 1, 1, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 1, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 1, 1, 1, 1, 1, 1, 1}, }; public Game1() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } /// <summary> /// 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. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here spriteBatch = new SpriteBatch(graphics.GraphicsDevice); base.Initialize(); } /// <summary> /// Load your graphics content. If loadAllContent is true, you should /// load content from both ResourceManagementMode pools. Otherwise, just /// load ResourceManagementMode.Manual content. /// </summary> /// <param name="loadAllContent">Which type of content to load.</param> protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { // TODO: Load any ResourceManagementMode.Automatic content tiles[0] = content.Load<Texture2D>("whiteTile"); tiles[1] = content.Load<Texture2D>("blackTile"); playerTexture = content.Load<Texture2D>("redTile"); } // TODO: Load any ResourceManagementMode.Manual content } /// <summary> /// Unload your graphics content. If unloadAllContent is true, you should /// unload content from both ResourceManagementMode pools. Otherwise, just /// unload ResourceManagementMode.Manual content. Manual content will get /// Disposed by the GraphicsDevice during a Reset. /// </summary> /// <param name="unloadAllContent">Which type of content to unload.</param> protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent) { // TODO: Unload any ResourceManagementMode.Automatic content content.Unload(); } // TODO: Unload any ResourceManagementMode.Manual content } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here //Find the tile that the player is on, on the X axis PlayerTileX = (int)playerLocation.X / tileWidth; //Find the tile that the player is on, on the Y axis PlayerTileY = (int)playerLocation.Y / tileHeight; keyboardState = Keyboard.GetState(); //move the player if (keyboardState.IsKeyDown(Keys.Up)) { if (Walkable[PlayerTileY, PlayerTileX] == 0 && Map[PlayerTileY, PlayerTileX + 1] == 0) { playerLocation.Y -= playerVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; } } if (keyboardState.IsKeyDown(Keys.Down)) { if (Walkable[PlayerTileY + 1, PlayerTileX] == 0 && Map[PlayerTileY + 1, PlayerTileX + 1] == 0) { playerLocation.Y += playerVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; } } if (keyboardState.IsKeyDown(Keys.Left)) { if (Walkable[PlayerTileY, PlayerTileX] == 0 && Map[PlayerTileY + 1, PlayerTileX] == 0) { playerLocation.X -= playerVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds; } } if (keyboardState.IsKeyDown(Keys.Right)) { if (Walkable[PlayerTileY, PlayerTileX + 1] == 0 && Map[PlayerTileY + 1, PlayerTileX + 1] == 0) { playerLocation.X += playerVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds; } } base.Update(gameTime); } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); // Draw the map for (int y = 0; y < mapHeight; y++) { for (int x = 0; x < mapWidth; x++) { spriteBatch.Draw(tiles[Map[y, x]], new Vector2(x * tileWidth, y * tileHeight), Color.White); } } // Draw the player. spriteBatch.Draw(playerTexture, playerLocation, Color.White); spriteBatch.End(); base.Draw(gameTime); } } }
  5. Hi folks. I am still trying to get my head around tile-based collision detection. This is what I have at the moment. The collisions are detected ok but some wierd stuff happens. I dont think the player's position is being reset properly after a collision has taken place. This makes it impossible for the plyer to (scooch?) around objects as the player has to move away from the obstacle in order to move on. Also sometimes the player zooms across the screen and breaks the array boundary. I hope someone can help me. p.s forgive me if i got the source tags wrong :) This is the array for the walkable / non-walkable tiles. int[,] iMapWalkable = new int[mapHeight, mapWidth] { { 1, 1, 1, 1, 1, 1, 1, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 1, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 1, 1, 1, 1, 1, 1, 1}, }; And this is my code for moving the player and detecting collisions. //Find the tile that the player is on, on the X axis PlayerTileX = (int)playerLoction.X / tileWidth; //Find the tile that the player is on, on the Y axis PlayerTileY = (int)playerLoction.Y / tileHeight; if (keyboardState.IsKeyDown(Keys.Up)) { if (Map[PlayerTileY, PlayerTileX] == 0 && Map[PlayerTileY, PlayerTileX + 1] == 0) { playerLoction.Y -= playerVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; } else { playerLoction.Y = (PlayerTileY + 1) * tileHeight; } } if (keyboardState.IsKeyDown(Keys.Down)) { if (Map[PlayerTileY + 1, PlayerTileX] == 0 && Map[PlayerTileY + 1, PlayerTileX + 1] == 0) { playerLoction.Y += playerVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; } else { playerLoction.Y = PlayerTileY * tileHeight; } } if (keyboardState.IsKeyDown(Keys.Left)) { if (Map[PlayerTileY, PlayerTileX] == 0 && Map[PlayerTileY + 1, PlayerTileX] == 0) { playerLoction.X -= playerVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds; } else { playerLoction.X = (PlayerTileX + 1) * tileWidth; } } if (keyboardState.IsKeyDown(Keys.Right)) { if (Map[PlayerTileY, PlayerTileX + 1] == 0 && Map[PlayerTileY + 1, PlayerTileX + 1] == 0) { playerLoction.X += playerVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds; } else { playerLoction.X = (PlayerTileX) * tileWidth; } }
  6. 2D tiles and XNA

    Yes very simple, doh. Probably why I could not find any tutorials on it. For the same reason you dont see many online tutorials on how to inhale. Thanks for the help, that is exactly what I wanted.
  7. 2D tiles and XNA

    Hi folks. I am using C# and XNA. I have seen that a lot of 2d games use some sort of tile-based collision in platform/maze/rpg type games, and have been trying to work out how to do this but all of the examples/tutorials I have managed to find seem to go a bit deeper than I am able to understand. They often include scrolling and movement physics and all kinds of other complicated stuff and I can’t weed out the bits I need. What I want is just the collision detection and response part. I can use a 2d vector of ints representing tiles to draw my game screen, and I can put a moving character sprite onto the screen. But I cannot make it so that the character can only move in the areas I want it to. I read something about making tiles walkable/non-walkable. How would I do this? And more importantly, how do I know my character’s position in regards to the type of tile it is standing on? I would be great if anyone can give me a very simple example or point me in the direction of a tutorial that concentrates solely on the collision detection aspect of a tile-based game.
  8. Getting around an obstacle

    Hi folks. Me and my character sprites have met with a bit of a problem. I am using C# and XNA. I will try my best to be as clear as possible here, but it is harder than I thought to explain what it is I don’t understand about something I don’t understand. So, here goes. How do I deal with an obstacle on a 2D game screen? I understand how to restrict a sprites movement to stay within a pre-defined area but I cannot work out how to handle the same kind of movement around an obstacle. If my sprite hits the edge of a screen boundary I stop its movement in that direction. But I can’t get any permutation of this technique to work if my sprite meets an impassable block in the centre of the screen. The only way I know how to detect a collision between two sprites requires me putting all of the sprites’ dimensions into an if statement, but this seems to cause problems with handling the movement of the sprite when a collision is taking place. I am sure there is a whole new technique I need to learn about for this kind of situation. I hope someone can tell me what it is I need to learn. It is kinda rough not being able to do this as I can’t make Pac-Man… and I really love that game.
  9. Hi., folks. I have been looking at some particle systems on the web and would like to incorporate one into my game. But while everything I have seen has looked awesome the code looks way too advanced for me to be able to understand. I don’t really want to incorporate something into one of my game unless I understand how it works. I was wondering if anyone could explain to me what a particle effect is at its most basic. This way I can try building my own or at least get an insight into the ones I have seen available for use on the web.
  10. Seeking math help

    Ahh trigonometry that is one of those words that hypnotist guy used to say at school. Thanks for the pointer that is just what i need for my Googling. Google my be great but no matter how many time you type in "help me I am dumb" it never seems to give you the right answer. Thanks again for the help.
  11. Seeking math help

    Hi folks. I been game programming for some 4 months now and I have a healthy collection of games under my belt, but I have hit a bit of a wall. My math knowledge is very weak indeed. All the games I have made so far have contained math as basic as I can get my head around and I feel that in order for me to progress I need overcome my fear. Could anyone point me toward a good book/tutorial that is not afraid of speaking down to beginners? Most online documentation I have found so far has just reinforced what my parents always used to tell me about paying attention at school. All my games so far are in 2 dimensions and I have no plans for upgrading for some time yet. Specifically I am looking for information on how angles relate to the x, y coordinates of my game screen. So far I have only had spites moving up-down-left-right. I am lost when it comes moving a sprite at a predefined angle across the screen (entering random x,y coordinates until it looks right is becoming rather time-consuming). I hope someone can point me in the right direction.
  12. Pong Paddle Physics Problem

    Hi folks. I am looking for some help with bounce physics for my Pong clone using XNA and C#. The way my game works at the moment is when the ball hits the paddle it is deflected at a random speed. This works just fine but I would like to have the ball deflected at a speed based on the speed the paddle was moving at when it collided with the ball. How would I approach this problem? My program takes the position of the ball and the paddle for each frame, so in the frame in which the collision takes place the computer sees both sprites as stationary… Doesn’t it? I hope this is not too vague. I am not looking for specific code help, more of a pointer in how to approach the problem. Thankyou for your time.
  13. An experiment in making multiplayer RPG's

    Hi all. I am assuming this project will be aimed at complete beginners as it is in the For Beginners forum. So here are my wishes as a complete beginner. First thing, the most important at all… it must be completable, by both the students and those running the course. I would hate to be overwhelmed by information right from the start and lose motivation for the rest of the course. And I would like the organisers to have a clear goal for the project; it would be disappointing to follow a course when the tutors have lost interest. I would like the MORPG to be in 2d. I understand that 3D graphics require some specialised math. Many of the people who start this course will have only coded temperature converters. And I don’t think a text MUD would tempt as many people into following the course as a graphical game would (though I secretly would love to make a text MUD). I hope to see the tutorial in C++. Most new players who come to this forum seem to be pointed toward C++ as a language to learn to play games. C++ is the language that even non-programmers have heard of. I think it would be bad for the health of the project if you were to use a less familiar language. If you are not going to be using a specific book to accompany the tutorial a detailed reading list for every module would be great. Thankyou for thinking of us beginners, it is great to know that we have a place on GD.net. I hope the project is as much fun for the organisers as I am sure it will be for the students.
  14. Seeking help with random numbers

    Thankyou for the responses guys, everything works just splendidly now.
  15. Seeking help with random numbers

    Hi everyone. My math is way rusty, I hope someone can help me out. I am using rand() in c++ to generate random numbers for a game. I understand how to get a number between 1 and 100. But I am stuck when it comes to generating a number between say 20 and 40, or between the numbers stored in two variables. It would also be great if anyone could point me toward a math primer aimed at old farts who have forgotten everything they learned at school.