Hello there,

I have the following situation : I have a moving ball in a rectangular 2D chamber. When the ball touches the walls of the chamber it changes it's direction. It is simple to implement the ball movement when the chamber is empty ( it has only 4 walls). My question is , how to I implement the movement of the ball in a chamber with numerous objects in it ? ( by objects I refer to rectangular or square shapes). Also , I know the initial ball direction. I've tried using a matrix to map the room, but I got stuck in it. Have any good ides of how could this be done ?

Recommended Answers

All 4 Replies

my idea is you should implement the algorithm to check for collision.
every time it goes a one step it will check for a collision.
The the collied objet have a method to that takes the before collision
velocity return the after collision velocities and then you can change the
velocities ( x and v vetexes ) of the ball object.

Yep If I am reading correctly, then you need to check the collision between
objects in your room, so they "interact" with each other.

I'm not sure the best way to do it in Windows or what, but I used to do similar in various forms of BASIC and the principles are the same.

The way I would do it in BASIC would be to have two variables representing the direction of the moving object... I'd usually call them DX and DY (delta-X and delta-Y). I would add them to the position so it would be as such:

up - DX=0, DY=-1
down - DX=0, DY=1
left - DX=-1, DY=0
right - DX=1, DY=0

Diagonals are the same only both axes move, such as:
up-left - DX=-1, DY=-1
etc.

If you want an angle, you can use fractions, that is a little more tricky and requires FLOATs.

Then, to reverse direction, you would simply say

dx=-dx; dy=-dy;

Then, I use X and Y as the position (probably the center of the ball).

Upon movement, I use TX, TY (test-X and test-Y) to hold the potential new position. Then, I check for collision or boundaries and update accordingly. (Then if I can't go there I can still refer to the original coordinates). To prevent flicker, I don't redraw unless the position changed, ie:

IF TX!=X || TY!=Y {//position is different so redraw here }

or, alternatively, if you want to see if the position is the same, that would be:

IF TX==X && TY==Y {//did not move. }

(But if you are in perpetual motion you shouldn't be stopping ever.)

As far as collision checking, that is what I am not sure of. In the olden days, I would just read the pixel on the screen... I'm sure there are better ways now. When I didn't want to rely on the screen, I'd have an array representing the screen and update the array as objects changed. However, when dealing with pixels that can be a very inefficient way to do things and if anything moves you have to update the array as well.

So either you read the screen directly, or you track where things are.

The other way is you can have each object in a struct and iterate between them on each movement cycle, calculate where the item is in relation to your ball and see if there possibly could have been a hit. That can be complex if the item is not a rectangle.

I don't know if it is even possible nowdays to read pixels in a window and if it would be the virtual screen (purely what is in the window) or if it would include what is physically on the screen (such as if another window is covering your view if it would see that instead).

Hope this gives you a place to start, anyway.

-Dan

Thx guys, I've finished it ! :) I used a collision function that I call at each step to detect if a collision occurred. Then I establish the new direction of the ball after a little discussion ( 4 ifs). This was simpler than I taught. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.