How to add colliders to complex maze - c#

I downloaded a maze sprite for expiremental purpose to use in my game and I'm trying to add colliders to the wall. But since the maze is complex, it is quite a lot of work to add a Box collider 2D to each wall.
I tried using the Polygon collider 2D and it was some sort of inaccurate mesh looking collider.Is there any better way to add colliders to a maze or is it possible to do it programmatically adding colliders by somehow detecting the structure of the maze?
Here is something similar to the maze I'm using:
Add comment

There are a few answers to this question, depending on your approach.
1. Sprite-based Approach (where you have an image of a maze)
Make sure that the sprite is transparent, and only opaque on the wall areas (a PNG image can have transparency). After you do this, then you should be able to attach a polygon collider to automatically create a collider. If the image is too big/complex like you say, then you might want to split it up into several different images (4 quadrants for example), and then arrange them and attach a polygon collider to each object. In general, the simpler a collider is, the more accurate and efficient it is.
The downside to the above approach is that you are having to do a lot of manual work. If you knew that you had a lot of hand-drawn mazes that you would need to build colliders for like this, then it might be worth automating the process described above with a script, but that could get complicated fast unless you know what you're doing. Essentially, the automation script could recursively split the sprite into quadrants, create corresponding GameObjects, and add PolygonColliders to each one.
Manually splitting the image in a photo-editing program or making an algorithm to generate the maze and colliders might be faster for you than automation, depending on how much you want to get into the code.
2. Algorithm-based Approach
Luckily, there are a lot of maze programming tutorials online. Most are for 3D mazes, but the logic is the same to make a 2D maze. If you're interested in this option, then I found tutorials on the topic here, and here. In order to add collision after the maze is generated in each of these tutorials you can add a BoxCollider2D to each side of each cell which has a wall (or if using a prefab, add a BoxCollider2D to the prefab).

Related

Unity - Detect collision with a specific tile, depending on physics shape

I have my game set up to detect collisions with a TilemapCollider2D. All of my tiles have their own custom physics shapes (half cells, triangles, etc.), and so far this part works.
Now, I want to have the ability to not only detect a collision, but also detect which tile that collision was with. Most solutions I've been able to find involve finding the cell coordinates of the collision, through tileMap.WorldToCell(worldPos), which would work great with square tiles. However, since my physics shapes are VERY irregular (sometimes much larger than a single cell), I need something that can detect based on the physics shape itself which tile was hit.
If this isn't possible with the TilemapCollider2D then I may have to find a solution using multiple colliders. Any idea of the best way to accomplish what I'm looking for? Thanks in advance!

Unity3d - Evaluate enclosed rooms by walls components

I am working on Sims-like building system.
I am at the point where I can create walls at runtime, but my problem is that I need to know when a list of adjacent walls create a well-enclosed room.
My code so far is pretty messy, so apologies if I don't provide any.
The setup is pretty simple, it is 3D environment where each wall is just a cube (see image below).
The game should allow players to build their own rooms made by walls prefabs (again, they are just cubes stiled as walls).
Any idea how to achieve this?

How to Combine Vertices and edges into one In Unity

I'm new to Unity and I'm making a car racing Game. Now, I'm stuck at some point. I was looking for some solution of my problem, but couldn't succeed.
My problem is:
When I run my game on my phone, it sticks badly because whenever there are several buildings in front of the car camera, like one building behind another building, it lags. Reason for this is there are so many vertices and edges at that time, So the Car Camera is unable to capture all that stuff at same time.
How do I preload the 2nd Scene while loading 1st Scene?
I am using Unity free version.
In graphics programming, there is a common routine to simply don't draw objects that aren't in the field of view. I'm sure Unity can handle this. Check link: Unity description on this topic
I'm not hugely knowledgeable about Unity, but as a 3D modeller there's a bunch of things you can do to improve performance:
Create a simplified version of your buildings with fewer polygons for use when buildings are a long way away. A skyscraper, for example, can be as simple as a textured box.
If you've done that already, reduce the distance at which the simpler imposters are substituted for the complex versions.
Reduce the number of polygons by other means. A good example is if you've got a window ledge sticking out of the side of a building, don't try and make it an extension of the body. Instead, make it a separate box, delete the facet that won't be seen, and move it to intersect with the rest of the building.
Another good trick is to use bump maps or normal maps to approximate smaller features, rather than trying to model everything.
Opaqueness. Try not to have transparent windows in your buildings. It's computationally cheaper to make them just reflect the skybox or a suitably blurred reflection imposter. Also make sure that the material's shader is in Opaque mode, if it supports this.
You might also benefit a little from checking the 'Static' box on the game object, assuming that buildings aren't able to be moved (i.e. by smashing through them in a bulldozer).
Collision detection can also be a big drain. Be sure to use the simplest possible detection mesh you can - either a box, cylinder, sphere or a combination.

What is best way to create scrolling WORLD?

