Бурундук: нет столкновения между фигурами
Это первый раз, когда я прихожу сюда, потому что у меня есть проблема, которую я не могу решить даже после нескольких дней поиска.
Я пытаюсь сделать небольшую фреймворковую игру (и учусь одновременно) с Monogame и Chipmunk-sharp (эта: https://github.com/netonjm/ChipmunkSharp)
Я создал 2 физических объекта, но столкновения между этими двумя объектами нет, хотя они падают под действием силы тяжести.
Мои сценарии:
GameObject
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;
namespace TestPhysic
{
class GameObject
{
public Vector2 Position
{
get { return pos; }
set { pos = value; }
}
protected Vector2 pos;
protected Texture2D text;
protected float angle = 0;
public GameObject(Texture2D firstText,Vector2 startingPos)
{
text = firstText;
pos = startingPos;
}
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(text,pos,null,Color.White,angle,Vector2.Zero,0.25f,SpriteEffects.None,0f);
}
public virtual void Draw(SpriteBatch spriteBatch,float rotation)
{
spriteBatch.Draw(text, pos, null, Color.White, rotation, Vector2.Zero, 0.25f, SpriteEffects.None, 0f);
}
public virtual void Update(GameTime gametime)
{
}
}
}
PhysicObject
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.Active();
shp.SetSensor(true);
shp.SetCollisionType(1);
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, -100), new ChipmunkSharp.cpVect(0.1f,0.1f));
}
}
}
}
Game1.cs (основной класс)
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ChipmunkSharp;
using System;
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));
spc.AddDefaultCollisionHandler().preSolveFunc = new Func<cpArbiter, cpSpace, object, bool>((arg1, arg2, arg3) => { Console.WriteLine("ij"); return true; });
spc.AddDefaultCollisionHandler().beginFunc = new Func<cpArbiter, cpSpace, object, bool>((arg1, arg2, arg3) => { Console.WriteLine("ij"); return true; });
}
/// <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,true);
// 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);
}
}
}
Как я могу решить эту проблему? и почему объекты все еще могут двигаться, даже когда я удаляю строку spc.Step?
большое спасибо