Développement XNA pour la XBox et le PC
226
de type Vector2 est donc le deuxième paramètre attendu par la méthode. Dans le cas
présent, il est choisi aléatoirement de manière à ce que la rotation ainsi subie par le corps
ne soit pas la même pour tous les astéroïdes.
Cette fois encore, n’oubliez pas de toujours coordonner position et rotation entre le sprite
et le corps !
Les astéroïdes utilisent eux aussi le moteur physique
class Asteroid : Sprite
{
static Random random = new Random(DateTime.Now.Millisecond);
Body body;
public Asteroid(PhysicsSimulator physicsSimulator, ContentManager content)
: base(Vector2.Zero)
{
base.LoadContent(content, "asteroid2");
body = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 32, 32, 1);
Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
Initialize();
}
private void Initialize()
{
body.Position = new Vector2(random.Next(0, ChapitreDix_2.SCREEN_WIDTH -
➥Texture.Width), -Texture.Height);
body.ApplyForce(new Vector2(random.Next(-3, 3) * 1000, random.Next(-3, 3) *
➥1000));
body.ApplyForceAtLocalPoint(new Vector2(random.Next(-3, 3) * 100,
➥random.Next(-3, 3) * 100), new Vector2(random.Next(0, Texture.Width),
➥random.Next(0, Texture.Height)));
Position = body.Position;
Rotation = body.Rotation;
}
public void Update(GameTime gameTime)
{
Position = body.Position;
Rotation = body.Rotation;
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))
=Labat FM.book Page 226 Vendredi, 19. juin 2009 4:01 16
226
de type Vector2 est donc le deuxième paramètre attendu par la méthode. Dans le cas
présent, il est choisi aléatoirement de manière à ce que la rotation ainsi subie par le corps
ne soit pas la même pour tous les astéroïdes.
Cette fois encore, n’oubliez pas de toujours coordonner position et rotation entre le sprite
et le corps !
Les astéroïdes utilisent eux aussi le moteur physique
class Asteroid : Sprite
{
static Random random = new Random(DateTime.Now.Millisecond);
Body body;
public Asteroid(PhysicsSimulator physicsSimulator, ContentManager content)
: base(Vector2.Zero)
{
base.LoadContent(content, "asteroid2");
body = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 32, 32, 1);
Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
Initialize();
}
private void Initialize()
{
body.Position = new Vector2(random.Next(0, ChapitreDix_2.SCREEN_WIDTH -
➥Texture.Width), -Texture.Height);
body.ApplyForce(new Vector2(random.Next(-3, 3) * 1000, random.Next(-3, 3) *
➥1000));
body.ApplyForceAtLocalPoint(new Vector2(random.Next(-3, 3) * 100,
➥random.Next(-3, 3) * 100), new Vector2(random.Next(0, Texture.Width),
➥random.Next(0, Texture.Height)));
Position = body.Position;
Rotation = body.Rotation;
}
public void Update(GameTime gameTime)
{
Position = body.Position;
Rotation = body.Rotation;
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))
=Labat FM.book Page 226 Vendredi, 19. juin 2009 4:01 16
