reset password
Author Message
alishas15
Posts: 10
Posted 10:40 Nov 03, 2015 |

(define (up-and-down world-state)                                               
  (let* ([mo1 (first world-state)]                        
         [mo1-x-pos (moving-object-x-pos mo1)]                                  
         [mo1-y-pos (moving-object-y-pos mo1)]
         [mo1-x-vel (moving-object-x-vel mo1)]
         [mo1-y-vel0 (moving-object-y-vel mo1)]
         [mo1-y-vel (if (at-limit mo1-y-pos HIGHEST-Y-POS LOWEST-Y-POS mo1-y-vel0)
                        (* -1 mo1-y-vel0)
                        mo1-y-vel0)]
         [new-mo1 (struct-copy moving-object mo1                               
                               [x-pos (+ mo1-x-pos mo1-x-vel)]
                               [y-pos (+ mo1-y-pos mo1-y-vel)]
                               [y-vel mo1-y-vel])]

[mo2 (second world-state)]
         [mo2-x-pos (moving-object-x-pos mo2)]
         [mo2-y-pos (moving-object-y-pos mo2)]
         [mo2-x-vel (moving-object-x-vel mo2)]
         [mo2-y-vel0 (moving-object-y-vel mo2)]
         [mo2-y-vel (if (at-limit mo2-y-pos HIGHEST-Y-POS LOWEST-Y-POS mo2-y-vel0)
                        (* -1 mo2-y-vel0)
                        mo2-y-vel0)]
         [new-mo2 (struct-copy moving-object mo2 
                               [x-pos (+ mo2-x-pos mo2-x-vel)]
                               [y-pos (+ mo2-y-pos mo2-y-vel)]
                               [y-vel mo2-y-vel])]

 

I know somewhere in this code has to be revised so that ball1 which is mo1 bounces off the ball underneath it which is mo2, but I am not sure where to start. Any help would be appreciated. 

redge
Posts: 31
Posted 10:53 Nov 03, 2015 |
A generic answer so as to avoid academic dishonesty concerns: Think about what your code is actually doing to each ball; i.e. moving them a few pixels at a time. It's easy to predict where they will be at the end of the next tick, and whether there will be a collision (a collision being defined as an intersection of each object's (x,y)+radius). From there it should be reasonably simple to modify your existing movement code to account for the collision and reverse the movement directions to send them away from each other. It will probably be easier to test initially with all velocities set to 1, but be sure to test your logic when the objects are moving several pixels at a time.
Last edited by redge at 10:54 Nov 03, 2015.
darkserith
Posts: 45
Posted 11:12 Nov 03, 2015 |

Am i right to assume that, this code has to work for: ?

1) the top ball: which only bounces only off the top of the scene and the middle ball

2) the middle ball: which only bounces off the top ball and the bottom ball

3) the bottom ball: which bounces only off the middle and the floor

I'm having trouble designing a "general" function that will work for all 3 balls: the top, mid, and bottom.

Reason being, the condition on which the top, middle, and bottom ball's will reverse are all different, according to conditions 1, 2, 3 above. 

So i feel like i need 3 separate testings functions, one for each ball. However, I think this lab wants me to only have ONE function that all 3 balls use to test direction reversal...any hint or thoughts on this would be nice.

redge
Posts: 31
Posted 11:34 Nov 03, 2015 |
In my, understanding, yes, all 3 balls need to have collision detection. A generic function would be nice, and is possible to implement. It's worth pointing out that some of your assumptions about what each ball can impact are problematic. You presume that each ball can only hit the adjacent ball or wall, but if the velocities differ, balls in adjacent rows could pass each other (the middle ball could end up below the bottom ball and bounce off the bottom edge, for example). With that in mind, your generic function should check for impacts with every other object, and all 4 boundaries. You may want to approach this with recursion or map to keep it generic, and if you're particularly clever you can write a solution that will work for any number of balls in a 2D space. However, it will be simpler to limit this solution to 3 balls. Instead of separate testing functions for each ball, remember that you have a list of objects that each need to be checked for collisions, and that list also contains everything that can be collided with, plus the object you're currently checking. Once again, the collision criteria doesn't actually differ between balls; consider the two balls you're looking for collisions with to be generic and you should be fine.
vsluong4
Posts: 87
Posted 12:08 Nov 03, 2015 |

I think lab 5 is where we abstract away the functions.  You're thinking too far ahead

alishas15
Posts: 10
Posted 15:59 Nov 04, 2015 |

Thanks for the replies. So from what I've gathered, all 3 balls need a collision test meaning these lines of codes need to be altered for each ball:

 

       [mo1-x-pos (moving-object-x-pos mo1)]                                  
         [mo1-y-pos (moving-object-y-pos mo1)]
         [mo1-x-vel (moving-object-x-vel mo1)]
         [mo1-y-vel0 (moving-object-y-vel mo1)]
         [mo1-y-vel (if (at-limit mo1-y-pos HIGHEST-Y-POS LOWEST-Y-POS mo1-y-vel0)
                        (* -1 mo1-y-vel0)
                        mo1-y-vel0)]

 

 

Last edited by alishas15 at 16:57 Nov 04, 2015.