I have a multi dimensional byte array (x,y,z) which describes the terrain in my game, for example if the byte has value 0 it is air(passable terrain) and if the value is 4 it is rock(solid block).
Now I am starting to code the movement of my character by looking up the cell I want to move to and allow movement if the cell has value 0 and prevent movement if it is 4.
However I've run into a problem which is that some of my characters are wider than 1 cell unit, so for a character that is 3x3 cells wide I now have to check for collisions with the array on 9 points each time I want to move the character( I tried checking only the 4 corners but it allowed the character to go through 1x1 blocks since the center of the character was no longer being checked for collisions)
However the problem goes deeper since I also have to check for height now I need to check for a 3x3x3 character 27 points and I needed to go even further since some creatures will be quite big I need them to be 5x5x5 for a total of 125 points being compared every frame when a character is moving and I also need to have quite a lot of characters on screen at the same time.
Now I am easily detecting the collision between characters by using Bounds.Intersects, however the collisions with the terrain feel quite messy, do I really have to calculate 125 points per frame against the terrain array or is there a better way?
Related
I need help building an algorithm to solve a tile flipping game: Given an integer 'n', an n x n square of 1s and 0s is formed. I must flip all of the 1s to 0s. Although, you can only flip the tiles in the form of a rectangle (everything in the rectangle is toggled), where the upper-left vertice of the rectangle is the upper-left corner of the square. I must compute the minimum number of toggles that can be made to have all zeros.
If the solution is optimal, no rectangle is flipped more than once (indeed, we can never flip it instead of flipping it twice).
The order of flips doesn't matter.
Thus, we can use the following algorithm:
for i = n - 1 downto 0
for j = n - 1 downto 0
if f[i][j] == 1
flip(i, j) // This function should flip the entire rectangle
res += 1
If we process cells in this order, later cells never affect any previous one. Thus, we either flip the current cell or we don't.
If N is large, you can use prefix sums on rectangles to find whether we need to make a flip or not to obtain an O(N^2) time complexity (if it's small, you can flip the rectangle naively).
Not sure if there's an actual benefit over #kraskevich's answer, apart of progressively shorter rows to flip.
My proposal is to flip the furthest row which is not already in its final form and discard it. Then, the same with the column at the same distance from origin. At this point you have an n-1 x n-1 square to which we can apply the same solution.
I still find very unfortunate the situation with inner homogeneous rectangles (all 0's or all 1's.
For example, say you have as input an nxn square such that its inner n-1 x n-1 square is homogeneous and the furthest row and/or column is randomly "scrambled" with 0's and 1's. Then, to flip these outer tiles you don't have choice but to totally scramble the inner square.
My questions are:
do we actually have no choice? No possible preprocessing which globally helps?
Would the inner rectangle get irreversibly scrambled? No property I'm not seeing which would still allow us to get profit of the fact that area was originally uniform? Something like it gets scrambled when flipping the outer-row tiles but after "unscrambling" the furthest row in the inner rectangle the whole of it woyld trivially get unscrambled too?
EDIT:
I believe the second question has affirmative answer, as conditional bit flipping is reversible.
However, I still feel the need of some proof of optimality I still don't come up with.
using a file I want to create a map and I am wondering about the best approach doing so.
Actually I searched the forum but I only found map generation algorithms that randomly creates maps.
Let's look at a minimal example.
e.g. Ihave a file containing
0110
1001
1000
0000
Every 0 shall be water and every 1 shall be earth.
I would handle this by simply havin two different bitmaps and loading them at the right coordinates. That'd be simple.
But let's guess we have a 1000*1000 big map and there is only enough space for 16*16 tiles per frame. Then I'd get the current position and would build the map around it.
Assuming we can only display 3*3 tiles, using the minimal example and being at position (2,2) where x and y is element 1..4 so what the user could see at this time would be:
011
100
100
Solution
I thought about using a text file, where a line represents the x-coordinate direction and
a column represents the y-coordinate direction. The whole file is being loaded at the beginning of the program. This shouldn't use too much ram assuming 1 tile needs 1 byte, what should be enough.
For redrawing the map when the user is moving, I'd get the moving direction and slide the current Bitmap for the height/width of a tile in the opposite direction and only look up the bitmaps for the new blank spaces. So I only need to look up the tile information for m+n-1 (where m is the amount of displayed tiles in y and n in x direction) tiles (max case if moving diagonal) instead of loading m*n tiles everytime the user moves.
Example
I created an example to make the above given example more easily to understand.
this is the whole map:
We can only display 3*3 tiles and the user is at position (2,2) so what we'd actually see is:
now he is moving towards the bottom right corner:
and the black framed section is being move to the opposite direction, so that we get:
now the blank tiles (black framed white areas) have to be looked up and teh final result will be:
Question
is this a good way of building a map? Or are there much faster functions, maybe already implemented in the microsoft xna-gamestudio package ?
I would pre-fetch 1-2 tiles range outside the screen view, so that you won't have weird pop-up as the player move.
But if your game is a top-down tile game, this solution is quite conservative. In most hardware today, you could create a very big range around the player without problem. Just look at the number of block Minecraft can process and display. Since you are reusing the same texture, you just load the asset once and reuse them in a tile, which would probably an object with very little memory footprint.
Have you tried implementing it yet?
I have a list of points of a stroke and I want to detect if this stroke is rectangular.
So 4 angles of approximately 90 degrees. Afterwards I need the size, position and orientation of the rectangle.
I am using C# but algorithms in other languages or pseudocode are also usefull.
Thanks
Well, I've made something like this a while ago.
You can download it here.
http://up352.siz.co.il/up2/lhmjmdenn53m.png
This thing allows you to detect edges - as you see its pretty accurate.
When you get the edges all you need is to calculate the angles between them - and if it's ~ 90 then its a rectangle.
I'll assume that you collected each stroke into a separate list:
Find the trend line for the stroke (I'd start with Simple linear regression for this).
Find the angle between the each two intersecting trend lines (compare to 90 deg with some threshold).
Find orientation (angle) of any of the trend lines to get orientation of shape (of course anything which is near 0 mod 90 deg is the same as 0 in case of a square).
Find the length of any of the trend lines (distance from one intersection to the other), and the length of on of it's adjacent (intersecting) lines, these two lengths will be your length and width (or width and height if you like) for size calculation (area, or anything else).
In step 1 you can use many trend line computing algorithms, and it might be worth your time checking a few of them out.
In case all points are sampled into the same collection, you first need to break this collection into the 4 strokes (which is a though task on it's own...tougher task)
It might be that my math is rusty or I'm just stuck in my box after trying to solve this for so long, either way I need your help.
Background: I'm making a 2d-based game in C# using XNA. In that game I want a camera to be able to zoom in/out so that a certain part of objects always are in view. Needless to say, the objects move in two dimensions while the camera moves in three.
Situation: I'm currently using basic trigonometry to calculate which height the camera should be at for all objects to show. I also position the camera between those objects.
It looks something like this:
1.Loop through all objects to find the outer edges of our objects : farRight, farLeft, farUp, farDown.
2.When we know what the edges of what has to be shown are, calculate the center, also known as the camera position:
CenterX = farLeft + (farRight - farLeft) * 0.5f;
CenterY = farUp + (farDown - farUp) * 0.5f;
3.Loop through our edges to find the largest value compared to our camera position, thus the furthest distance from the center of screen.
4.Using the largest distance-value we can easily calculate the needed height to show all of those objects (points):
float T = 90f - Constants.CAMERA_FIELDOFVIEW * 0.5f;
float height = (float)Math.Tan(MathHelper.ToRadians(T)) * (length);
So far so good, the camera positions itself perfectly based on the calculations.
Problem:
a) My rendering target is 1280*720 with a Field of View of 45 degrees, so one always sees a bit more on the X-axis, 560 pixels more actually. This is not a problem per se but more one that on b)...
b) I want the camera to be a bit further out than it is, so that one sees a bit more on what is happening beyond the furthest point. Sure, this happens on the X-axis, but that is technically my flawed logic's result. I want to be able to see more on both the X- and Y-axis and to control this behavior.
Question
Uhm, so to clarify. I would like to have some input on a way to make the camera position itself, creating this state:
Objects won't get closer than say... 150 pixels to the edge of the X-axis and 100 pixels to the edge of the Y-axis. To do this the camera shall position itself along the Z-axis so that the field of view covers it all.
I don't need help with the coding, just the math and logic of calculating the height of my camera. As you probably can see, I have a hard time wrapping this inside my head and even harder time trying to explain it to you.
If anyone out there has been dealing with this or is just better than me at math, I'd appreciate whatever you have to say! :)
Don't you just need to add or subtract 150 or 100 pixels (depending on which edge you are looking at) to each distance measurement in your loop at step 3 and carry this larger value into length at step 4? Or am I missing something.
I can't explore this area further at the moment, but if anyone is having the same issue but is not satisfied by provided answer there is another possibility in XNA.
ViewPort.Unproject()
This nifty feature converts a screen space coordinate to a world space one.
ViewPort.Project()
Does the opposite thing, namely converting world space to screen space. Just thought that someone might want to go further than me. As much as my OCD hates to leave things not perfect, I can't be perfectioning this... yet.
I have a mesh defined by 4 points in 3D space. I need an algorithm which will subdivide that mesh into subdivisions of an arbitrary horizontal and vertical size. If the subdivision size isn't an exact divisor of the mesh size, the edge pieces will be smaller.
All of the subdivision algorithms I've found only subdivide meshes into exact powers of 2. Does anyone know of one that can do what I want?
Failing that, my thoughts about a possible implementation is to rotate the mesh so that it is flat on the Z axis, subdivide in 2D and then translate back into 3D. That's because my mind finds 3D hard ;) Any better suggestions?
Using C# if that makes any difference.
If you only have to work with a rectangle in 3D, then you simply need to obtain the two edge vectors and then you can generate all the interior points of the subdivided rectangle. For example, say your quad is defined by (x0,y0),...,(x3,y3), in order going around the quad. The edge vectors relative to point (x0,y0) are u = (x1-x0,y1-y0) and v = (x3-x0,y3-y0).
Now, you can generate all the interior points. Suppose you want M edges along the first edge, and N along the second, then the interior points are just
(x0,y0) + i/(M -1)* u + j/(N-1) * v
where i and j go from 0 .. M-1 and 0 .. N-1, respectively. You can figure out which vertices need to be connected together by just working it out on paper.
This kind of uniform subdivision works fine for triangular meshes as well, but each edge must have the same number of subdivided edges.
If you want to subdivide a general mesh, you can just do this to each individual triangle/quad. This kind of uniform subdivision results in poor quality meshes since all the original flat facets remain flat. If you want something more sophisticated, you can look at Loop subidivision, Catmull-Clark, etc. Those are typically constrained to power-of-two levels, but if you research the original formulations, I think you can derive subdivision stencils for non-power-of-two divisions. The theory behind that is a bit more involved than I can reasonably describe here.
Now that you've explained things a bit more clearly, I don't see your problem: you have a rectangle and you want to divide it up into rectangular tiles. So the mesh points you want are regularly spaced in both orthogonal directions. In 2D this is trivial, surely ? In 3D it's also trivial though the maths is a little trickier.
Off the top of my head I would guess that transforming from 3D to 2D (and aligning the rectangle with the coordinate axes at the same time) then calculating the mesh points, then transforming back to 3D is probably about as simple (and CPU-time consuming) as working it all out in 3D in the first place.
Yes, using C# means that I'm not able to propose a code to help you.
Comment or edit you question if I've missed the point.