Physics/Box2D: prismatic joints?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
DireMitten
Prole
Posts: 9
Joined: Thu Jun 23, 2016 3:07 pm

Physics/Box2D: prismatic joints?

Post 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: Image 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 :awesome:
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Physics/Box2D: prismatic joints?

Post 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.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Physics/Box2D: prismatic joints?

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Physics/Box2D: prismatic joints?

Post by raidho36 »

Try joining the two bodies with prismatic joint but without defining the axis, by omitting these parameters in the function call.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Physics/Box2D: prismatic joints?

Post 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.
DireMitten
Prole
Posts: 9
Joined: Thu Jun 23, 2016 3:07 pm

Re: Physics/Box2D: prismatic joints?

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Physics/Box2D: prismatic joints?

Post by raidho36 »

You think using physics library for this is inappropriate? That should be trivially accomplished using basic math, and with better results.
DireMitten
Prole
Posts: 9
Joined: Thu Jun 23, 2016 3:07 pm

Re: Physics/Box2D: prismatic joints?

Post 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: Image

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
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Physics/Box2D: prismatic joints?

Post 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.
DireMitten
Prole
Posts: 9
Joined: Thu Jun 23, 2016 3:07 pm

Re: Physics/Box2D: prismatic joints?

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests