I was trying to translate something from C++ to C#, now the translation is completely done, however I'm getting an error when compiling that says index of array is out of bounds so I want to know if I declared the array properly.
[Entire Code]
using System;
using SFML.Graphics;
using SFML.Window;
using SFML.Audio;
using SFML.System;
namespace Pruebas_Algoritmia
{
class Program
{
const int num = 8;
static int[,] points = new int[num, 2] {{300, 610},
{ 1270, 430},
{1380,2380},
{1900,2460},
{1970,1700},
{2550,1680},
{2560,3150},
{500, 3300}};
public struct Car
{
public float x;
public float y;
public float speed;
public float angle;
public int n;
public Car(float speed,float angle, int n)
: this()
{
speed = 2;
angle = 0;
n = 0;
}
public void move()
{
x += MathF.Sin(angle) * speed;
y -= MathF.Cos(angle) * speed;
}
public void findTarget()
{
float tx = points[n, 0];
float ty = points[n, 1];
float beta = angle - MathF.Atan2(tx - x, -ty + y);
if (Math.Sin(beta) < 0)
{
angle += 0.005f * speed;
}
else
{
angle -= 0.005f * speed;
}
if ((x - tx) * (x - tx) + (y - ty) * (y - ty) < 25 * 25)
{
n = (n + 1) % num;
}
}
};
static void OnClose(object sender, EventArgs e)
{
RenderWindow window = (RenderWindow)sender;
window.Close();
}
static void Main(string[] args)
{
RenderWindow app = new RenderWindow(new VideoMode(640, 480), "Car Racing Game", Styles.Default);
app.SetFramerateLimit(60);
app.SetVerticalSyncEnabled(true);
app.DispatchEvents();
app.Clear();
Texture t1 = new Texture("c://images/background.png");
Texture t2 = new Texture("c://images/car.png");
t1.Smooth = true;
t2.Smooth = true;
Sprite sBackground = new Sprite(t1);
Sprite sCar = new Sprite(t2);
sBackground.Scale = new Vector2f(2, 2);
sCar.Origin = new Vector2f(22, 22);
float R = 22f;
const int N = 5;
Car[] car = new Car[N];
for (int i = 0; 0 < N; i++)
{
car[i].x = 300 + i * 50;
car[i].y = 1700 + i * 80;
car[i].speed = 7 + i;
}
float speed = 0f;
float angle = 0f;
float maxSpeed = 12.0f;
float acc = 0.2f;
float dec = 0.03f;
float turnSpeed = 0.08f;
float offsetX = 0;
float offsetY = 0;
while (app.IsOpen)
{
Event e;
app.Closed += new EventHandler(OnClose);
app.Display();
}
bool up = false;
bool down = false;
bool right = false;
bool left = false;
if (Keyboard.IsKeyPressed(Keyboard.Key.Up))
{
up = true;
}
if (Keyboard.IsKeyPressed(Keyboard.Key.Right))
{
right = true;
}
if (Keyboard.IsKeyPressed(Keyboard.Key.Down))
{
down = true;
}
if (Keyboard.IsKeyPressed(Keyboard.Key.Left))
{
left = true;
}
//movement
if (up && speed < maxSpeed)
{
if (speed < 0)
{
speed += dec;
}
else
{
speed -= acc;
}
}
if (down && speed >- maxSpeed)
{
if (speed > 0)
{
speed -= dec;
}
else
{
speed -= acc;
}
}
if (!up && !down)
{
if (speed - dec > 0)
{
speed -= dec;
}
else if(speed + dec < 0)
{
speed += dec;
}
else
{
speed = 0;
}
}
if (right && speed != 0)
{
angle += turnSpeed * speed / maxSpeed;
}
if (left && speed != 0)
{
angle -= turnSpeed * speed / maxSpeed;
}
car[0].speed = speed;
car[0].angle = angle;
for (int i = 0; i < N; ++i)
{
car[i].move();
}
for (int i = 0; i < N; ++i)
{
car[i].findTarget();
}
for (int i = 0; i < N; ++i)
{
for(int j = 0; i < N; ++j)
{
float dx = 0;
float dy = 0;
while (dx * dx + dy * dy < 4 * R * R)
{
car[i].x += dx / 10.0f;
car[i].x += dy / 10.0f;
car[j].x += dx / 10.0f;
car[j].x += dy / 10.0f;
dx = car[i].x - car[j].x;
dy = car[i].y - car[j].y;
}
}
}
app.Clear(Color.White);
if (car[0].x > 320) offsetX = car[0].x - 320;
if (car[0].y > 240) offsetY = car[0].y - 240;
//sBackground
sBackground.Position = new Vector2f(-offsetX, -offsetY);
app.Draw(sBackground);
Color[] colors = new Color[] { Color.Red, Color.Green, Color.Magenta, Color.Blue, Color.White};
for (int i = 0; i < N; ++i)
{
sCar.Position = new Vector2f(car[i].x - offsetX, car[i].y - offsetY);
sCar.Rotation = car[i].angle * 180 / 3.141593f;
sCar.Color = colors[i];
app.Draw(sCar);
}
app.Display();
}
}
}
I'm getting the error around line 88 where I have this.
const int N = 5;
Car[] car = new Car[N];
for (int i = 0; 0 < N; i++)
{
car[i].x = 300 + i * 50;
car[i].y = 1700 + i * 80;
car[i].speed = 7 + i;
}
Originally this looked like this in C++
const int N=5;
Car car[N];
for(int i=0;i<N;i++)
{
car[i].x=300+i*50;
car[i].y=1700+i*80;
car[i].speed=7+i;
}
The error I get literally says the index is out of bounds, also I'm getting an exception after this for loop saying that there's unreachable code for every line below the loop
You made a typo in your C# code. You wrote
0 < Ninstead ofi < N.