Collisions et physique : créer un simulateur de vaisseau spatial
CHAPITRE 10
225
public void Update(GameTime gameTime)
{
Position = body.Position;
Rotation = body.Rotation;
if (ServiceHelper.Get().IsKeyDown(Keys.Left))
body.Rotation -= 0.05f;
if (ServiceHelper.Get().IsKeyDown(Keys.Right))
body.Rotation += 0.05f;
if (ServiceHelper.Get().IsKeyDown(Keys.Up))
body.ApplyForce(new Vector2((float)Math.Sin(body.Rotation) * 100,
➥(float)Math.Cos(body.Rotation) * -100));
if (ServiceHelper.Get().IsKeyDown(Keys.Down))
body.ApplyForce(new Vector2((float)Math.Sin(body.Rotation) * -100,
➥(float)Math.Cos(body.Rotation) * 100));
if (body.Position.X > ChapitreDix_2.SCREEN_WIDTH + (2 * Texture.Width))
body.Position = new Vector2(-Texture.Width, body.Position.Y);
if (body.Position.X < 0 - (2 * Texture.Width))
body.Position = new Vector2(ChapitreDix_2.SCREEN_WIDTH + Texture.Width,
➥body.Position.Y);
if (body.Position.Y > ChapitreDix_2.SCREEN_HEIGHT + (2 * Texture.Height))
body.Position = new Vector2(body.Position.X, -Texture.Height);
if (body.Position.Y < 0 - (2 * Texture.Height))
body.Position = new Vector2(body.Position.X, ChapitreDix_2.SCREEN_HEIGHT
➥+ Texture.Height);
}
public void ResetPosition()
{
body.Position = new Vector2(ChapitreDix_2.SCREEN_WIDTH / 2 - (Texture.Width
➥/ 2), 9 * (ChapitreDix_2.SCREEN_HEIGHT / 10) - Texture.Height);
Position = body.Position;
body.Rotation = 0;
Rotation = body.Rotation;
Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
}
}
En ce qui concerne la classe Asteroid, il faut appliquer deux forces. La première utilise la
méthode ApplyForce() que nous venons de voir (la direction sera aléatoire). La seconde
utilise la méthode ApplyForceAtLocalPoint() qui, comme son nom l’indique, vous permet
d’appliquer une force à un point précis de votre corps. Ce point, déterminé par un objet
=Labat FM.book Page 225 Vendredi, 19. juin 2009 4:01 16
CHAPITRE 10
225
public void Update(GameTime gameTime)
{
Position = body.Position;
Rotation = body.Rotation;
if (ServiceHelper.Get
body.Rotation -= 0.05f;
if (ServiceHelper.Get
body.Rotation += 0.05f;
if (ServiceHelper.Get
body.ApplyForce(new Vector2((float)Math.Sin(body.Rotation) * 100,
➥(float)Math.Cos(body.Rotation) * -100));
if (ServiceHelper.Get
body.ApplyForce(new Vector2((float)Math.Sin(body.Rotation) * -100,
➥(float)Math.Cos(body.Rotation) * 100));
if (body.Position.X > ChapitreDix_2.SCREEN_WIDTH + (2 * Texture.Width))
body.Position = new Vector2(-Texture.Width, body.Position.Y);
if (body.Position.X < 0 - (2 * Texture.Width))
body.Position = new Vector2(ChapitreDix_2.SCREEN_WIDTH + Texture.Width,
➥body.Position.Y);
if (body.Position.Y > ChapitreDix_2.SCREEN_HEIGHT + (2 * Texture.Height))
body.Position = new Vector2(body.Position.X, -Texture.Height);
if (body.Position.Y < 0 - (2 * Texture.Height))
body.Position = new Vector2(body.Position.X, ChapitreDix_2.SCREEN_HEIGHT
➥+ Texture.Height);
}
public void ResetPosition()
{
body.Position = new Vector2(ChapitreDix_2.SCREEN_WIDTH / 2 - (Texture.Width
➥/ 2), 9 * (ChapitreDix_2.SCREEN_HEIGHT / 10) - Texture.Height);
Position = body.Position;
body.Rotation = 0;
Rotation = body.Rotation;
Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
}
}
En ce qui concerne la classe Asteroid, il faut appliquer deux forces. La première utilise la
méthode ApplyForce() que nous venons de voir (la direction sera aléatoire). La seconde
utilise la méthode ApplyForceAtLocalPoint() qui, comme son nom l’indique, vous permet
d’appliquer une force à un point précis de votre corps. Ce point, déterminé par un objet
=Labat FM.book Page 225 Vendredi, 19. juin 2009 4:01 16
