Find the selected segment in a wheel of fortune spinning wheel - c#

in Unity I have a circle sprite that is rotating over time.
private void Update()
{
transform.Rotate(0, 0, 10 * Time.deltaTime);
}
I want to calculate n pieces from this circle. Some examples:
The amount of pieces is given by the length of an array
private void SplitCircle(Part[] parts)
{
// split the circle by the amount of parts
}
I just have to calculate it. I don't need the UI part!
Each piece would take (360 / n) degrees from the circle. When having 5 pieces the first part should have a range from 0 to 72. The second one from 73 to 144 etc.
In my scene I have a button that stops the rotation of the circle. Is it possible to calculate which part is on top when pressing the button?

Normalize the circle rotation value to between 0 and 360 (rotation % 360).
Subtract this from each number in each segment's range in turn. For each segment, find the one where the bottom of the adjusted range values are < 0 at the lower bound, and >= 0 at the upper bound. Pick that one.
If the selected segment is out, adjust the pre-normalised rotation value by 90 degrees until the outcome fits with your "up".
Also, your range should not be 0..72, 73..144 etc. What about 72.5 degrees? It should be 72..144 with the lower bound being >= and the upper bound being <.
In untested C#ish pseudocode...
var adjust = 0; /* Adjust this +/- 90 if the result comes out with the wrong result */
var spriteRotation = (getSpriteRotationDegrees() + adjust) % 360.0f;
for (var i = 0; i < parts.length; i++) {
var lowerBound = parts[i].lowerBound() - spriteRotation;
var upperBound = parts[i].upperBound() - spriteRotation;
if (lowerBound < 0 && upperBound >= 0) {
break;
}
}
var winningPart = parts[i];

Related

How to detect if object collide with each other if all I have is a list of their positions and their size?

I have a lot of circles and I store their position in a list.
All circles have the same radius.
How can I detect if any circle collides with another circle?
What would be the fastest way?
Given the Pythagorean Theorem, a^2 + b^2 = c^2, if you take dx = circle[0].x - circle[1].x, dy = circle[0].y - circle[1].y, then you can get the hypotenuse which is the straight-line distance between centers by hypotenuse = Mathf.Sqrt(dx^2 + dy^2);.
However, you don't actually need the distance/hypotenuse, you just need to know if that value is less than 2*r, or two radii, because if the distance between the two circles is less than that then you know they're colliding.
This means that you don't need to take the square root, you can just leave it squared and compare it to the collision distance squared, or (2*r)^2. That is, you are colliding when:
(dx^2 + dy^2) <= (2*r)^2
In code, it looks like:
public class Circle
{
public float R { get; private set; }
public float X { get; private set; }
public float Y { get; private set; }
}
public static bool IsColliding(Circle c0, Circle c1)
{
var collisionDistance = (c0.R + c1.R)*(c0.R + c1.R);
var dx = c0.X - c1.X;
var dy = c0.Y - c1.Y;
return dx*dx + dy*dy <= collisionDistance;
}
If the radii are all the same then you can cache collisionDistance and not need to recalculate it on each step.
NOTE: My solution here assumes the radii aren't the same; the "2*r" term is replaced by c0.R + c1.R. If they're the same then it reduces to c0.R + c0.R or 2*c0.R.
:EDIT: To check collisions, if you have 4 circles, you'd need to check 1 against 2, 3, and 4. Then, because you already checked 1 against 2 you can skip 2 against 1. You just need to check 2 against 3 and 4, then just 3 against 4.
That is, if you have a List<Circle> circles, you would get:
var nCircles = circles.Count;
for(int i=0; i<nCircles-1; i++)
{
for(int j=i+1; j<nCircles; j++)
{
if(IsColliding(circles[i], circles[j]))
{
// Do something
}
}
}
This would check 0 against 1,2,3; then 1 against 2,3; then 2 against 3.
Assuming you have a lot of circles (e.g. at least 10,000) and you want to find all circle collisions, you can check each circle against all remaining circles, starting with the first. You can optimize the check by using the bounding box around each circle to filter in only nearby circles, and then test against a diamond inside to filter in close circles and finally test against the full circle to filter out corner circles. Since we know the radius is the same in all cases, we are filtering the centers against a circle of 2*r.
Testing only the circles inside the bounding box is about a 10% speedup, filtering in the circles inside the diamond is about another 1% speedup, and testing in the order given is fastest for collisions of 0.15% to 62% of the total list, but if your list is much smaller, extra tests may cost more time than just the simple test against the bounding circle.
Also if you only care for the first collision for each circle, you can add a break statement after the Add in the if to stop testing once a collision is found.
var r = 3.0;
var ans = new List<(int, int)>();
var r2 = 2 * r;
var r_sq = r2 * r2;
for (int j1 = 0; j1 < circles.Count - 1; ++j1) {
var c1 = circles[j1];
for (int j2 = j1 + 1; j2 < circles.Count; ++j2) {
var c2 = circles[j2];
var dx = Math.Abs(c1.x - c2.x);
var dy = Math.Abs(c1.y - c2.y);
if (dx <= r2 && dy <= r2 && ((dx + dy <= r2) || (dx * dx + dy * dy <= r_sq))) {
ans.Add((j1, j2));
}
}
}
If this isn't sufficient, and you can store your circles in a different structure, it is probably time to look at something like k-d trees.

