忍者ブログ

Memeplexes

プログラミング、3DCGとその他いろいろについて

XNAで物理シミュレーション(JigLibX) その2

今回は箱を描画するだけです。
よくわかっている人は飛ばしてください。

Xna部分の変更だけでJigLibX部分には変更ありません。

プログラム

BasicWorldGame.cs
using Microsoft.Xna.Framework;
using JigLibX.Physics;
using JigLibX.Collision;

public class BasicWorldGame : Game
{
    GraphicsDeviceManager graphics;
    PhysicsSystem world;
    BoxRenderer boxRenderer;

    public BasicWorldGame()
    {
        graphics = new GraphicsDeviceManager(this);
        InitializePhysics();
    }

    private void InitializePhysics()
    {
        world = new PhysicsSystem();
        world.CollisionSystem = new CollisionSystemSAP();
    }

    protected override void LoadContent()
    {
        boxRenderer = new BoxRenderer(GraphicsDevice);
    }

    protected override void UnloadContent()
    {
        boxRenderer.Dispose();
    }

    protected override void Update(GameTime gameTime)
    {
        world.Integrate(1 / 60f);

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
        boxRenderer.SetCamera(
            Matrix.CreateLookAt(
                new Vector3(2, 1, 5),
                new Vector3(),
                Vector3.Up
                ),
            Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45),
                GraphicsDevice.Viewport.AspectRatio,
                0.1f, 1000
                )
            );
        boxRenderer.Draw(Matrix.Identity);
        base.Draw(gameTime);
    }
}

BoxRenderer.cs
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

class BoxRenderer:System.IDisposable
{
    public GraphicsDevice GraphicsDevice { get; private set; }
    VertexBuffer vertexBuffer;
    BasicEffect basicEffect;

    public BoxRenderer(GraphicsDevice graphicsDevice)
    {
        GraphicsDevice = graphicsDevice;
        basicEffect = new BasicEffect(GraphicsDevice) { VertexColorEnabled = true};

        Vector3[] positions = new []
        {
            new Vector3(1, 1, 1),
            new Vector3(1, -1, 1),
            new Vector3(-1, -1,1),
            new Vector3(-1, 1, 1),

            new Vector3(1, 1, -1),
            new Vector3(1, -1, -1),
            new Vector3(-1, -1,-1),
            new Vector3(-1, 1, -1),
        };
        VertexPositionColor[] vertices = new[]
        {
            new VertexPositionColor(positions[0], Color.Blue),
            new VertexPositionColor(positions[1], Color.Blue),
            new VertexPositionColor(positions[2], Color.Blue),

            new VertexPositionColor(positions[3], Color.Blue),
            new VertexPositionColor(positions[0], Color.Blue),
            new VertexPositionColor(positions[2], Color.Blue),

            
            new VertexPositionColor(positions[3], Color.Red),
            new VertexPositionColor(positions[2], Color.Red),
            new VertexPositionColor(positions[7], Color.Red),

            new VertexPositionColor(positions[7], Color.Red),
            new VertexPositionColor(positions[2], Color.Red),
            new VertexPositionColor(positions[6], Color.Red),
            

            new VertexPositionColor(positions[0], Color.Green),
            new VertexPositionColor(positions[3], Color.Green),
            new VertexPositionColor(positions[7], Color.Green),

            new VertexPositionColor(positions[0], Color.Green),
            new VertexPositionColor(positions[7], Color.Green),
            new VertexPositionColor(positions[4], Color.Green),


            new VertexPositionColor(positions[4], Color.Cyan),
            new VertexPositionColor(positions[6], Color.Cyan),
            new VertexPositionColor(positions[5], Color.Cyan),

            new VertexPositionColor(positions[4], Color.Cyan),
            new VertexPositionColor(positions[7], Color.Cyan),
            new VertexPositionColor(positions[6], Color.Cyan),


            new VertexPositionColor(positions[5], Color.Magenta),
            new VertexPositionColor(positions[2], Color.Magenta),
            new VertexPositionColor(positions[1], Color.Magenta),

            new VertexPositionColor(positions[5], Color.Magenta),
            new VertexPositionColor(positions[6], Color.Magenta),
            new VertexPositionColor(positions[2], Color.Magenta),


            new VertexPositionColor(positions[0], Color.Yellow),
            new VertexPositionColor(positions[5], Color.Yellow),
            new VertexPositionColor(positions[1], Color.Yellow),

            new VertexPositionColor(positions[0], Color.Yellow),
            new VertexPositionColor(positions[4], Color.Yellow),
            new VertexPositionColor(positions[5], Color.Yellow),
        };

        vertexBuffer = new VertexBuffer(
            graphicsDevice,
            typeof(VertexPositionColor),
            vertices.Length,
            BufferUsage.WriteOnly
            );
        vertexBuffer.SetData<VertexPositionColor>(vertices);
    }

    public void SetCamera(Matrix view, Matrix projection)
    {
        basicEffect.View = view;
        basicEffect.Projection = projection;
    }

    public void Draw(Matrix world)
    {
        basicEffect.World = Matrix.CreateScale(1 / 2f) * world;
        basicEffect.CurrentTechnique.Passes[0].Apply();


        GraphicsDevice.SetVertexBuffer(vertexBuffer);
        GraphicsDevice.DrawPrimitives(
            PrimitiveType.TriangleList,
            0, 
            vertexBuffer.VertexCount / 3
            );
    }

    public void Dispose()
    {
        basicEffect.Dispose();
        vertexBuffer.Dispose();
    }
}


このプログラムは斜め上から見た箱を描きます。

JigLibXDemo02.jpg


まだシミュレーターと結びつけていないので、動きません。











拍手[0回]

PR