View Full Version : retrieving the perpendicular angle from an arc
upuaut
March 17th, 2003, 06:03 AM
ok.. I'm working on the pinball game. I need to come up with a way of retreiving the right angle from an curved line.
The idea is to have the ball follow a curve around the upper border of the game field. The ball itself is going to be a point, which the ball follows, so that I can use the shape of the object when doing hit detection. I'm not sure if that's relivant to the subject or not.. but it gives some idea of where I'm going with this.
I'm open for suggestions.
CyanBlue
March 17th, 2003, 06:22 AM
Howdy...
I guess Kitiara will love it when the game is done... ;)
Anyways... I found a couple of things that might help you to please her... :P
Gravity Ball by FlashGuru (http://www.actionscripts.org/showMovie.php?id=12)
Billiard Ball Physics at FlashKit (http://www.flashkit.com/board/showthread.php?threadid=281136&pagenumber=1)Hope to see a good game... ;)
upuaut
March 17th, 2003, 06:55 AM
thanks.. I'll look into these.
upuaut
March 17th, 2003, 08:42 AM
not anything I haven't accomplished already.. but thanks cyanblue. :)
CyanBlue
March 17th, 2003, 02:48 PM
I'll let you know if I find something else... ;)
lava
March 17th, 2003, 03:16 PM
haha... I TOLD YOU... you're going to need to know the equation for the curve...
maybe if you draw it in actionscript, kinda like lost's besier curve thing on his site, you can figure out the equation from there...
but in order to get the normal, you need to find out the tangent of the curve..
BUT to make a pinball game, you don't NEED to do a curve... have you played the altoids one? I think they ran into that same problem too, and decide to do away with curved surfaces
upuaut
March 17th, 2003, 04:51 PM
I'm not sure if it's worth doing without the curve lavaboy. :)
lava
March 17th, 2003, 05:01 PM
ok well, if you want an irregular curve, you're going to end up using calculus, no matter what...
however, if you want to use the inside edge of a circle as a curve, there are some tricks I can think about... I'll write them when I get home
upuaut
March 17th, 2003, 05:44 PM
I'd be more than happy to hear them.
I was also thinking the same thing as you.. that ploting out the points in a list or arrray, which coincide with the actual curve.. might be the way to go. I'm not quite ready to go to that extreme yet... but maybe soon. :P
lava
March 17th, 2003, 06:16 PM
well.... lets think about how we might have calculated this in pre-newtonian times..
if you have an irregular curve, you could have some hidden straight lines, like 4 or 5, that follow the contour of the curve. You would definitely be able to set the angular response to those... and the ball would simulate to a degree the path it would take along that curve.
I dont think you need to go as far as the pixel on this.
pom
March 17th, 2003, 06:54 PM
http://flash-france.com/fla.php?op=viewfilm&film_id=318
And you can also check all the fla's from Alcys, he's incredibly talented. Tell me if you need a translation :P
lava
March 17th, 2003, 07:49 PM
WOW..
that's all I have to say.
and yes... I need a translation
upuaut
March 17th, 2003, 09:54 PM
Yeah I need a translation as well. That's exactly the math I'm looking for.
lava
March 18th, 2003, 02:56 AM
I think what that FLA is doing is determining where in the contour the ball hits, and hten checking where the coordinates of the surrounding pixels are... then it determines the normal from there...
but I dont think you need to go into that much detail... I think if you're doing an irregular surface (not as irregular as the ones in the example) you should be ok with 4 or 5 invisible lines...
upuaut
March 18th, 2003, 03:03 AM
ehh.. I'm not so sure about that. In the case of a pinball.. we want it to run up a vertical and then bend as it reaches the arc. That's not going to be accomplished by invisible lines.
lava
March 18th, 2003, 07:12 AM
I'll make up an example later on on what I'm talking about...
lava
March 18th, 2003, 11:36 AM
I drew a simple picture in photoshop, so that you can see what I'm talking about... this is my solution to the curve problem
pom
March 18th, 2003, 12:02 PM
Well, Alcys did post an explanation on my french board. I'll try to translate his explanation late tonight. :)
lava
March 18th, 2003, 12:10 PM
thanks! I'm really interested as to how it works.
But David, you also have to think about this... the ball is only going to pass through that area less than 5% of the time (by my estimate..) is a behavior that requires so many hit tests per area necessary?
upuaut
March 19th, 2003, 04:05 AM
well the ball really only has one behavior. And probebly there will never be more than 5 balls in play no matter what game you make. I don't think that the hit tests will be nearly as slugging to the system as you think they are going to be.
and how may hit tests. It's one per ball, per frame.. and possibly only every other frame. Am I thinking about that incorrectly?
lava
March 19th, 2003, 07:06 AM
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.
upuaut
March 19th, 2003, 08:08 AM
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.
lava
March 19th, 2003, 11:13 AM
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...
lava
March 19th, 2003, 12:47 PM
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
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.