Random values seem to be not really random?

Trying to make a simple bacteria-killing game using WinForm in C#, but the bacteria (I am using Panel for the time being) doesn't seem to move around at random.
Specifically, the problem I am having is, the bacteria tries to move towards the upper left corner and move around there only. Ideally, the bacteria needs to move around the rectangle range evenly, but I am not sure how to achieve that.
Look at the gif file below.
As you can see the red Panel moves around the upper left corner only. How can I get it to move everywhere evenly and randomly?
Here is my code:
private Panel _pnlBacteria; //Panel representing a piece of bacteria
private Random r = new Random(); //For randomly-generated values
private int _prevX; //Stores the previous X location
private int _prevY; //Stores the previous Y location
public Form1()
{
InitializeComponent();
_pnlBacteria = new Panel();
/* Get more property assignments to this._pnlBacteria (omitted) */
//Bacteria's start position is also randomly selected
_prevX = r.Next(50, 300);
_prevY = r.Next(50, 500);
}
//Timer runs every 100 seconds changing the location of the bacteria
private void TmrMoveBacteria_Tick(object sender, EventArgs e)
{
int x, y;
//Get random values for X and Y based on where the bacteria was previously
//and move randomly within ±10 range. Also it cannot go off the screen.
do
{
x = r.Next(_prevX - 10, _prevX + 10);
y = r.Next(_prevY - 10, _prevY + 10);
}
while ((y <= 0) || (y >= 500) || (x <= 0) || (x >= 300));
//Save the new location to be used in the next Tick round as previous values
_prevX = x;
_prevY = y;
//Apply the actual location change to the bacteria panel
_pnlBacteria.Top = y;
_pnlBacteria.Left = x;
}
I tried changing the +10 to +12, leaving -10 as it is, but now this only made the bacteria move to the bottom right corner only. I am at a loss.
Can anyone please help?
If you read the documentation of Random.next(int,int) you'll find the the lower bound is inclusive and the upper bound is exclusive, that's why -10 and +11 works.
A slightly different way to tackle this might be to choose a random direction and a random distance to travel in that direction. This will allow more movement over the form if we increase the maximum distance the bacteria can travel before changing direction.
We can also "weight" the randomness of the distance so that shorter distances are chosen more often. This will add more variety and randomness to the movement pattern, while still allowing a few long sprints to another location.
Here's an example that you can copy/paste into a new form project implementing this idea. You can play with the settings for maxDistance (the furthest distance allowed before changing direction) and stepDistance (the distance travelled on each iteration of the Timer). A smaller stepDistance will result in smoother, but slower movement. I've made it pretty small, so I've also decreased the Interval property of the Timer to speed it up.
You'll notice I also added a method to validate that the direction is valid, so that the bacteria doesn't run off the screen. I created an enum to represent directions, which made it easy to check for movement in a particular direction (i.e. if the enum value contains "North" and we're too close to the top, then it's an invalid direction - this covers "North", "NorthWest", and "Northeast" directions).
Edit: I moved the creation of the "weighted distances" list to the constructor, and modified it to select exponentially fewer items rather than linearly fewer items.
Hope it makes sense:
public partial class Form1 : Form
{
// Program Settings and Controls
private readonly Panel pnlBacteria; // Panel representing a piece of bacteria
private readonly Random random = new Random(); // For randomly-generated values
private readonly Timer tmrMoveBacteria; // Timer used for bacteria movement
private readonly int bacteriaSize = 20; // Stores the size for our bacteria
private const int maxDistance = 50; // The maximum number of moves allowed in the same direction.
private const int stepDistance = 3; // The distance to travel on each iteration of the timer. Smaller number is slower and smoother
private readonly List<int> weightedDistances; // Contains a weighted list of distances (lower numbers appear more often than higher ones)
// Bacteria state variables
private Direction direction; // Stores the current direction bacteria is moving
private int distance; // Stores the distance remaining to travel in current direction
// Represents possible directions for bacteria to move
private enum Direction
{
North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest
}
public Form1()
{
InitializeComponent();
// Initialize our weighted differences array so that 1 is
// chosen most often and maxDistance is chosen the least often
weightedDistances = new List<int>();
for (var i = 0; i < maxDistance; i++)
{
var weight = maxDistance / (i + 1);
for (var j = 0; j <= weight; j++)
{
weightedDistances.Add(i + 1);
}
}
// Give life to the bacteria
pnlBacteria = new Panel
{
BackColor = Color.Red,
Width = bacteriaSize,
Height = bacteriaSize,
Left = random.Next(0, ClientRectangle.Width - bacteriaSize),
Top = random.Next(0, ClientRectangle.Height - bacteriaSize)
};
Controls.Add(pnlBacteria);
// Start bacteria movement timer
tmrMoveBacteria = new Timer {Interval = 10};
tmrMoveBacteria.Tick += TmrMoveBacteria_Tick;
tmrMoveBacteria.Start();
}
/// <summary>
/// Sets the direction and distance fields to valid values based on the
/// current bacteria position, direction, and remaining distance
/// </summary>
private void UpdateDirectionAndDistance()
{
// Get all directions
var validDirections = Enum.GetValues(typeof(Direction)).Cast<Direction>();
// Remove invalid directions (based on the bacteria position)
if (pnlBacteria.Top < bacteriaSize) validDirections =
validDirections.Where(dir => !dir.ToString().Contains("North"));
if (pnlBacteria.Right > ClientRectangle.Width - bacteriaSize) validDirections =
validDirections.Where(dir => !dir.ToString().Contains("East"));
if (pnlBacteria.Left < bacteriaSize) validDirections =
validDirections.Where(dir => !dir.ToString().Contains("West"));
if (pnlBacteria.Bottom > ClientRectangle.Height - bacteriaSize) validDirections =
validDirections.Where(dir => !dir.ToString().Contains("South"));
// If we're supposed to keep on moving in the same
// direction and it's valid, then we can exit
if (distance > 0 && validDirections.Contains(direction)) return;
// If we got here, then we're setting a new direction and distance
distance = weightedDistances[random.Next(weightedDistances.Count)];
var directions = validDirections.Where(d => d != direction).ToList();
direction = directions[random.Next(directions.Count)];
}
/// <summary>
/// Executes on each iteration of the timer, and moves the bacteria
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TmrMoveBacteria_Tick(object sender, EventArgs e)
{
// Ensure direction and distance are valid
UpdateDirectionAndDistance();
// Move the bacteria
var dirStr = direction.ToString();
if (dirStr.Contains("North")) pnlBacteria.Top -= stepDistance;
if (dirStr.Contains("East")) pnlBacteria.Left += stepDistance;
if (dirStr.Contains("South")) pnlBacteria.Top += stepDistance;
if (dirStr.Contains("West")) pnlBacteria.Left -= stepDistance;
distance--;
}
}
The left and right arguments of r.Next are inclusive and exclusive respectively.
x = r.Next(_prevX - 10, _prevX + 10);
y = r.Next(_prevY - 10, _prevY + 10);
So the probability space of this code is a 19x19 square off center of (_prevX,_prevY) to its upper left. This means displacement is more likely towards the upper left.
If we use 11s on the right, then the probability space is a 20x20 square centered at (_prevX,_prevY). The bacteria will move more evenly. Its average displacement within its probability square is zero, so there's not a single favored direction. But the diagonal directions will be favored since diagonal displacement is the greatest on average. This may or may not be important depending on the situation.
If we use radius and direction as explained by Rufus L, then we can generate a more "natural" circular probability space. That's why it's the preferred approach.
The problem is that you are moving the object back and forth because the range is prev -+10;
x = r.Next(_prevX - 10, _prevX + 10);
y = r.Next(_prevY - 10, _prevY + 10);
you should add a direction flag for yval and xval, set it with certain probability p(less then 1/2) , when the flag is on go right/up, when its off go left/down ,
if (forwardx){
x = r.Next(_prevX , _prevX + 10);
}
else {
x = r.Next(_prevX - 10, _prevX );
}
if (forwardy){
y = r.Next(_prevY , _prevY + 10);
}
else{
y = r.Next(_prevY - 10 , _prevY );
}
forwardx = r.Next(10) == 1; //p = 0.1
forwardy = r.Next(10) == 1;
Good luck!
You should use the size of your panel.height - size of your square.height same for length instead of constants this way if the screen size changes your game will still work.
_prevX = r.Next(bateriaSize, this.Width-bacteriaSize); //this.Width Gets you the of form
_prevY = r.Next(bateriaSize, this.Height-bacteriaSize); //This.Height gets you the height of form

