Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

If somebody wanted to develop a system between two intersecting rectangles so that the rectangles would, in a gradual process, push eachother away from one another until no longer intersecting, with the repelling force being stronger depending upon how deep the intersection is... what would the math look like?

share|improve this question
    
Should they accelerate until not intersecting? And should they slow down gradually to stop just when they stop intersecting? –  Mikael Högström Sep 27 '12 at 21:39
    
Yes, they should accelerate until not intersecting- as far as slowing down once they stop intersecting, the world's friction would take care of that once acceleration is no longer applied to the rectangles from their intersection. –  TheBroodian Sep 27 '12 at 22:02
add comment

1 Answer

up vote 11 down vote accepted
a:Object;
b:Object;

dx:Number = a.x - b.x; //distance by x
dy:Number = a.y - b.y;
distance:Number = Math.sqrt ( dx*dx + dy*dy );

If you want to simulate magnet behavior, you want to base forces on distance between them. Physics say so:

formula for magnetic force

But you will be fine with:

force = Math.floor ( MAX_FORCE / distance );

And then you need to use trigonometry to apply force as velocity change:

var angle:Number = Math.Atan2 ( dy, dx );
var x_speed:Number = force * Math.cos ( angle );
var y_speed:Number = force * Math.sin ( angle );

a.vx += x_speed;
a.vy += y_speed;
b.vx -= x_speed;
b.vy -= y_speed;

//and of course a.x += a.vx etc.
share|improve this answer
    
I just want you to know, sir, that you are wonderful. –  TheBroodian Sep 27 '12 at 21:09
1  
You show wonderfulness with +1s. –  Byte56 Sep 27 '12 at 21:13
    
After implementing this to my game, it -almost- works, but I am experiencing an issue: Object1 is a player character and object2 is a dummy box that does nothing other than sit in place unless outside forces are applied to it; when object1 is moved into object2, and presses too far into object2, first it stops moving (desirable considering its intersection has gone too far, and is being repelled too hard to continue pushing any further) but then when object1 stops accelerating into object2, object1 is repelled out of object2, but then object2 sort of 'sticks' to object1, and they both scoot –  TheBroodian Sep 27 '12 at 22:12
    
and they both scoot away in the direction opposite what object1 was originally pushing from to begin with. I have no idea what would cause this behavior? –  TheBroodian Sep 27 '12 at 22:14
2  
Never mind, thank you again for your help and willingness to have a look at my code. My aforementioned problem was due to my own faulty handling of friction, I debugged and located it and handled it. Everything works great, thanks again! :) –  TheBroodian Sep 27 '12 at 22:32
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.