How to make a Chunks in Unity 2D - c#

Create 2D Top Down Chunks
I was wondering how I can make a chunk system in Unity2D. My map is too big, I want to make a system of chunks, to unload and load the parts of the map, as the player progresses.
I need to do this for the game to be more optimized, but I don't know how to do that.
I've already tried to create some code or search for examples on the internet, but unfortunately I didn't find anything.

The problem in the question can be solved with a lot of strategies, but all of them require good programming experience.
I can give you an idea to send you on a good path, but without writing code, because for each project the code would change.
Think you have 4 parts. These parts are rectangular or square and form a large rectangle much larger than the player's view.
The player can start from any position, but for the example imagine that he is in the first rectangle, the one at the bottom left.
We call the rectangles A, the lower left one, B the lower right one, C the upper left one, and D the upper right one.
When the player goes to the right, walking for a while he will arrive in rectangle B.
If it continues to move to the right at some point it will arrive at the end of rectangle B and at this point you have to place rectangle A, which is on the left, next to the right side of rectangle B, so that the player does not see the end.
We now have rectangle A on the right and rectangle B on the left.
If the player continues to go to the right, he will reach the boundary of rectangle A, and there you have to move rectangle B to the right of the terrace A, as in the initial situation.
If the player goes to the left you have to think in the opposite way.
If it goes up, you have to think about the same system, but instead of with rectangles A and B, with rectangles A and C or B and D: it depends on which one it is.
This is one way to have an infinite map.
You can customize this system to have not 4 blocks but more, and to get the result you want.

Related

How to animate a simple 3D shape using Helix Toolkit?

I'm trying to animate a simple rectangular shape so that it scales in size in a certain direction. As it is, I am making a rectangle that extends from point A to B. The end goal is to animate it so that it starts at A and is transformed to be the length required to get to B.
I'm pretty new to animation in general, so this process seems finicky to me.
Right now I am:
Creating a vector between the start and end point
Finding the 8 corners of the rectangle along that vector
Creating 2 triangles for each face of the rectangle
Rendering the shape
This is all being done by using a MeshBuilder object and adding the triangles and points individually.
So, the way I'm creating the prism doesn't really help for what I need to do. Ideally I suppose, I would just create a short prism aligned between the points, and then just extend the rectangle to be the right length in an animation.
Any thoughts?
I solved this is a sense by scaling the 3D object from a size of 0 in the X/Y/Z to 1.0. So instead of the prism "extending" from A to B, it more or less "grows" to B.
Note that the ScaleTransform3D needed to have the CenterXYZ properties set to the coordinates of point A in order for it to be anchored to the correct position.
If I find a better solution, I'll update this answer later.

Wrapping my head around "snap to" guideline(s)

So I created this window editor in WPF that helps me create Forms quickly. Now, one feature I've worked on was create a guideline tool. At its core it's just creates lines to help keep my UI elements organized on the screen. I will show you an example. The long black lines are the guidelines I spoke about earlier.
Now, I noticed that in a lot of art programs (i.e Photoshop) and popular IDEs that implement Forms that they have a "snap-to" feature where a UI element will snap to a line UI or to another UI element in order to maintain alignment. Something like this:
I already have the guidelines showing up in my editor. Now, what I would like help understanding is, how would I go about implementing the "snap to" feature? I'n not asking for code, just a breakdown (a visual breakdown will be most welcomed).
These are my questions:
How does an object know if one of its edges (top, bottom, left, right) touched a line?
How would I know how to unsnap the UI element if the user keeps moving the mouse past the guideline?
If I have (say) 10 lines how do I make sure that the object attaches to the nearest line(s)?
UPDATE
When an object moves or is resized, keep track of its actual size/location relative to the mouse, and separately keep track of a snapped version of the same information. If a given actual edge is within some arbitrary distance of a line -- say 4 pixels (arbitrary WPF units, really). If it's within that distance, set it to the value for the line it's close to. You still have the actual mouse-relative values as well, so you know to unsnap it if the the user keeps on dragging it and it leaves that 4-unit zone.
When an object is being resized, at most two edges of the bounding box will be changing position (assuming you can drag corners as well as edges). When you're moving an object, all four edges of the bounding box will move.
So you need to keep track of which edges are moving, and only do snap-line proximity testing on those edges. When you're moving an object, snapping the left or top edge to a line is easy. That's just the position of the object. But if you snap the right or top edge to a line, you're setting
snappedPos.X = nearestVerticalSnapLine.X - draggedObject.Width;
or
snappedPos.Y = nearestHorizontalSnapLine.Y - draggedObject.Height;
You may also have cases where opposite edges will both be in proximity to lines: Say you're dragging a seven-unit square across a ten-unit grid. When it's inside a grid box, all four sides will be in proximity to a grid line. Which wins? The closer one.
Locating the snap lines is easy -- %.

Detect mouseover of non-square part of an Image

So I am working on a Risk type game in XNA/C#. I have a map, similar this one, and I need to be able to detect mouseovers on each territory (number). If these areas were squares, it would be easy, as they could each be represented by a rectangle. However, they are different size polygons. Is there a polygon shape that behaves similar to a square? If there isn't, how would I go about doing this?
I sugest this:attach color to each number, recreate your picture in these colors: every shape will be in its particular color. Dont draw it onscreen, use it only as reference map. And when the user clicks or moves mouse over your original map, you just simply project mouse coordinates into the color map, check the color of pixel laying under the mouse and because you have each color associated to number of territory...
This is not c# specific (as I've never written anything in the language, so no idea of what apis there are), though there are 2 algorithms that come to mind for detecting if a point is inside a polygon (which can be used to detect if a mouse point is over another polygon/map shape).
One is based on raycasting, where you cast a ray in 1 direction from the (mouse) point to "infinity" (edge of the board in this case) and count the number of times it crosses the polygon's edges. If it is odd, then the point is inside the polygon, if it is even, then the point is outside of the polygon.
A wiki link to it: http://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm
The other algorithm that comes to mind works only for triangles I think but it can be more simple to implement I think (taking a quick glance at your shapes, I think they can easily be broken down into triangles and some are already triangles). It is to do with checking if the point is on the same (internal) "side" of all the edges in the triangle. To find out what "side" a point is on vs an edge, you'd take create 2 vectors, the first vector would be the edge itself (made up of 2 points) and the other vector would be the first point of that edge to the input point, then calculate the cross product of those 2 vectors. The result will be negative or positive, which can be used to determine the "direction".
A link to it: http://www.blackpawn.com/texts/pointinpoly/default.html
(On that page is another algorithm that can also work for triangles)
Hit testing on a polygon is not so difficult to do in real time. You could use a KD-Tree for optimisation if the map is huge. Otherwise find a simple Contains method for a polygon and use that. I have one on another computer. Let me know if you'd like it.

XNA- Looking for an algorithm

I've been bashing my head for the past couple of weeks trying to find an algorithm that would help me accomplish, on first look very easy task regarding Vector3's. So, I got this one object made out of 5 cubes(2 sides, 1 top, 1 bottom, 1 back, (and there will probably be the front cubes)). Now I want to make it so when I want to Scale(2,1,1); the whole object lets say on X axis only on the top and the bottom cube would get scaled. And the side cubes should only change their position(so their X dimension stays exactly the same). The problem is with the back side. Because it does not fully cover the back side, thus if I were to Scale(2,1,1); it, it would then leave this gap on each side, which I do not want. The same principle should go for all axis. For Y I would Scale(); the side cubes, and only move the top and bottom cubes, and apply a this diffrent scale on the back side. For the Z axis I would Scale(); both the sides, top and bottom cubes, and only move the back cube.
Here are the images of an example object:
Back of the object:
http://img33.imageshack.us/img33/3125/backyd.png
Side of the object:
http://img801.imageshack.us/img801/6796/fromtheside.png
Front of the object:
http://img688.imageshack.us/img688/281/frontxh.png
Hope you can help,
EDIT:
I am sorry, I wasnt clear enought, when I Scale(); an object,lets say on X axis, like I said before want to Scale(); only the top and bottom cube. Now, about the side cubes, I want to move them so they are positioned just like before, in this case on both ends of the top and bottom cubes.
Would it be acceptable to have a parent matrix for the whole array and just scale that? Or are you worried about preserving the thickness of the walls?

Detect Rotation of a scanned image in C#

We want a c# solution to correct the scanned image because it is rotated. To solve this problem we must detect the rotation angle first and then rotate the image. This was our first thought for our problem. But then we thought image warping would be more accurate as I think it would make the scanned image like our template. Then we can process it as we know all the coordinates of our template... I searched for a free SDK or a free solution in c#. Helping me in this will be great as it is the last task in our work. Really, thanks for all.
We used the PrimeOCR product to do this. It's not free, but we couldn't find a free program that was comparable.
So, the hard part is to detect the angle of the page.
If you have full control over the template, the simplest way to do this is probably to come up with an easily-detectable symbol (e.g. a solid black circle) and stick 3 of them on the template. Then, detect them (just look for big blocks of pixels with high saturation, in the case of a solid black circle).
So, you'll then have 3 sets of coordinates. If you have a top circle, a left circle, and a right circle with all 3 circles at difference distances from one another, detecting which circle is the top circle should be pretty easy.
Then just call a rotation function. This part is easy and has been done before (e.g. http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-rotate ).
Edit:
I suggested a circle because it's easier to find the center, but a rectangle should work, too.
To be more explicit about how to actually locate the rectangles/circles, take the average Brightness value of every a × a group of pixels. If that value is greater than b, then that a × a group of pixels is part of a rectangle. a and b are varables you'll want to come up with yourself.
Use flood-fill (or, more precisely, Connected Component Labeling) group the resulting pixels together. The end result should give you your rectangles.

Categories