i´m having a little problem with my world collison in a platformer game i´m working on. I know why the (or I think I know) problem occours. My program seems to go into the first "if" when it´s not ment too. Something there is wrong (I think) but I can´t find what.
Secondly I wonder if my solution over all is an good idea or if a should go for something else. If so what´s wrong? I´m pretty beginner on programming so please be gentle with me ;D
protected void CheckWorldCollison()
{
float collisionWidth = this.width + (Tile.GetTileSize() * 2);
float collisionHeight = this.height + (Tile.GetTileSize() * 2);
for (int x = -1; x < (collisionWidth / Tile.GetTileSize()) - 1; x++)
{
for (int y = -1; y < (collisionHeight / Tile.GetTileSize()) - 1; y++)
{
Vector2 tempTilePosition = new Vector2((int)(this.position.X / Tile.GetTileSize()) + x, (int)(this.position.Y / Tile.GetTileSize()) + y);
if (tempTilePosition.X >= 0 && tempTilePosition.X < (Game1.testMap.GetWidth())
&& tempTilePosition.Y >= 0 && tempTilePosition.Y < (Game1.testMap.GetHeight()))
{
this.tilesToCheck.Add(Game1.testMap.GetTile(tempTilePosition));
}
}
}
foreach (Tile t in this.tilesToCheck)
{
t.setIsCkecked(true);
if (t.GetTileType() != TileType.transparent)
{
Vector2 distance = this.position - t.GetPosition();
//Console.WriteLine(distance);
Console.WriteLine((distance + this.origin).X);
if (((distance + this.origin).X >= 0 && (distance + this.origin).X <= Tile.GetTileSize())
|| ((distance - this.origin).X >= 0 && (distance - this.origin).X <= Tile.GetTileSize()))
{
Console.WriteLine((distance + this.origin).Y);
if ((distance + this.origin).Y <= 0)
{
if (this.speed.Y > 0)
{
if ((distance + this.origin + this.speed).Y > 0)
{
this.position.Y = (t.GetPosition() - this.origin).Y;
this.speed.Y = 0;
}
}
}
if (((distance - this.origin).Y - Tile.GetTileSize()) <= 0)
{
if (this.speed.Y < 0)
{
if (((distance - this.origin + this.speed).Y - Tile.GetTileSize()) < 0)
{
this.position.Y = (t.GetPosition() + this.origin).Y + Tile.GetTileSize();
this.speed.Y = 0;
}
}
}
}
if (((distance + this.origin).Y >= 0 && (distance + this.origin).Y <= Tile.GetTileSize())
|| ((distance - this.origin).Y >= 0 && (distance - this.origin).Y <= Tile.GetTileSize()))
{
if ((distance + this.origin).X <= 0)
{
if (this.speed.X > 0)
{
if ((distance + this.origin + this.speed).X > 0)
{
this.position.X = (t.GetPosition() - this.origin).X;
this.speed.X = 0;
}
}
}
if (((distance - this.origin).X - Tile.GetTileSize()) >= 0)
{
if (this.speed.X < 0)
{
if (((distance - this.origin + this.speed).X - Tile.GetTileSize()) < 0)
{
this.position.X = (t.GetPosition() + this.origin).X + Tile.GetTileSize();
this.speed.X = 0;
}
}
}
}
}
}
this.tilesToCheck.Clear();
}
Just wanna say that I guess that these questions pop up here like all the time, but I hadn´t written if I really didn´t need help.
Related
I have two project: a class library and a winform and all of the calculations is in the class library. now I want to use a progress bar for one of my forms when the user click on Run botton. I tried the below code but the progress bar filled as soon as loading data (about 6000 or more) in datagridview.
Code in Winform project:
private void BtnRun_Click(object sender, EventArgs e)
{
progressBar1.Maximum = TTools.Depth.Count;
progressBar1.Step = 1;
foreach (double doub in TLitho.CalcPercent().Item3)
{
progressBar1.PerformStep();
}
if (TTools.Depth != null)
{
dataGridView1.AllowUserToAddRows = false;
List<double> DolomitePer = TLitho.CalcPercent().Item3;
List<double> Depth = TTools.Depth;
var sourceD = new BindingSource
{
DataSource = Depth
};
for (int i = 0; i < (sourceD.Count); i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = sourceD[i];
}
var sourcePD = new BindingSource
{
DataSource = DolomitePer
};
for (int i = 0; i < (sourcePD.Count); i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[1].Value = sourcePD[i];
}
}
}
code in class library project:
public static Tuple<List<double>, List<double>, List<double>> CalcPercent()
{
for (int j = 0; j < TTools.Depth.Count; j++)
{
MinimumIndexQC.Add(80000);
MinimumIndexQC[j] = lQC[j].IndexOf(lQC[j].Min());
MinimumIndexCD.Add(80000);
MinimumIndexCD[j] = lCD[j].IndexOf(lCD[j].Min());
MinimumIndexQD.Add(80000);
MinimumIndexQD[j] = lQD[j].IndexOf(lQD[j].Min());
CDandND.Add(Math.Abs(lCD[j][MinimumIndexCD[j]] - TPorosity.CalculateNDPorosity()[j]));
QCandND.Add(Math.Abs(lQC[j][MinimumIndexQC[j]] - TPorosity.CalculateNDPorosity()[j]));
QDandND.Add(Math.Abs(lQD[j][MinimumIndexQD[j]] - TPorosity.CalculateNDPorosity()[j]));
if (CDandND[j] < QCandND[j] && CDandND[j] < QDandND[j])
{
DistanceCD.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.9162) / Math.Sqrt(Math.Pow(0.0173, 2) + 1))
+ (Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.7031) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)));
DolomitePercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.7031) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)) / DistanceCD[j]);
CalcitePercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.9162) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)) / DistanceCD[j]);
QuartzPercent.Add(0);
DistanceQC.Add(0);
DistanceQD.Add(0);
}
else if (QCandND[j] < CDandND[j] && QCandND[j] < QDandND[j])
{
DistanceQC.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0192 * TPorosity.Neutron[j]) - 2.5818) / Math.Sqrt(Math.Pow(0.0192, 2) + 1))
+ (Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.7031) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)));
QuartzPercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.7031) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)) / DistanceQC[j]);
CalcitePercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0192 * TPorosity.Neutron[j]) - 2.5818) / Math.Sqrt(Math.Pow(0.0192, 2) + 1)) / DistanceQC[j]);
DolomitePercent.Add(0);
DistanceCD.Add(0);
DistanceQD.Add(0);
}
else if (QDandND[j] < CDandND[j] && QDandND[j] < QCandND[j])
{
DistanceQD.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0192 * TPorosity.Neutron[j]) - 2.5818) / Math.Sqrt(Math.Pow(0.0192, 2) + 1))
+ (Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.9162) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)));
QuartzPercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.9162) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)) / DistanceQD[j]);
DolomitePercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0192 * TPorosity.Neutron[j]) - 2.5818) / Math.Sqrt(Math.Pow(0.0192, 2) + 1)) / DistanceQD[j]);
CalcitePercent.Add(0);
DistanceCD.Add(0);
DistanceQC.Add(0);
}
else
{
DistanceCD.Add(0);
DistanceQC.Add(0);
DistanceQD.Add(0);
DolomitePercent.Add(0);
CalcitePercent.Add(0);
QuartzPercent.Add(0);
}
if (DolomitePercent[j]==0 && CalcitePercent[j]==0 && QuartzPercent[j]==0 && TPorosity.BulkDensity[j] < (-0.0173 * TPorosity.Neutron[j]) + 2.9162)
{
DolomitePercent[j] = 100;
}
if (DolomitePercent[j] == 0 && CalcitePercent[j] == 0 && QuartzPercent[j] == 0 && TPorosity.BulkDensity[j]> 2.5943 * Math.Pow(Math.E, (-0.009 * TPorosity.Neutron[j])))
{
QuartzPercent[j] = 100;
}
}
return Tuple.Create(DolomitePercent, CalcitePercent, QuartzPercent);
}
}
Hope anyone help me.
thank you
I have a for loop and what I'd like to do is to store the data of every for cycle in C#.
At the moment it only stores the datas of the last iteration.
Attached my code. Thanks a lot!
for (int i = 0; i < n; i++)
{
if (i <= k - p - 1)
{
alpha[i] = 1;
NewCPVector[i] = CPVector[i];
}
if (k - p <= i && i <= k-1)
{
alpha[i] = (FinalKnotsVector[k] - Initialknots[i]) / (Initialknots[i + p + 1] - Initialknots[i]);
NewCPVector[i] = alpha[i] * CPVector[i] + (1 - alpha[i]) * CPVector[i - 1];
}
if (i >= k)
{
alpha[i] = 0;
NewCPVector[i] = CPVector[i - 1];
}
}
I'm going to assume that your arrays hold double values. (However it could be other types, like float or decimal) you just have to specify that type in the declaration of the list
You could save the data in a List this way:
List<double> data = new List<double>();
for (int i = 0; i < n; i++)
{
if (i <= k - p - 1)
{
alpha[i] = 1;
data.Add(NewCPVector[i] = CPVector[i]);
}
if (k - p <= i && i <= k-1)
{
alpha[i] = (FinalKnotsVector[k] - Initialknots[i]) / (Initialknots[i + p + 1] - Initialknots[i]);
data.Add(alpha[i] * CPVector[i] + (1 - alpha[i]) * CPVector[i - 1]);
}
if (i >= k)
{
alpha[i] = 0;
data.Add(CPVector[i - 1]);
}
}
Here's the part 1 of my question, if you wanna check the background of this question :
Detecting brackets in input string
Forgive me if the title doesn't match, since I also confused how to name it appropriately to picture my problem. If anyone knows a more appropriate title, feel free to edit.
So, given below code (my own code) :
private const int PARTICLE_EACH_CHAR = 4;
/*ProcessBarLines : string s only contains numbers, b, [, and ]*/
private int ProcessBarLines(Canvas canvas, string s, int lastLineAboveNotation)
{
List<int> bracket = new List<int>();
List<int> other = new List<int>();
int currentCloseNumber = 0;
int currentOpenNumber = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '[')
{
bracket.Add(i);
currentOpenNumber++;
if (i - 1 > 0 && s[i - 1] != '[')
{
currentOpenNumber = 1;
}
}
else if (s[i] == ']')
{
bracket.Add(i);
currentCloseNumber++;
if (i + 1 >= s.Length || s[i + 1] != ']' || currentOpenNumber == currentCloseNumber)
{
int min = bracket.Count - (currentCloseNumber * 2);
int max = bracket[bracket.Count - 1];
List<int> proc = new List<int>();
int firstIndex = -1;
int lastIndex = -1;
for (int ii = 0; ii < other.Count; ii++)
{
if (other[ii] > min && other[ii] < max)
{
proc.Add(other[ii]);
if (firstIndex == -1)
{
firstIndex = ii;
lastIndex = ii;
}
else
{
lastIndex = ii;
}
}
}
double leftPixel = firstIndex * widthEachChar;
double rightPixel = (lastIndex * widthEachChar) + widthEachChar;
DrawLine(canvas, currentCloseNumber, leftPixel,
rightPixel, lastLineAboveNotation * heightEachChar / PARTICLE_EACH_CHAR);
lastLineAboveNotation += currentCloseNumber - 1;
currentOpenNumber -= currentCloseNumber;
currentCloseNumber = 0;
}
}
else
{
other.Add(i);
}
}
return lastLineAboveNotation + 1;
}
Here's the test cases :
Picture 1 & 2 is the correct answer, and picture 3 is the wrong answer. Picture 3 should have a line, just like inverted from number 2, but, apparently, (if you look closely) the line is drawn on the right, but it should be on the left to be correct (above 0).
I figured, the problem is, I'm quite sure on the "min". Since it doesn't give the correct starting value.
Any idea on this? Feel free to clarify anything. It's used for writing numeric musical scores.
Btw, DrawLine() just meant to draw the line above the numbers, it's not the problem.
Finally! I found it!
private int ProcessBarLines(Canvas canvas, string s, int lastLineAboveNotation)
{
List<int> bracket = new List<int>();
List<int> other = new List<int>();
int currentCloseNumber = 0;
int currentOpenNumber = 0;
int space = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '[')
{
bracket.Add(i);
currentOpenNumber++;
if (i - 1 > 0 && s[i - 1] != '[')
{
currentOpenNumber = 1;
}
}
else if (s[i] == ']')
{
bracket.Add(i);
currentCloseNumber++;
if (i + 1 >= s.Length || s[i + 1] != ']' || currentOpenNumber == currentCloseNumber)
{
int min = bracket[Math.Max(bracket.Count - ((currentCloseNumber * 2) + space), 0)];
int max = bracket[bracket.Count - 1];
space = max - min - 1;
List<int> proc = new List<int>();
int firstIndex = -1;
int lastIndex = -1;
for (int ii = 0; ii < other.Count; ii++)
{
if (other[ii] > min && other[ii] < max)
{
proc.Add(other[ii]);
other[ii] = -1;
if (firstIndex == -1)
{
firstIndex = ii;
lastIndex = ii;
}
else
{
lastIndex = ii;
}
}
}
double leftPixel = firstIndex * widthEachChar;
double rightPixel = (lastIndex * widthEachChar) + widthEachChar;
DrawLine(canvas, currentCloseNumber, leftPixel,
rightPixel, lastLineAboveNotation * heightEachChar / PARTICLE_EACH_CHAR);
lastLineAboveNotation += 1;
currentOpenNumber -= currentCloseNumber;
currentCloseNumber = 0;
}
}
else
{
other.Add(i);
}
}
return lastLineAboveNotation + 1;
}
If someone got a more efficient code, please let us know!
I have a function to check neighbors of an array and if that element is equal with 1. X is for each neighbor found and v[l] is the position for each 0. I have a problem with this code each time gives me "Index was outside the bounds of the array" and i don't know what to do else.
public int modificari(int i,int j,int n,int m)
{
int x = 0;
v = new int[n];
l=0;
if (mat[i, j] == 1)
{
if (j++ < m)
{
if (mat[i, j++] == 1)
x++;
else
{
v[l] = i * n + j + 2;
l++;
}
}
if (j++ < m && i++ < n)
{
if (mat[i++, j++] == 1)
x++;
else
{
v[l] = (i + 1) * n + j + 2;
l++;
}
}
if (i++ < n)
{
if (mat[i++, j] == 1)
x++;
else
{
v[l] = (i + 1) * n + j + 1;
l++;
}
}
if (j-- >= 0 && i++ < n)
{
if (mat[i++, j--] == 1)
x++;
else
{
v[l] = (i + 1) * n + j;
l++;
}
}
if (j-- >= 0)
{
if (mat[i, j--] == 1)
x++;
else
{
v[l] = i * n + j;
l++;
}
}
if (j-- >= 0 && i-- >= 0)
{
if (mat[i--, j--] == 1)
x++;
else
{
v[l] = (i - 1) * n + j;
l++;
}
}
if (i-- >= 0)
{
if (mat[i--, j] == 1)
x++;
else
{
v[l] = (i - 1) * n + j + 1;
l++;
}
}
if (j < n && i-- >= 0)
{
if (mat[i--, j++] == 1)
x++;
else
{
v[l] = (i - 1) * n + j + 2;
l++;
}
}
if (x < 2 && x > 3)
return 1;
else
return random();
}
return x;
}
That is a total mess. It is very hard to follow, even for an experienced coder. Use of one letter variable names and inline ++ operators is usually discouraged for the sake of readability.
I've quickly tried to rewrite your function from my best guess of what you're trying to achieve. I'm hoping you can see a different way to approach the problem that suits you better.
NOTE: I did not test this code at all, it probably has compile errors.
public struct Point
{
public int X;
public int Y;
public Point( int x, int y )
{
X = x;
Y = y;
}
}
public class Whatever
{
// ...
// Here is a list of the positions of all the neighbours whose values are
// zero.
List<Point> zeroPositions = new List<Point>();
// ...
public int Modificari(int pointX, int pointY)
{
// Determine dimensions of array.
int height = mat.GetLength(0);
int width = mat.GetLength(1);
// Find the minimum and maximum positions bounded by array size. (So we
// don't try to look at cell (-1, -1) when considering the neighbours of
// cell (0, 0) for instance.
int left = Math.Max( pointX - 1, 0 );
int right = Math.Min( pointX + 1, width );
int top = Math.Max( pointY - 1, 0 );
int bottom = Math.Min( pointY + 1, height );
// This is the number of neighbours whose value is 1.
int oneCount = 0;
zeroPositions.Clear();
for( int y = top; y <= bottom; y++ )
{
for( int x = left; x <= right; x++ )
{
if( mat[x, y] == 1 )
{
oneCount++;
}
else if( mat[x, y] == 0 )
{
zeroPositions.Add( new Point( x, y ) );
}
}
}
return oneCount;
}
//...
}
Also I'd really advise you to try not to do too many things in a function. Try making a different function for getting positions of ones and for returning the number of zeros.
I have chess table and my elements are now moving according to rules .But when I drag out of rules my button is disappearing...How I can solve it ???
(red buttons are showing where can I go my elements)
for example knight is moving as rules now (if I don't pass over the red buttons there is no problem)but when I pass over the red places and if I drop there the knight disappears and red places turns back to their original color ( no more red places which indicates where my knight can go ). i tried to make debug but since i am new in c# and debugging i haven't solved the problem. i will be happy if u enlighten my way. how can i solve it? thanks
void btn_DragEnter(object sender, DragEventArgs e)
{
Button button = (Button)sender;
e.Effect = DragDropEffects.Move;
for (int x = 0; x <= 7; x++)
{
for (int y = 0; y <= 7; y++)
{
btn[x, y].Image = null;
if ((x + y) % 2 == 0)
btn[x, y].BackColor = Color.Black;
else
btn[x, y].BackColor = Color.White;
}
}
}
void btn_DragDrop(object sender, DragEventArgs e)
{
Button button = (Button)sender;
button.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
int[] dizi = (int[])button.Tag;
int x = dizi[0];
int y = dizi[1];
for (int a = 0; a <= 7; a++)
{
for (int b = 0; b <= 7; b++)
{
btn[a, b].AllowDrop = false;
}
}
if ((x + 1 >= 0 && y + 2 <= 7) && (y + 2 >= 0 && x + 1 <= 7))
{
btn[x + 1, y + 2].BackColor = Color.Red;
btn[x + 1, y + 2].AllowDrop = true;
}
if ((x + 1 >= 0 && y - 2 <= 7) && (y - 2 >= 0 && x + 1 <= 7))
{
btn[x + 1, y - 2].BackColor = Color.Red;
btn[x + 1, y - 2].AllowDrop = true;
}
if ((x - 1 >= 0 && y + 2 <= 7) && (y + 2 >= 0 && x - 1 <= 7))
{
btn[x - 1, y + 2].BackColor = Color.Red;
btn[x - 1, y + 2].AllowDrop = true;
}
if ((x - 1 >= 0 && y - 2 <= 7) && (y - 2 >= 0 && x - 1 <= 7))
{
btn[x - 1, y - 2].BackColor = Color.Red;
btn[x - 1, y - 2].AllowDrop = true;
}
if ((x + 2 >= 0 && y + 1 <= 7) && (y + 1 >= 0 && x + 2 <= 7))
{
btn[x + 2, y + 1].BackColor = Color.Red;
btn[x + 2, y + 1].AllowDrop = true;
}
if ((x + 2 >= 0 && y - 1 <= 7) && (y - 1 >= 0 && x + 2 <= 7))
{
btn[x + 2, y - 1].BackColor = Color.Red;
btn[x + 2, y - 1].AllowDrop = true;
}
if ((x - 2 >= 0 && y + 1 <= 7) && (y + 1 >= 0 && x - 2 <= 7))
{
btn[x - 2, y + 1].BackColor = Color.Red;
btn[x - 2, y + 1].AllowDrop = true;
}
if ((x - 2 >= 0 && y - 1 <= 7) && (y - 1 >= 0 && x - 2 <= 7))
{
btn[x - 2, y - 1].BackColor = Color.Red;
btn[x - 2, y - 1].AllowDrop = true;
}
}
DoDragDrop is called only when actually dropping the piece. Your logic for determining where the piece can be dropped should be run when starting the drag, in MouseDown or MouseMove, before the call to DoDragDrop. It also seems you are clearing the red buttons in the DragEnter function. That also should be done in the MouseDown/MouseMove function, after the call to DoDragDrop.