Page 1 of 2
Physics/Box2D: prismatic joints?
Posted: Tue Apr 04, 2017 2:16 pm
by DireMitten
I'm trying to constrain a body to a line segment, which lies between two other bodies. I'm calling this constraint a "rail". Here is a drawing of what I'm trying to do:
The green circle should move freely along the green arrows, and should not be able to move at all along the red arrow.
So far I'm doing something like this:
Code: Select all
function AttachToRail(rail, bodyB)
local endX = rail.StartBody:getX()
local endY = rail.StartBody:getY()
local axisX = rail.BodyEnd:getX()
local axisY = rail.BodyStart:getY()
local joint = Physics.newPrismaticJoint(
rail.BodyStart, bodyB,
endX, endY,
axisX, axisY
)
end
Which works alright when the "rail" doesn't move. If it does move, the attached body just keeps moving along the axis that was originally designated. Looking at the docs for PrismaticJoints
love2d.org/wiki/PrismaticJoint, I don't see any way of updating the axis.
Does anyone have any ideas as to how I can accomplish my goal? Am I just doing something wrong? Any help would be appreciated. Thanks for reading
Re: Physics/Box2D: prismatic joints?
Posted: Tue Apr 04, 2017 5:09 pm
by ivan
Limiting the length of the joint is done using setLimits.
Keep in mind that by default prismatic joints work without any limits at all, so the variables "endX, endY" may not work as you'd expect.
The next problem you are looking at is that prismatic joints work with 2 bodies.
Lastly although the prismatic joint will try to contain the movement along the predefined axis (axisX, axisY) it is still possible to knock the body off that axis if another large/super-massive/kinematic object collides with it.
Good luck.
Re: Physics/Box2D: prismatic joints?
Posted: Tue Apr 04, 2017 8:55 pm
by airstruck
I don't think you can do this with joints alone. You'll probably need to constantly rotate one of the end bodies so that it always faces the other, and then join that end body to the middle body with a prismatic. You might also want to give the middle body a very low mass so its motion doesn't affect the body it's joined to that much.
Re: Physics/Box2D: prismatic joints?
Posted: Tue Apr 04, 2017 11:01 pm
by raidho36
Try joining the two bodies with prismatic joint but without defining the axis, by omitting these parameters in the function call.
Re: Physics/Box2D: prismatic joints?
Posted: Wed Apr 05, 2017 5:18 am
by airstruck
Prismatic does have special behavior if you define the axis as 0, 0. It keeps both bodies at the same angle, but does not restrict them to an axis as it normally does. This means moving one body will not affect the position of the other. It's roughly the same behavior as a motor joint with high torque and low force.
Unfortunately I think one of the end bodies needs to point toward the other in this scenario, so that a prismatic joint (a regular one with an axis) can join one end body to the middle body along an axis that points toward the other end body. In other words, the angle of one of the end bodies needs to be determined by the relative position of the other, as far as I can tell, and there isn't a joint for that.
Re: Physics/Box2D: prismatic joints?
Posted: Wed Apr 05, 2017 1:11 pm
by DireMitten
Thanks a lot people <3
From your advice and some time to think about it, I've come to a conclusion: I'm going to find another way of doing it. Specifically I'm trying to make a match 3 game sorta deal. You press a directional button to change the "gravity", and when blocks fall into place they get removed and stuff happens.
Instead of using joints, I'm just going to have a grid of "barriers" between where the rails would have been, which will hopefully keep everything in place.
Re: Physics/Box2D: prismatic joints?
Posted: Wed Apr 05, 2017 1:34 pm
by raidho36
You think using physics library for this is inappropriate? That should be trivially accomplished using basic math, and with better results.
Re: Physics/Box2D: prismatic joints?
Posted: Wed Apr 05, 2017 2:58 pm
by DireMitten
I'm still going to be using Box2D/love.physics, but I won't be using any constraints.
Whipping up a solution like I explained (or tried to explain, anyway <.<) was quick and easy. Here is a gif of it working almost as intended:
As you might be able to see, the balls tend to "bounce into each other", which is mostly visible when a lot of balls in the same line hit each other. Unfortunately it's a bit hard to tell since the gif is only running at 20 FPS. It's almost as if they "compress" a little. Would you happen to know why this happens? I'd like for the balls to just stop dead when they hit each other or the walls. I've tried playing around with the restitution and friction a bit, but it doesn't seem to help much. I also run the physics simulation about 100 times per update, which also makes no difference.
Code: Select all
for i = 1, updateResolution do
world:update(dt / updateResolution)
end
Re: Physics/Box2D: prismatic joints?
Posted: Wed Apr 05, 2017 3:03 pm
by raidho36
Again, physics library is not fit for this task. You should use common math. The core of your problem implementing what you want is that you're not using right tools to do the job. It's like trying to hammer a nail using a microscope, and complain that it won't hit the head squarely.
Re: Physics/Box2D: prismatic joints?
Posted: Wed Apr 05, 2017 3:52 pm
by DireMitten
Oh sorry, I misunderstood your previous comment.
Yeah, you're right. But now that I'm getting the hang of Box2D, it's going pretty smoothly. It doesn't have to be perfect, and I feel like I can prototype/iterate faster with it, compared to doing everything from scratch. Thanks for reminding me though.