Get the perimeter of a list of verticies

I am currently developing a level editor, and I've gotten to the point where I have collected a list of vertices that I want to use to create a Polygon collider.
I have obtained these vertices by tagging certain tiles as "colliders" and running them through an algorithm to get a list of tiles that are connected. I then created a list of vertices from the list of connected tiles and removed any duplicates.
Below is an image that will help explain. All of the dots are verticies that are currently in my list, but I want to use the ones colored red to create a polygon.
This is how I ended up solving my problem. Each vertex is a part of four different tiles (Excluding edge of map cases), so I just iterated through every vertex and counted how many neighbors are "of the same type". If the result was 4 then it meant that the vertex was somewhere in the middle of the polygon. If the result was 3 it meant the vertex was on an inner corner. 2 meant it was on an edge. 1 meant it was an outer corner.
private List<Vector2> GetPerimeterOfVerticeList(List<Vector2> vertices, TileType type)
{
int neighborCount = 0;
List<Vector2> perimeter = new List<Vector2>();
//Check the four tiles touching this vertex
foreach (Vector2 v in vertices)
{
//upper left tile
if (v.x - 1 >= 0 && v.y <= gridHeight - 1 && grid[(int)v.x - 1, (int)v.y].type == type)
{
neighborCount++;
}
//upper right
if (v.x <= gridWidth - 1 && v.y <= gridHeight - 1 && grid[(int)v.x, (int)v.y].type == type)
{
neighborCount++;
}
//bottom right
if (v.y - 1 >= 0 && v.x <= gridWidth - 1 && grid[(int)v.x, (int)v.y - 1].type == type)
{
neighborCount++;
}
//bottom left
if (v.y - 1 >= 0 && v.x - 1 >= 0 && grid[(int)v.x - 1, (int)v.y - 1].type == type)
{
neighborCount++;
}
//If we have less than 4 neighbors, it means we are on the edge. 3 is an inner corner, 2 is a side piece, 1 is an outer corner
if (neighborCount < 4)
{
perimeter.Add(v);
}
//Reset the neighbor count back to 0 for the next vertex check.
neighborCount = 0;
}
return perimeter;
}

