Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Chipmunk Sharp] Body qui passent à travers

    17 novembre 2017 à 22:01:26

    Bonsoir à tous.

    Je suis actuellement en train d'essayer de créer un petit framework avec Monogame et le moteur physique Chipmunk Sharp, j'ai fait un test avec 2 carrés : ils respectent bien la gravité mais ne se rentrent jamais dedans.

    Voici le code des deux classes en question : 

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using ChipmunkSharp;
    
    namespace TestPhysic
    {
        /// <summary>
        /// This is the main type for your game.
        /// </summary>
        public class Game1 : Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
            PhysicObject square;
            PhysicObject ground;
            ChipmunkSharp.cpSpace spc;
            ChipmunkSharp.cpTransform tr = new ChipmunkSharp.cpTransform();
    
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
                spc = new ChipmunkSharp.cpSpace();
                spc.SetGravity(new ChipmunkSharp.cpVect(0, 1f));
            }
    
            /// <summary>
            /// Allows the game to perform any initialization it needs to before starting to run.
            /// This is where it can query for any required services and load any non-graphic
            /// related content.  Calling base.Initialize will enumerate through any components
            /// and initialize them as well.
            /// </summary>
            protected override void Initialize()
            {
                // TODO: Add your initialization logic here
    
                base.Initialize();
            }
    
            /// <summary>
            /// LoadContent will be called once per game and is the place to load
            /// all of your content.
            /// </summary>
            protected override void LoadContent()
            {
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);
    
                square = new PhysicObject(Content.Load<Texture2D>("square"), new Vector2(300,0),spc);
                ground = new PhysicObject(Content.Load<Texture2D>("rose"), new Vector2(250,150),spc);
                
    
                // TODO: use this.Content to load your game content here
            }
    
            /// <summary>
            /// UnloadContent will be called once per game and is the place to unload
            /// game-specific content.
            /// </summary>
            protected override void UnloadContent()
            {
                // TODO: Unload any non ContentManager content here
            }
    
            /// <summary>
            /// Allows the game to run logic such as updating the world,
            /// checking for collisions, gathering input, and playing audio.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            protected override void Update(GameTime gameTime)
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                    Exit();
    
                // TODO: Add your update logic here
                spc.Step(1 / 60);
                square.Update(gameTime);
                ground.Update(gameTime);
                base.Update(gameTime);
            }
    
            /// <summary>
            /// This is called when the game should draw itself.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                // TODO: Add your drawing code here
                spriteBatch.Begin();
                ground.Draw(spriteBatch);
                square.Draw(spriteBatch);
                spriteBatch.End();
    
                base.Draw(gameTime);
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using ChipmunkSharp;
    
    namespace TestPhysic
    {
        class PhysicObject : GameObject
        {
            static int collCount = 0;
            ChipmunkSharp.cpBody physic;
            ChipmunkSharp.cpSpace space;
            ChipmunkSharp.cpShape shp;
            ChipmunkSharp.cpTransform trsf;
    
            public ChipmunkSharp.cpBody Physic
            {
                get { return physic; }
            }
    
            public ChipmunkSharp.cpShape Shape
            {
                get { return shp; }
            }
    
            public PhysicObject(Texture2D firstText, Vector2 startingPos, ChipmunkSharp.cpSpace space,bool isKinematic = false) : base(firstText, startingPos)
            {
                trsf = new cpTransform();
                collCount++;
                physic = new cpBody(1000,cp.PHYSICS_INFINITY);
    
                if (isKinematic)
                    physic.SetBodyType(cpBodyType.KINEMATIC);
                shp = new ChipmunkSharp.cpCircleShape(physic, 100, ChipmunkSharp.cpVect.Zero);
                shp.SetSensor(true);
                shp.Active();
                physic.SetPosition(new ChipmunkSharp.cpVect(Position.X, Position.Y));
                if (space != null)
                {
                    space.AddBody(physic);
                    space.AddShape(shp);
                    this.space = space;
                }
            }
    
            public override void Update(GameTime gametime)
            {
                base.Update(gametime);
                physic.UpdateVelocity(space.GetGravity(), 1,0.01f);
                physic.UpdatePosition(1);
                shp.Update(trsf);
                ChipmunkSharp.cpVect vec = physic.GetPosition();
                Position = new Vector2(vec.x, vec.y);
                angle = (physic.GetAngle() % 360) / 57.2958f;
                if (Position.Y > 200) {            
                    physic.ApplyImpulse(new ChipmunkSharp.cpVect(1, -10), new ChipmunkSharp.cpVect(0.1f,0.1f));
                }
            }
        }
    }

    Une idée pour resoudre ce probleme ? 

    Merci d'avance ;)

    -
    Edité par Smich74 17 novembre 2017 à 22:03:24

    • Partager sur Facebook
    • Partager sur Twitter
    Si c'était facile, tout le monde le ferait.

    [Chipmunk Sharp] Body qui passent à travers

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown