# gamemaker getting a ball to bounce 90 degrees off of a 45 angle

I am trying to get a circle to bounce off a 45 degree diamond shap using Gamemaker Studio. I would like the ball to bounce in straight lines so my directions would be 0, 90, 180, or 270 degrees, depending on which side of the diamond the collision takes place. Precise bouncing is giving me all sorts of results so I think I would have to do it in code.

I tried saying:

if (direction >=0 && direction <=90) { direction = 90 }

And doing this for all the angles, but this did not work.

Has anyone encountered this before?

• Always rounding up twists the angle. Anything between 45 and 135 wound round to 90. (Some langauges have built in round functions that can be set to round to the nearest 90). – mobo Aug 3 '13 at 14:18
• It's always a good idea not to write "did not work" and instead describe what you were expecting and what it actually did. – MichaelHouse Aug 3 '13 at 14:41

So for each triangle rotation assign it a type. and then depending on the angle you approach from change the angle then check it's type using other.type during a collision (or whatever variable you store the other object as when you check for a collision so if you do q = collision(??) which returns the object you collided at then you'd do q.type instead)

For example from the example below lets say the top left one is assigned type = 1 at creation

so

if(other.type == 1)
{
if     (direction ==  0){direction = 90}
else if(direction == 90){direction =270}
else if(direction ==180){direction =  0}
else                    {direction =180}
// no need to compare anything since only one left is direction == 270
// by process of elimination for the else statement
}


the others would be 2 3 4 respectively and it's up to you to keep track of which is which.

https://www.dropbox.com/s/bqaqapbr0fiv50m/triangles.png

there is probably a better way which would be to give one triangle object a direction which it would rotate it's image by when it's drawn and be given the direction at creation

and then that direction would be used in the calculations instead of checking type

  if     (direction ==  0){direction = 90 - other.direction}
else if(direction == 90){direction =270 - other.direction}
else if(direction ==180){direction =  0 - other.direction}
else                    {direction =180 - other.direction}
// no need to compare anything since only one left is direction == 270
// by process of elimination for the else statement


so if the top left one is direction == 0 then the bottom left one would be direction == 90 and as you can see subtracting 90 from the end result creates the same desired effects.