Trying to move an object in a matrix using line drawing algorithm

I'm trying to move an object in a matrix (an array with [x,y]) using a line drawing algorithm, to help you understand what I mean I'm trying to make an object move like this:
But instead of going "in line" it goes like this:
I opened another question about this problem here, and you told me to use a line drawing algorithm, which I did, but I still can't make it move in that order.
A little bit about the code (I'm giving you some 'background' so you won't be confused): The Location variable contains a location on the matrix, it has x and y, which can be accessed like this:
Location loc = new Location(x,y);//Declaring a new location
int row = loc.Row;//Gets the x value (Row)
int col = loc.Col;//Gets the y value (Column)
The Direction variable contains a direction, there are 5 directions:
Direction.NORTH;
Direction.SOUTH;
Direction.EAST;
Direction.WEST;
Direction.NOTHING; //This direction means to stay at the same place, or not move
I think it's obvious what each of them means.
The command game.Destination(myPirate, direction); calculates where the object ends up on the next turn(returns a location).
Now here is the code that I got:
public static Direction NextDirection(Game game,Pirate myPirate,Location EndLocation)
{
List<Direction> westEast = new List<Direction>() { Direction.EAST, Direction.WEST };
List<Direction> northSouth = new List<Direction>() { Direction.NORTH, Direction.SOUTH };
int startX = myPirate.Loc.Row;
int startY = myPirate.Loc.Col;
int endX = EndLocation.Row;
int endY = EndLocation.Col;
if (startX == endX && startY == endY) return Direction.NOTHING; //If its alredy on spot return the location of the pirate;
int dx = endX - startX;
int dy = endY - startY;
if (dx == 0) //The X of the end is the same as the x of the start, need to move only on the y axis;
{
return MyBot.GetBestDirection(game, myPirate, EndLocation);
}
if (dy==0) //The Y of the end is the same as the y of the start, need to move only on the x axis;
{
return MyBot.GetBestDirection(game, myPirate, EndLocation);
}
int slope = dy / dx;
Location destination;
double distance = MyBot.Distance(myPirate.Loc, EndLocation);
if (slope > 1 || slope < -1)
{
double distance2;
foreach (Direction dir in northSouth) //In here the algoritem decides whats the best direction (north or south);
{
destination = game.Destination(myPirate, dir);
distance2 = MyBot.Distance(destination, EndLocation);
if (distance2 < distance) return dir;
}
game.Debug("Couldnt find a good direction, going by the default dirction algorithem.");
return MyBot.GetBestDirection(game, myPirate, EndLocation);
}
else
{
double distance2;
foreach (Direction dir in westEast)//In here the algoritem decides whats the best direction (west or east);
{
destination = game.Destination(myPirate, dir);
distance2 = MyBot.Distance(destination, EndLocation);
if (distance2 < distance) return dir;
}
game.Debug("Couldnt find a good direction, going by the default dirction algorithem.");
return MyBot.GetBestDirection(game, myPirate, EndLocation);
}
}
On some parts in the code above I'm also using parts from the MyBot class, here are these parts:
public static Direction GetBestDirection(Game game, Pirate myPirate, Location loc)//ADD: If the destination is not passable get another direction;
{
double distance = Distance(myPirate.Loc, loc);
List<Direction> allDirections = new List<Direction>() { Direction.NORTH, Direction.SOUTH, Direction.WEST, Direction.EAST, Direction.NOTHING };
Location destination;
foreach (Direction dir in allDirections)
{
destination = game.Destination(myPirate, dir);
double distance2 = Distance(destination, loc);
if (distance2 < distance && game.IsPassable(destination)) return dir;
}
return Direction.NOTHING;
}
The 'object' I told you about is called myPirate in the code, and it can only move in one direction each turn. the code run again in each turn until it gets to the target.
How can I make this program work right? What is wrong with my code?
You should compute the slope as a float or double, not as an int. When you use integer division, 1/2 is 0, and of course when the slope is 0 you are moving to the right. This means if you go from (0,0) to (20,10) you would have to start with 10 (1,0) steps if you are using integer division, and that is not the best behavior.
If you set a fixed threshold for the (floating point) slope, such as if the slope is less than 1.0 then go right, then you will not follow a line closely at all because you will move due right until you get to a point where the slope is greater than the threshold. So, don't use a fixed threshold.
A quick-and-dirty method is to randomize the threshold so that it causes you to move in the right direction on average. I'll assume dx>0 and dy>0. You can handle the other quadrants by symmetry. To move in the right direction on average, you can choose a random integer from [0,dx+dy-1]. If it is less than dx, then take a step in the x direction. If it is greater than or equal to dx, take a step in the y direction. Equivalently, choose a random double in [0,1] and if it is lower than dx/(dx+dy), take a step in the x direction, otherwise take a step in the y direction.
If you don't like the randomization, then you can derandomize this. Instead of picking a fixed threshold, you can choose a pseudo-random function of (dx,dy,x,y). For example, you could compare dx/(double)(dx+dy) with (2^((dx+3*dy)%28) mod 29)/29.0. This sets the threshold from 1/29 to 28/29 in a roughly uniform manner.
Edit: Here is some untested code.
// assume dx>0, dy>0
int linearMod28 = (dx + 3*dy) % 28; // 0 through 27
int pseudorandomMod29 = (1 << linearMod28) % 29; // 1 through 28
double threshold = pseudorandomMod29/29.0; // 1/29 through 28/29
if (dx/(double)(dx+dy) < threshold)
// take a step in the x direction
else
// take a step in the y direction

