Tile based collision in XNA - c#

I have drawn tiles in my XNA game and loaded my character. My character, however, doesn't move- the map does, which gives it the illusion of movement. Now I am wondering how to actually test against them for collision. I mean, where does the collision code go and how do I make all tiles represent 'one big thing'?

There's a tutorial on pixel based collision detection on XNA Creator's club. You'll need to figure out what objects you want to do collision detection on. I guess you want the character to move across the tiled background, so you don't want to check for collision between your character and the background. Instead you should make any obstacle s sprites and do collision detection on those.

You might have a look at Nick Gravelyn's Tile Engine Tutorials, it goes through the whole process of creating a tile engine. There's a link here to see all the tutorials on YouTube.

You could have a look at the Platformer Starter Kit, it shows how to organize tiles in a map and check for collisions.

Rectangles have a intersects method. If your player is centered and you know the coordinates, loop through the other texture2ds and check for an intersection before scrolling the map.

Related

How do I make dynamic 2D collisions faster in unity?

Currently I'm working on a 2D game with destructible terrain in unity. It works great! Except for one thing.... collision generation... Since the terrain is destructible, I have to generate collisions on the go. I tried and it's awful performance. From 1000 fps from editing the terrain to 1 fps when I enable collisions generation. This is a huge issue and I know it's possible within unity because this guy :
https://forum.unity.com/threads/wip-nimbatus.221798/
created it with collisions as well. I tried contacting him but no response yet! Any of you guys have any ides on what I can do? Thanks!
I found a rather annoying but working solution to the issue. So basically what was wrong was I was setting points as vertices. The guy I messaged told me that at the time when he made his game, the 2D engine wasn't a thing in unity so he was forced to use regular 3D components. I didn't want to do this because it would possibly limit what I can do with 2D packages since I am not using the 2D engine. But in the end I decided that unless I could find a extremely fast edge finding algorithm that supported concave meshes WITH holes, I would need to just use the regular Unity3D MeshCollider component. I'm not sure why I was having an issue with the Polygon collider as I thought it would work the same as the MeshCollider but in 2D. I think the issue was I was setting the path of the Polygon Collider as each vertice. This is wrong because I think I would need to set the edge vertices only. (My mesh does not share vertices so I'm not sure how to find the edges) Of course I could re write it to share vertices but I think I will be find with the 3D components (For now at least)

AI in different-shaped planets - Unity 3D

I am currently working in a Unity 3D project (in version 5.0.1 to be precise) in which the character can walk in different-shaped planets without falling. So it is always attracted by the planets gravity (similar mechanic to the Super Mario Galaxy games).
Now, the problem is when I try to code the AI for the enemies. This AI should be pretty much simple, but because of this thing that I have with the planets shapes, I haven't been able to come up with a solution. The enemy should check where the player is and move ahead to its position. It's a really dumb enemy. But the thing is, that the enemy must be able to track the player and get to its position no matter what planet he is in. So, if the planet is circular, I cannot make the enemy look at the player and then move forward, as I would do in a plane planet.
I have already tried using a NavMesh, but Unity doesn't want to collaborate and ends up making a NavMesh of 1/3 of the planet. I don't understand why. I have tried changing every single parameter of the NavMesh baking but I haven't succeeded.
Another problem that I would like to do is: ¿How would you make a player be able to walk on the surface of any planet, no matter what its shape, in a smooth way while using a Mesh Collider? I am currently achieving this by getting the planet's normal with a raycast, rotating the player so that the Y axis is perpendicular to the normal and applying a force in -Y to simulate gravity. But this brings lots of problems when I implement the Mesh Collider. It results in a jittery effect. But, when I use a primitive collider from Unity, it works fine...
Anyway. What I need the most now is the answer to the AI question. The other one is kinda like a plus.
P.S: It doesn't matter if the solution is in Javascript. I can then translate it to C# :)

Collision detection between my player and the tiles(background). XNA

So, I want to check collision between my player and the tiles. The tile isn't one big object, but the tile is 32*32 pixels size and there are like 11 of them, used as floor so the player will be able to walk on it.
My question is, how am I going to detect it?
Pixel Collision doesn't sound very effective.
If I should use rectangle collision, I'd like to get an explanation how am I going to implement it into my code.
Thanks alot.
I suggest downloading and learning the Platformer Starter Kit, developed by Microsoft.
Download: Starter Kit Download
MSDN discussion Starter Kit Discussion
The simplest explanation for their solution is that tiles are kept in a 2D array to represent the world. When the player's Update() function is called a HandleCollisions() function is called that loops through a subset of the tile array to look for possible collisions. For every possible collision with the player the depth of intersection with the player bounds and the tile, the players position is adjusted to bring it out of the tile.

Goblin XNA -- Creating a labyrinth style game

Anyone know how to use Goblin XNA to implement ball control in the same way as one might find in the board game labyrinth?
There don't appear to be any tutorials or information at all regarding how to do this, despite there being a demo video displaying just such a thing.
I've setup the environment and gravity and added the ground and a sphere. I use WorldTransformation.Decompose to extract the current orientation of the board. I know the next step will be either ApplyLinearVelocity or AddForce to the sphere based on the current board orientation, but I don't know how to constantly apply these methods to the ball so that the ball is moving in response to the movement of the ball. Adding code to the Draw or Update methods only executes the code a single time. Anyone familiar with Goblin XNA at all and able to help?
As far as I can see in Goblin XNA the Update and Draw methods are called the same way as standard XNA games. Can you give more specific information? source code perhaps?

Silverlight Collision Detection when controls are in different canvasses/layers

For our game we have to create collision detection.
The problem is that the collided objects are in different canvasses/layers, which makes collision detection by pointlocation inpossible.
Does anyone have an idea how to solve this?
It's hard to give a great answer without some more information, but if all your layers are the same size then you can just roll your own collision detection. All you need to know is the locations and sizes of two things to be collision detected. Then you just test to see if one rectangle intersects with the other rectangle.
There is also a function that might be useful to use called TranslatePoint. This translates from one UIElements coordinates to another. So if you had a ball bouncing around in a smaller area of the screen with it's own local coordinate system, you could get the ball's coordinates relative to the entire screen with this function.
Can I suggest you try using the Farseer physics engine just to save yourself some pain?
http://farseerphysics.codeplex.com/
It's very good and is in use in some WP7 games already.
http://www.farseergames.com/
There's also some Blend behaviors and helpers to make using it even easier:
http://physicshelper.codeplex.com/

Categories