Attraction point

Hi !

I'm working on a new game and I wanted to create, using box2D, some Attract Points.
I mean points that grab the bodies within a certain area (a circle) so that they don't move once caught.

For that I use a sensor static body which shape is a circle and for each collision with the circle, I calculate a force aimed to the center of this circle as below :
Code:
         Contact c = ce.contact;
         Body b = null;
         
         if(c.isTouching())
         {
            if(c.getFixtureA().getBody() != m_body) b = c.getFixtureA().getBody();
            else if(c.getFixtureB().getBody() != m_body) b = c.getFixtureB().getBody();
            
            Vec2 dir = (new Vec2(m_x, m_y)).add(b.getPosition().negateLocal());
            dir = dir.mul(m_f/m_r); //m_f and m_r are forces and radius set.
            
            b.applyForce(dir, b.getPosition());
         }


But still, it doesn't work… When I release a circle shaped body above this point it is affected by the force I apply but it still goes down in the end.

What am I doing wrong ?

Thanks !
You mean the force you are applying is not strong enough to overcome gravity? Have you tried making it bigger?
Yes I tried but it didn't seem to change… Even with force set to 50.0f… The ball seem to go too fast to be stopped.
I wondered if it the radius wasn't large enough so i tried with a radius twice as large as the ball's but there is still only 5 contacts detected (thanks to the log I can see that).

I changed the making of the vect to the code below : (it is more logical according to physical attraction forces)
Code:
Vec2 dir = (new Vec2(m_x, m_y)).add(b.getPosition().negateLocal());
float d = dir.length();
dir.normalize();
dir = dir.mul(m_f*(m_r - d)/m_r);

It works a bit better (there are oscillations) but the ball still goes down eventually not matter what the force is…

There is obviously something wrong, if I put m_f to 500.0f the last force has 845 as length which shouldn't happen right ?
As d should be less than m_r (if it wasn't then there shouldn't be any contact right ?) so (m_r - d)/m_r shouldn't be between 0 and 1 ?

Thanks

Edit : Yes there is a problem here… I have m_r = 0.75 but I have a contact for b > 2 and that's normal in a way, when there is a contact the center of the ball isn't necessarily in the circle… I tried applying the force only if d < m_r but that's worse…

Have you any idea how should I do to get what I want ? Thanks !
(That is to mean I want some attract points on which my body will reach equilibrium soon as if they were on the ground, once attracted they don't move unless we apply other forces to them).
Maybe you could try canceling gravity (by applying a force upwards every timestep) on the body when it's in range of the attracting point.
I tried but no better…

I also tried in another way with :
Code:
Vec2 pos = b.getPosition();
            
float x = m_x - pos.x, y = m_y - pos.y;
float d = x*x + y*y;
            
float k = d/m_r;
            
Vec2 vec = new Vec2(k*m_f*x/d, k*m_f*y/d);
b.applyForce(vec, pos);


Which works a little bit better but it is still not what I want… No matter the value I give to the force, it doesn't stop oscillating.
Furthermore, it is not a true central force… The force should be greater when approaching the center of the attraction circle.

Maybe I should try to find a complete other way to do what I want since I don't actually want to reproduce a gravitational force.

Do you have any idea ?

Thanks again.
Maybe you could slow it down a little by setting some linear damping, or just by applying a drag force (small force in opposite direction to movement every time step).

oh... 1000 posts 🤓
You mean I should do that all the time or only when there is contact ?

Edit : Well, I was thinking a bit and it seems that what I want is not really applicable (since it not something we really encounter in real life).
So I though i might reconsider the way I think it.

Instead of an attract point, what I actually want is a hang point in a way. Something on which my character (this is a ball) could hand on. But if it got hit by something then he might fall from it so putting it static is not still the solution.
Is there a way to do so ?

I wondered if it was possible with joints… Is there a breakable joint in box2D (I mean which breaks under pressure -- ie. if the hang point is moving and goes through a hole too tight for the ball to go through I'd like the ball to fall from the hang point) ?

Thanks !

Since it nos the goal of this topic I might open a new one.

Original thread: https://box2d.org/forum/viewtopic.php?t=8406