How to draw thousands of tiles without killing FPS

I've looked everywhere for a workaround to this issue (I may just be blind to see the solutions lying around). My game currently renders the tilemap on the screen and will not render tiles that are not actually within the screen bounds. However, each tile is 16x16 pixels, that means 8100 tiles to draw if every pixel on the screen contains a tile at 1920x1080 resolution.
Drawing that many tiles every cycle really kills my FPS. If I run 800x600 resolution my FPS goes to ~20, and at 1920x1080 it runs at around 3-5 FPS. This really drives me nuts.
I've tried threading and using async tasks, but those just flicker the screen. Probably just me coding it incorrectly.
Here's the drawing code that I currently use.
// Get top-left tile-X
Vector topLeft = new Vector(Screen.Camera.X / 16 - 1,
Screen.Camera.Y / 16 - 1);
Vector bottomRight = new Vector(topLeft.X + (Screen.Width / 16) + 2,
topLeft.Y + (Screen.Height / 16) + 2);
// Iterate sections
foreach (WorldSection section in Sections)
{
// Continue if out of bounds
if (section.X + ((Screen.Width / 16) + 2) < (int)topLeft.X ||
section.X >= bottomRight.X)
continue;
// Draw all tiles within the screen range
for (int x = topLeft.X; x < bottomRight.X; x++)
for (int y = topLeft.Y; y < bottomRight.Y; y++)
if (section.Blocks[x - section.X, y] != '0')
DrawBlock(section.Blocks[x - section.X, y],
x + section.X, y);
}
There are between 8 and 12 sections. Each tile is represented by a char object in the two-dimensional array.
Draw block method:
public void DrawBlock(char block, int x int y)
{
// Get the source rectangle
Rectangle source = new Rectangle(Index(block) % Columns * FrameWidth,
Index(block) / Columns * FrameHeight, FrameWidth, FrameHeight);
// Get position
Vector2 position = new Vector2(x, y);
// Draw the block
Game.spriteBatch.Draw(Frameset, position * new Vector2(FrameWidth, FrameHeight) - Screen.Camera, source, Color.White);
}
The Index() method just returns the frame index of the tile corresponding to the char.
I'm wondering how I could make it possible to actually allow this much to be drawn at once without killing the framerate in this manner. Is the code I provided clearly not very optimized, or is it something specific I should be doing to make it possible to draw this many individual tiles without reducing performance?
Not sure if this is the best way to deal with the problem, but I've started to use RenderTarget2D to pre-render chunks of the world into textures. I have to load chunks within a given area around the actual screen bounds at a time, because loading all chunks at once will make it run out of memory.
When you get close to the bounds of the current pre-rendered area, it will re-process chunks based on your new position in the world. The processing takes roughly 100 milliseconds, so when loading new areas the player will feel a slight slowdown for this duration. I don't really like that, but at least the FPS is 60 now.
Here's my chunk processor:
public bool ProcessChunk(int x, int y)
{
// Create render target
using (RenderTarget2D target = new RenderTarget2D(Game.CurrentDevice, 16 * 48, 16 * 48,
false, SurfaceFormat.Color, DepthFormat.Depth24))
{
// Set render target
Game.CurrentDevice.SetRenderTarget(target);
// Clear back buffer
Game.CurrentDevice.Clear(Color.Black * 0f);
// Begin drawing
Game.spriteBatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend);
// Get block coordinates
int bx = x * 48,
by = y * 48;
// Draw blocks
int count = 0;
foreach (WorldSection section in Sections)
{
// Continue if section is out of chunk bounds
if (section.X >= bx + 48) continue;
// Draw all tiles within the screen range
for (int ax = 0; ax < 48; ax++)
for (int ay = 0; ay < 48; ay++)
{
// Get the block character
char b = section.Blocks[ax + bx - section.X, ay + by];
// Draw the block unless it's an empty block
if (b != '0')
{
Processor.Blocks[b.ToString()].DrawBlock(new Vector2(ax, ay), true);
count++;
}
}
}
// End drawing
Game.spriteBatch.End();
// Clear target
target.GraphicsDevice.SetRenderTarget(null);
// Set texture
if (count > 0)
{
// Create texture
Chunks[x, y] = new Texture2D(Game.CurrentDevice, target.Width, target.Height, true, target.Format);
// Set data
Color[] data = new Color[target.Width * target.Height];
target.GetData<Color>(data);
Chunks[x, y].SetData<Color>(data);
// Return true
return true;
}
}
// Return false
return false;
}
If there are any suggestions on how this approach can be improved, I won't be sad to hear them!
Thanks for the help given here!

Categories