## Reset/jump constraint to the initial position

maiklof
Posts: 22
Joined: Wed Nov 20, 2013 10:01 am

### Reset/jump constraint to the initial position

Hi,
I am using Generic6DofSpring2 constraints and I would like to find an easy way to reset the constraint to the initial position.
It is a quite particular case in which the parent body is static and the child (connected with the constraint) is dynamic (mass>0).
I am running the constraint with a motor, in velocity mode, so I can move the child in one axis (let's say x) from 0m to 1m:
[PARENT]
|<---------------------[CHILD]----------------------->|
0m 1m
What I want to achieve is the child to automatically jump to initial position (0m) if velocity is positive and it arrives to the upper limit. Kind of a loop effect. Important to mention that I want this to happen between two simulation steps!
The hard way, I guess, is doing the following:
- Detect when the child is at upper limit and velocity is >0.
- Then remove the constraint
- Move the child body to the initial position
- Set a new constraint and apply the velocity.

Is there any other way?
Last edited by maiklof on Mon Feb 20, 2023 2:09 pm, edited 1 time in total.
drleviathan
Posts: 839
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

### Re: Reset/jump constraint to the initial position

Let me repeat it back to you to make sure I understand:

(1) You have two objects A and B connected by a constraint C.

(2) You want to be able to reset the transforms of the two objects such that the constraint is satisfied (e.g. objects A and B are experiencing zero torque/force from the constraint C).

If item (2) is NOT correct please supply a different description.

If (2) is correct then I think the way to do the math is as follows (I did not try it, but this is what I would try first):

(3) I assume you want to leave object A alone and move object B. If this isn't correct please say so.

The btGeneric6DofSpring2Constraint C has two protected data members:

C.m_frameInA
C.m_frameInB

I think the constraint is satisfied when all vectors in either of those two frames are transformed to identical world-frame vectors through the two objects A and B. In other words, when world_frame_A == world_frame_B using the two relations below:

world_frame_A = A.m_worldTransform. * m_frameInA
world_frame_B = B.m_worldTransform * m_frameInB

Note, I'm using the convention that the transforms operate from the left on hypothetical vectors on the right.

When the two frames are equal you get this equation:

A.m_worldTransform * C.m_frameInA = B.m_worldTransform * C.m_frameInB

Solving for B.m_worldTransform we get:

B.m_worldTransform = A.m_worldTransform * C.m_frameInA * inverse(C.m_frameInB)
maiklof
Posts: 22
Joined: Wed Nov 20, 2013 10:01 am

### Re: Reset/jump constraint to the initial position

Thanks for your reply! I have changed my previous description to be more clear on what I want to achieve, I think I did not express myself properly the first time.
drleviathan
Posts: 839
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

### Re: Reset/jump constraint to the initial position

Slamming the transform of the child object doesn't just work?

Code: Select all

``child_body->setWordTransform(child_initial_transform);``