In this game im trying to create, players are going to be able to go in all directions
I added one single image(1024x768 2d texture) as background, or terrain.
Now, when player moves around I want to display some stuff.
For example, lets say a lamp, when player moves enough, he will see lamp. if he goes back, lamp will disappear because it wont be anymore in screen
If Im unclear, think about mario. when you go further, coin-boxes will appear, if you go back they will disappear. but background will always stay same
I thought if I spawn ALL my sprites at screen, but in positions like 1599, 1422 it will be invisible because screen is only 1024x768, and when player moves, I will set place of that sprite to 1599-1,1422-1 and so. Is it a good way to do this ?
Are there better ways?
There are two ways you can achieve this result.
Keep player and camera stationary, move everything else.
Keep everything stationary except the player and the camera.
It sounds like you are trying to implement the first option. This is a fine solution, but it can become complicated quickly as the number of items grows. If you use a tile system, this can become much easier to manage. I recommend you look into using a tile engine of some sort. There are a lot of great tile map editors as well.
Some resources for using Tiles:
Tiled -- Nice Map Editor
TiledLib -- XNA Library for using Tiled Maps
What you're describing there is a Viewport, which describes a portion of the 'world' that is currently visible.
You need to define the contents of your 'world' somehow. This can be done with a data structure such as a scene graph, but for the simple 2D environment you're describing, you could probably store objects in an array. You would need to bind your direction keys to change the coordinates of the viewport (and your character if you want them to stay centered).
It's a good idea to only draw objects that are currently visible. Without knowing which languages or packages you are using it's difficult to comment on that.
I would look into Parallax scrolling. Here is an example of it in action.
If this is what you require, then here is a tutorial with source code.
XNA Parallax Scrolling
After you are finished with basic scrolling, try to implement some frustum culling. That is only draw objects which are actually visible on the screen and avoid unnecessary drawing of stuff that cannot be seen anyway.
I would prefer solution number 2 (move player and camera) - It would be easier for me, but maybe its just personal preference.

XNA 2d arcade game sprite follow

I am going to make a game like XNA example game "Platformer1" which comes with the XNA. But I need longer levels which doesn't fit in the screen (like Super Mario levels). How can I manage this kind of level? Do I need to use a 2d camera that follows the sprite? If I do this way how can I load the level? I am a bit confused and I am not sure if I could explain my problem clearly. Hope someone can help?
The tutorial based on Platformer Starter Kit in MSDN has a step Adding a Scrolling Level which guides you through creation of longer levels. The tutorial is very detailed, I highly recommend it.
I couldn't find the tutorial in the section for XNA Game Studio 4.0, but differences should be minimal. According to the comment at the bottom of the page, all you need to change is replace
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None, cameraTransform);
with
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, cameraTransform);
in the tutorial code.
If you want to create a side scrolling game, then I would look into parallax scrolling. A quick google/bing will help you find lots of tutorials. Also, another useful tip is to search YouTube for XNA videos has we a lot of posters share their source code .
Here is a link to Microsofts Parallax Scrolling.
Sounds like you have a few problems ahead of you.
But I need longer levels which doest'n fit in the screen(like super mario levels). How can I manage this kind of levels.
There are several ways to do this, but a fairly easy way would be to have a 2d array (or sparse array, depending on how large your levels are) of a class named Tile that stores info about the tile image, animation, ...whatever.
Yes, you'll probably want a "camera". This can be as simple as only drawing a certain range of that array or a more featured camera that uses transforms to zoom out and translate across your level.
Hopefully this will help get you started.
I've done a decent amount of work in XNA, and from my experience, there are 2 ways to draw a 2D scene:
1) Strictly 2D. This method is much easier, but has a few limitations. There is no "camera" per se, what you do is move everything underneath the fixed 2D "camera". I say "camera" in quotes because the camera is fixed (as far as I know). The upside is that it's easy, the downside is that you can't easily zoom in or out or do other camera effects.
2) 2D in 3D. Set up a 3D world with a 2D plane. This is more flexible, but is also more challenging to work with because you will need to set up a 3D world and 3D camera. If this is your first attempt with making a game, I would highly recommend against this method.
I'm really only familiar with the strictly 2D method, and you would want a list of map objects that have a 2D coordinate. You would also want to store which section of the map you are looking at, I do this with a Rectangle or Vector2 that stores this. This value would move forward as the character moves. You can then take your 2D map objects' coordinate and subtract the (X,Y) of the top-left of what you are looking at to determine an object's screen position. So:
float screenX = myMapObject.X - focusPoint.X;
float screenY = myMapObject.Y - focusPoint.Y;
An other thing to note, use floats or Vector2/3 to store locations, you may not think it's required now, but it will be down the line.
It might be overkill, but my SF project uses XNA to draw a Strictly 2D scene that you can move around: http://sourceforge.net/projects/asteroidoutpost/
I hope this helps.
Have a look at Nick Gravelyns tutorials. They helped me tonne when I was first starting out - Really really worth a look for learning a lot on 2D games.
All the videos are now on youtube here

Categories

Resources