Retrieving the perpendicular angle from an arc

The way that I’m thinking about it is that the ball does not conduct the hit test… the objects do. It’s easier to keep track of behavior (in my mind).

I mean… because every object is going to have it’s own method… when the ball hits a certain object, it’s going to react differently from another one. So you’d be checking every object on the table against the ball… well, not EVERY object… someone posted a trick against this in a past thread.

I think in the case of that curve… it’s going to be a lot of objects…

but it might turn that poms translation will have an elegant solution to this problem… I hope it does.

It might be easier to keep track of behavior, but my way reduces the hit test to only one per 1 - 2 frames. I’m not sure I understand what you’re saving by going with the hit test on each object. Can you explain it again?

sorry… a bit dense this morning.

well. first of all, lets just say that you cant use hittest().
Why? because hittest() only puts up a flag when two objects are overlapping. Lets say the ball is going to hit a wall, and it’s moving at a high speed… well, the ball might not be touching the wall one second, and then the next frame the ball could be all the way inside the wall… get my drift? So we need to know ahead of time where the ball is and where it’s moving… maybe you know this, but I wanted to be clear on that point… it’s a sidetrack…

Ok, back to the topic. Let’s say we’re doing something a lot less complicated… we have a ball with constant velocity bouncing around the screen… The angle of it’s motion is not vertical or horizontal… meaning that it’s moving in two dimensions… and we want the ball to bounce at the edges of the screen… we’d have to do this code… we’ll call the ball “ball”


//somewhere else in code
// lets say that the boundaries are 640x480

//start at the middle
ball._x = 320
ball._y = 240

ball.velocity = 10
ball.angle = 30 //in degrees

xmin = 0
xmax = 640
ymin= 0
ymax = 480


//ball actions
onClipEvent(enterFrame){

// convert angle to radians
// need to do this because sin and cos work only with
// radians, not degrees

radians = angle * 180 / 3.1415

// calculate change in x and y

changeX = velocity * sin(radians)
changeY = velocity * cos(radians)

//calculate position on next frame

nextX = this._x + changeX
nextY = this._y + changeY

// hit tests

if (nextx < 0){
// compute angle of incidence, compute exit angle..

}

if (nextx > 640){
// compute angle of incidence, compute exit angle..

}

if (nexty < 0){
// compute angle of incidence, compute exit angle..

}

if (nexty > 480){
// compute angle of incidence, compute exit angle..

}


}


My point here is that you have to conduct 4 hit tests every frame… if you have more objects to bounce off from… it’s going to get more complicated.

there’s tricks to reducing the number of hit tests computed… and I’ll talk about them later, but first I’m going to post a tutorial on getting angle of incidence…

ok, here’s a simple tutorial on calculating the exit angle for a vertical wall… In doing the pinball machine, we’re going to need to know how to calculate at what angle the ball is going to bounce when it hits a surface. This is a pretty simple example… the behavior of a ball when it hits a wall… this example applies to whatever angle the ball hits…from the left, from the right, if the ball is going up or if the ball is going down… cause those directions are described by the angle of the ball.

I attached a gif image for you guys to see… Please look at it while reading this tutorial.

Phi = the symbol with the circle with the vertical line
theta = the symbol with the oval with the horizontal line
alpha = the other symbol…

In this example, phi is the direction (or the angle) of the velocity vector of the ball. We know this, because angle and velocity are properties of our ball object. All angles start at 0, and continue counterclockwise to 360. If you compare it to a watch, 0 degrees is at 3 o’clock, 90 degrees is at 12, 180 degrees is at 9 o’clock,
270 degrees is at 6 o’clock, and 360 degrees is back at 3 o’clock.

theta is the angle of incidence… this is the angle at which the ball hits RELATIVE to the wall. It is measured from the line of the trajectory of the ball towards the wall…

alpha is the exit angle… it is the angle at which the ball will go after hitting the wall. Alpha is not relative.

when an object hits the wall, it comes in towards the wall at an angle of theta (from the initial direction to the wall) and it leaves the wall at the same angle (from the wall to the final direction). If you look at the diagram, it makes sense.

so lets calculate the angles…

theta = 270 - Phi
alpha = 270 + theta

it follows that

alpha = 270 + 270 - Phi
alpha = 540 - Phi

the neat thing about angles is that they overlap… so you can subtract so 70 degrees is equal to (70 + 360) degrees. This means that we can subtract 360 degrees from 540 and so…

alpha = 180 - phi.

So to find at what angle your ball is going to travel after hitting a vertical wall, just substract the angle your ball is traveling from 180…

now mind you, this is only for a VERTICAL wall… for a horizontal wall things are different… alpha = 360 - phi… you should find that out on your own.

Angled surfaces are different, and I’ll talk about them next