[PD] bouncing

marius schebella marius.schebella at gmail.com
Tue Oct 16 02:12:09 CEST 2007

```thanks, Uğur,
I like your diving into math, but actually I have more calculations than
just the bouncing, so there will be also interaction with the objects
and I will not write maths for those too. so I will stick with pyode, I
think it makes life a little easier...
marius.

Uğur Güney wrote:
> # I read the first mail and find an algorithm for the special case of
> "moving point particles in  a 2D pentagon boundary". The answer is given as
> using pyode script but I am excited about my solution and want to share it
> with you :-)
> # The easy part of using a rectangular boundary (of which sides are parallel
> to the axes) is that, one should only check if x position of a particle is
> greater than x_max (coordinate of the right side of the rectangle), if
> x<x_min, y<y_min and y>y_max. And if it exceeds the boundary in the next
> step of the simulation according to ODE (or any kind of calculation) of the
> simulation, then reverse the velocity in that direction (and mirror the
> coordinate using the boundary line as the axis of symmetry).
> # So, We can try to use this simple idea in pentagon case.
> # We can divide the pentagon in five triangles: Put a point in the center
> and draw lines to vertexes.
> http://www.math.union.edu/~dpvc/courses/1999-00/MTH012-02-WI00/notes/DividedPentagon.jpg<http://www.math.union.edu/%7Edpvc/courses/1999-00/MTH012-02-WI00/notes/DividedPentagon.jpg>
> # Call the bottom triangle 0th triangle, turn in counter clockwise direction
> and call other triangles as 1st, 2nd...
> # The algorithm is as following:
> # First determine in which triangle the particle is. One can get this using
> an atan2(x,y) like function. atan2 returns the angle between the position of
> the particle and the x-axis. We can beforehand calculate the angles between
> the sides of triangles and the x-axis. And using this information we can
> find in which triangle the particle is. (or maybe y/x is just enough for
> determination)
> # If it is in the zeroth triangle, all we have to do is to look for the y
> position. If it is less than minus of the height of the bottom triangle then
> it exceeds the boundary. Apply the reflection like in a rectangular
> boundary.
> # One can not do this in other triangles (1st, 2nd...). Because their
> boundaries are not parallel to axes but they have slopes. So, if we rotate
> the coordinate system, until the bottom side of the triangle becomes
> parallel to y-axis, e.g. rotate 72 degrees clock wise for 1st triangle, 2*72
> degrees for the 2nd..., (360/5=72) we can apply this reflection law. (this
> is the trick)
> # After applying the reflection one must rotate the coordinate system
> counter clock wise.
> # Here is the formula of rotating a coordinate system (taken from Arfken,
> Mathematical Methods for Physicists). (x,y) is the coordinate in the old
> system and (x', y') is the coordinate in the new (rotated) system, a is the
> angle of rotation.
>
> x' = x*cos(a) + y*sin(a)
> y' = -x*sin(a) + y*cos(a)
>
> # But one don't have to make trigonometric calculations for every rotation,
> because a is constant. It is 72 or integer multiples of 72 and one can
> calculate sines and cosines beforehand e.g. sin(72) = 0.9511, cos(72)=0.3090,
> for CCW rotations, and sin(-72)=-0.9511, cos(-72)=0.3090 for CW rotations.
> # Here is the pseudo code:
>
> take_the_simulation_one_step_further()
> for_each_particle:
> n = the_number_of_triangle_which_includes_the_particle(x,y)
> (x',y',vx',vy')=rotate_the_velocity_and_coordinate_of_the_particle(-n*72,x,y,vx,vy)
> (x',ynew',vx',vynew')=reflect_the_particle_(if_necessary)_as_if_it_reflects_from_the_bottom_of_a_rectangle(x',y',vx',vy')
>
> (x,y,vx,vy)=rotate_the_velocity_and_coordinate_of_the_particle(n*72,x',ynew',vx',vynew')
>
> # I didn't try this of course, but seems plausible. Thanks for reading :-)
> -uğur-
>
>
> On 10/13/07, marius schebella <marius.schebella at gmail.com> wrote:
>> hi,
>> I need to bounce objects against a border that is not running parallel
>> to x or y, but in a gradient angle. actually I need to bounce the
>> objects within a pentagon. iSeg2D allows object to cross the border when
>> they bounce, there is no x/y-max for gradients.
>> I am also looking into frank's pyode script. Is there something in it,
>> that can do that?
>> marius.
>>
>> _______________________________________________
>> PD-list at iem.at mailing list
>> UNSUBSCRIBE and account-management ->
>> http://lists.puredata.info/listinfo/pd-list
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> PD-list at iem.at mailing list
>> UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list

```