Farseer Physics weird collisions
Asked Answered
L

0

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

Lightening answered 6/3, 2017 at 19:2 Comment(11)
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?Fusion
I removed all Transform.Scale and the result was almost the same: i.imgur.com/m1N7kjT.pngLightening
It looks like it's offset by a fixed amountLightening
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?Fusion
Transform.Scale was 0.5 or so, so that might be what you mean.Lightening
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.Fusion
I have a property called Static in the class, so I want to set the body's type to thatLightening
Circles do seem to work fineLightening
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)?Ecotype
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.Lightening
It has to do with the scale and int castingLatona

© 2022 - 2024 — McMap. All rights reserved.