[Box2D, C++] Bodies Jittering

Started by
3 comments, last by JoeJ 1 year, 6 months ago

Whenever the camera is moved, all dynamic bodies seem to jitter for some reason.

At render, transforms are converted into pixels with:

SDL_FRect transform_to_dst(const Scene& scene, const Transform& transform, const Renderable& renderable) noexcept {
	const Transform& cam_transform = scene.registry.get<Transform>(scene.get_active_camera());

	SDL_FRect dst{};
	dst.x = ((transform.value.x - transform.value.w) - (cam_transform.value.x - 	cam_transform.value.w)) * renderable.scale_factor;
	dst.y = -((transform.value.y + transform.value.h) - (cam_transform.value.y + cam_transform.value.h)) * renderable.scale_factor;
	dst.w = (transform.value.w * 2) * renderable.scale_factor;
	dst.h = (transform.value.h * 2) * renderable.scale_factor;

	return dst;
}

Then rendered like so:

for (auto [entity, transform, renderable, visuals] : scene.registry.view<Transform, Renderable, Static_visuals>().each()) {
	const SDL_FRect dst{transform_to_dst(scene, transform, renderable)};
	visuals.content.render(renderable.renderer, &dst, util::to_degrees(transform.angle));
}

The two entities I have for testing have a b2PolygonShape with sizes of (0.5, 0.5). Whenever the camera is moved, these two entities seem to shift by about 1 pixel. If I don't give them a b2Body, they stay in their place as they should. Any idea what could be causing this?

Video: https://imgur.com/a/YBLRQGn

Currently the camera's position gets set to the player at every update, but moving the camera on its own results in the same issue.

Edit: disabling the b2Bodies but setting their positions to the spots they would've fallen to still results in the same issue, so the problem doesn't seem to be with Box2D.

Advertisement

its_artem said:
Any idea what could be causing this?

Maybe it's a temporal issue, not a spatial one?
E. g. if physics are on, this causes the boxes to be one frame behind or ahead.

@JoeJ Actually, I tried disabling the b2Bodies and setting their locations to the positions they would have fallen to, and they're still jittering. I guess the issue is with my rendering then, but I don't really see what's wrong with it.

its_artem said:
Actually, I tried disabling the b2Bodies and setting their locations to the positions they would have fallen to, and they're still jittering.

Well, figuring out it's not because of physics is progress.

Just stating the obvious, but now you need to figure out what's the difference between a static tile form the background and a dynamic box.
Ideally you can align a dynamic box exactly over a background tile, so you can log numbers of both to a text file and see for example that world space coords remain equal, but camera space coords diverge slightly.

its_artem said:
I guess the issue is with my rendering then

Maybe somewhere is some rounding going on, to snap things to the screen resolution?
This happens anyway, at latest when the GPU does it's rasterization. But maybe you do it somewhere before on your own too.

Logging numbers should help to figure it out. Basically you need to write temporary debugging code, and the task is not to guess what could cause the problem, but to write debugging code which helps to narrow it down no matter what's the (unknown) cause. Because the problem only shows in a temporal context, you can't use the debugger to step through code as usual. Logging numbers to a file is the better option, because it visualizes how things change over time.

(Obvious, but can't help otherwise)

This topic is closed to new replies.

Advertisement