8

I'm starting a game, and I'm using Farseer as the physics engine.

I have setup a few boxes acting as terrain tiles, and a box as the player. The collisions between these two are very weird, they intersect and slide when it's not a "direct" hit.

Here's a GIF of what's happening: http://i.imgur.com/HR0J0D3.gifv

And here are the snippets:

//Creating the body
PhysBody = BodyFactory.CreateRectangle(
    Transform.World.PhysWorld,
    ConvertUnits.ToSimUnits(Width * Transform.Scale),
    ConvertUnits.ToSimUnits(Height * Transform.Scale),
    5,
    ConvertUnits.ToSimUnits(Transform.Position.ToVector2()));

//The tiles are static, the player is not
PhysBody.BodyType = Static ? BodyType.Static : BodyType.Dynamic;

//Drawing the test rectangles
ShapeRenderer.Instance.DrawRectangle(
    sb,
    ConvertUnits.ToDisplayUnits(PhysBody.Position).ToVector2(),
    new Size2((int)(Width * Transform.Scale), (int)(Height * Transform.Scale)),
    Color.White);

I don't think the rest is important, all I did was create a world with 9.8 gravity.

I have also tried without using Transform.Scale and with just one box below the player, and the problem still happens.

The graphics engine I'm using is Ultraviolet

Felipe
  • 336
  • 8
  • 19
  • 1
    It almost appears that the physics engine is using different box dimensions. Can you verify that `Transform.Scale` is being applied correctly both to physics and rendering? – Basic Mar 06 '17 at 19:08
  • I removed all Transform.Scale and the result was almost the same: http://i.imgur.com/m1N7kjT.png – Felipe Mar 06 '17 at 19:10
  • 1
    It looks like it's offset by a fixed amount – Felipe Mar 06 '17 at 19:10
  • Comparing the gif with the png linked in comments, the heights are different (in absolute pixels). Not sure if that's an image scaling thing, but the boxes also have a different aspect ratio. Perhaps it's a fraction of the collider size? – Basic Mar 06 '17 at 19:13
  • `Transform.Scale` was 0.5 or so, so that might be what you mean. – Felipe Mar 06 '17 at 19:15
  • I think that was it. I was trying to determine if the offset varied when scale did and if so, was the variance proportional to the scale - it might've helped narrow down which bit of the code was off. I can't see anything obviously wrong with your code from inspection (although I'm not certain you meant to assign instead of compare in `PhysBody.BodyType = Static ? ... : ...;`). Although I've used other physics engines, I'm not familiar with farseer so don't think I can help much. I did find someone else with a similar issue but no solution. – Basic Mar 06 '17 at 19:53
  • I have a property called `Static` in the class, so I want to set the body's type to that – Felipe Mar 06 '17 at 20:01
  • Circles do seem to work fine – Felipe Mar 06 '17 at 20:20
  • It almost seems to me like your collision is checking at a single corner instead of checking to make sure that both corners would have touched the terrain (it loks like the check is only happening on the left corner not both). Does the sliding happen when the box is hanging over the right side of the terrain, the left side of the terrain, both sides(the terrain is smaller than the width of the character)? – Ansel Faillace Mar 15 '17 at 14:33
  • Doing more testing I discovered that there is a kind of weird invisible circular shape on the bottom-right corner of the rectangle, but I have explicitly created a rectangle, so I have no idea of why it may be doing that. – Felipe Mar 15 '17 at 18:28
  • It has to do with the scale and int casting – Emily Mar 21 '17 at 09:15

0 Answers0