Re: Bouncing circles, simple




"Mike Austin" <no@xxxxxxxx> wrote in message
news:rRW_e.92036$qY1.63316@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> cr88192 wrote:
>> "Mike Austin" <no@xxxxxxxx> wrote in message
>> news:Z8l_e.328012$5N3.67275@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>>
>>>It's not very hard and I've done it before long ago, but I can't seem to
>>>get two circles to bounce off each other correctly. All I need is the
>>>incident angle, and the angle between it and the velocity vector,
>>>correct?
>>>
>>>I did some Googling, but everybody seems to write the math a little
>>>different so I can't get a good grasp on it. The code below adds
>>>relative velocity to simplify the math, I'll convert to absolute when I
>>>understand it better.
>>>
>>>Thanks,
>>>Mike
>>>
>>>
>>>// The relative position of two objects
>>>delta = obj_a.pos - obj_b.pos
>>>
>>>// The angle between two objects
>>>angle = atan2( delta y, delta x )
>>>
>>>// Calculate momentum transfered
>>>speed = obj_a.vel.length * dot( delta, obj_a.vel ).normalize
>>>
>>>// Transfer momentum from obj_a to obj_b
>>>obj_a.vel -= vector( angle.cos * speed, angle.sin * speed )
>>>obj_b.vel += vector( angle.cos * speed, angle.sin * speed )
>>>
>>>// Do the same for obj_b to obj_a
>>>[...]
>>
>>
>> hmm, it is curious what exactly is going on to allow the code to be
>> written like this (or, it is some form of psuedocode...).
>
> It's converted from Io to pseudo-c.
>
ok.

>> ok, you probably don't want to do this as 2 steps:
>> A->B then B->A, because in my experience (especially when working
>> directly with an object's velocity, vs an impulse or such) this can lead
>> to poor behavior.
>
> I would like to do it in two steps because I also need to calculate force
> between them (1 / d^2), and it simplifies the code.
>
ok.

though possible, one may need to be a little more careful I would guess.

<snip>
>
> I don't know why you're using e or what restitution is supposed to do.
> Here's my revised pseudo-code, but I'm not sure it conserves momentum -
> "object_a velocity length + object_b velocity length" should be equal
> before and after the collision, correct?
>
e is mostly just what I used before (originally I think it came from one of
the papers I read). however, the notion was not new to me, just, previously,
I had not bothered to come up with a good name for it anyways (it was stuck
in one of my general purpose 'f' or 'g' variables).

generally, it refers to how much of a "bounce force" to add. otherwise, if
all is working well, the objects should not bounce (inelastic collisions).
I am, however, not sure on whether or not the laws of conservation still
hold (whenever there is a collision, a little fudging is made to the
forces). reality works a little different on this front, and typically
physical realism was put at a lower priority than "visual realism".

> relative_position = object_a.position - object_b.position
> incident_angle = atan2( relative_position.y, relative_position.x )
> velocity_angle = atan2( object_a.velocity.y, object_a.velocity.x )
> magnitude := object_a.velocity length * cos( incident_angle -
> velocity_angle )
>
> object_a.velocity -= vector( cos( incident_angle ) * magnitude,
> sin( incident_angle ) * magnitude )
> object_b.velocity += vector( cos( incident_angle ) * magnitude,
> sin( incident_angle ) * magnitude )
>
> [calc object_b]
>
yeah, it might work.
if one does not need mass or other features, this could be ok.

still, worth noting:
though subtle, I still view "impulses" as an important feature.
instead of applying forces to velocity directly, they accumulate them and
add them at the end of the frame.

something like:
object_a.impulse -= vector( cos( incident_angle ) * magnitude,
sin( incident_angle ) * magnitude )
object_b.impulse += vector( cos( incident_angle ) * magnitude,
sin( incident_angle ) * magnitude )

and after all the physics are done:
object.velocity += object.impulse
object.impulse = vector(0, 0)

the effects of this are related to multiple contacts.

in the multiple contact case, often, one collision will prevent correctly
handling another, and so forth (a simple example of this would be, in the
case of multiple contacts, some contacts having little effect on an objects
movement, so during that time, eg, an object will head right through another
or other weird effects).

otherwise, it is not that expensive to add them.


.



Relevant Pages

  • Re: Bying floats
    ... dark sense of humour in our maths group and would give us questions ... velocity very quickly, bounce and usually walk away. ... A cow had just fallen off the top of the cliff at the middle ...
    (uk.rec.sheds)
  • Re: Collision detection
    ... top and bottom as well as "bounce" off each other. ... Not entirely sure what you mean by elastic here but the collision ... I have attempted the collision detection but ... see if any point is included in both rectangles, ...
    (microsoft.public.vb.general.discussion)
  • Re: Is ther in truth, no mass???
    ... A skateboarder is moving at 7.83 m/s along a horizontal section of a track that is slanted upward by 47.1 ° above the horizontal at its end, which is 0.768 m above the ground. ... Assume that the only force acting on him during the collision is that due to the ground. ... The swimmer then runs off the raft horizontally with a velocity of +5.87 m/s relative to the shore. ... Puck A has a mass of 0.0210 kg and is moving along the x axis with a velocity of +6.45 m/s. ...
    (sci.physics)
  • Re: Mechanics: problem of falling stick by I. E. Irodov
    ... then its velocity is not altered. ... here's the geometry just before the collision: ... So the collision can be treated for each mass ... by its total momentum and the angular momentum about the center of mass. ...
    (sci.physics.research)
  • Re: Collision detection
    ... Collision detection is made simpler if you track each object's center position. ... amount of overlap and determining that the lesser overlap ... determines which way the bounce should happen. ... it has overlap in both directions. ...
    (microsoft.public.vb.general.discussion)