A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: pinball physics

  1. #1
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002

    pinball physics

    i've conducted some searches and found some good stuff around here, but its so hard to understand things.

    I need to find the point of collision with a circle and a line. I'd love some simple explanation about what does what.

    Find the angle at which the ball must bounce off.

    Things i think i need to know:

    - balls angle of velocity or the slope at which the ball is travelling at
    - the walls angle
    - the walls length

    I've read some stuff on projection but it gets way too confusing quickly, dot product and stuff like that, normals. I have no idea what they are, and after reading through explanations i'm like "what the hell"

    here's how i'd imagine i'd go about it

    -get angle of velocity
    -project this angle onto the line somehow
    -find the distance between that point of intersection and the balls centre
    -if the distance is less than the balls radius we hit the wall
    -place the ball correctly on the line
    -compare angles between the wall and the ball
    -update the balls x and y speeds by cosing and sining them with the correct angle
    -trace "hoorah the ball bounced off the wall"

    i pretty much have problems with all of that, especially with the projection, it just makes no sense to me. Please please help a lost soul.

    been to metanet (honestly can anyone understand their source files), seen the pinball physics engine made in flash 5, searched the forums, searched for circle - line collision, i have Jobe's pinball example (thats what i've been working off) can anyone just give me some explanations, please.

    Also if you are going to give me some advice please don't use mathematical symbols, i have no idea what they are and nice variable names would be good, like:

    bad - b = y-s*x;
    good - angleVelocity = y-slope*x

    i need to know what i'm typing, i have trouble learning from single letters.

    Thankyou, and I greatly appreciate any help
    Last edited by mr_malee; 03-20-2006 at 08:56 PM.
    lather yourself up with soap - soap arcade

  2. #2
    hmm Pugger's Avatar
    Join Date
    Sep 2003
    Perth, Australia
    Sounds like I am having the same trouble you are. I've been working my ass off trying to use vectors to create a number of AS2 classes I can use to replicate the effect of an object bouncing.

    So far I've done some buggy prototypes based on metanet and tonypa's tutorials. All I'm saying is I need help too.

  3. #3
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Yeah, all i want is some nice explained code which tells me how to check for a collision and what i need to do to react, i'll handle the broad phase collision, that i can do. Its just the really hard narrow phase collision thats getting to me. Man i hope my brain is still growing cause i really want to learn this stuff

    it seems whenever i do get some source or direction it almost is never the same as any other source file, i understand that people code differently, but it looks as if the mathematics change. Which one do i choose to learn off? how the hell should i know, man this is frustrating.
    Last edited by mr_malee; 03-21-2006 at 01:00 AM.
    lather yourself up with soap - soap arcade

  4. #4
    hmm Pugger's Avatar
    Join Date
    Sep 2003
    Perth, Australia
    I share the same frustations, everyone has their own way of doing things. I suppose its because of representation.

    Like tonypa's vectors contain the start point, end point, direction. While others just contain direction, length and angle. I think it seems to be a matter of what you are going to use them for. Now I've been trying to use tonypa's sort of style and design as I want to avoid using sin and cos.

    I have no idea how many times I've looked at tonypa's tutorials and source code (tut6a.fla argggh). When I get home tonight I'm booting up my flash and having another crack. My last prototype is so close. I'll share my findings, if any.

    brain hurts

  5. #5
    Senior Member ozmic66's Avatar
    Join Date
    Oct 2005

    I know how you feel

    hey guys,

    I must say that i know exactly how you feel
    I've been feeling the same frustration and confusion for years until a few months ago when i finally figured it out

    it all became clearer after i read jakobsen's article because it tied up a bunch of loose ends which just made everything clearer.
    so... let's see if i can explain it to you. let's start with this:

    i started this thread a while back (check the examples i posted)

    first i thought that i should use angles for everything, but that made everything a bit more complicated than it needed to be. Use vectors! once u get them (and they're not very hard) a new world of possibilities will open up. The most important, and usefull thing you can have is the dot-product (i know..it baffled me too at first, but its really pretty simple)
    try this thread where i explained a bit about dot-product.

    also, you do need to seperate the physics and geometry part of this
    geom. being finding collisions and fixing objects so that they don't penetrate, and physics being the reaction after the collision

    so read up on vectors and post here any questions that you have

    good luck
    Last edited by ozmic66; 03-21-2006 at 02:11 PM.

  6. #6
    Iron Chef In-Training OpethRockr55's Avatar
    Join Date
    Sep 2005
    Kitchen Stadium
    I totally agree with Ozmic in using vectors, they are BILLIONS OF TONS easier to use than other things with a mess of trigonometry. I think Metanet and Tonypa lay the foundation of vectors down extremely well, Metanet in their slideshow, "Beyond HitTest", and Tonypa in his breakdowns.

    Unlike Ozmic, I went from Jakobsen first, to the "What the F&!%?" stage, to knowing what I'm doing half the time. I'm still kinda in the "What the F&!%?" stage with my box-box collisions, but I've got the .fla open now and should have the basic detection and projection done.

    I personally like Metanet's use of the SAT, which would be excellent for pinball physics where I don't think a great deal of friction would be used, but bounce is an easy addition to the projection method.

    EDIT:// I almost forgot to mention to EXPERIMENT! Just mess around with the tutorials or explanations and try to understand what's happening.
    Last edited by OpethRockr55; 03-21-2006 at 07:44 PM.

  7. #7
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    anyone have experience with the ICE engine,using it within flash and stuff? I've purchased the full version and I love it, but there's no help when it comes to making your pinball game in flash (graphics and sounds, events etc.)

    Anyone used this before?

    -thanks for the suggestions guys, i've been reading about vectors and they seem ok, i get very confussed about variable names which i think is my biggest problem (sounds stupid) but seeing this written 10 times

    v.px = v.p0.dx+v.p1.dx*v.p1.vx+v.p2.vx/v2.p1.dx*v2.p2.dx

    can get a little confusing
    lather yourself up with soap - soap arcade

  8. #8
    Senior Member
    Join Date
    Apr 2002
    Looks like I am in the same boat as you mr mallee

    I have read Tonypa's tutorials, got the jobe makar book, read tons of websites and still cannot even get a ball to bounce off a stationary line.

    I am able to check the collision fine using:

    // Calculate difference in the x positions of the 2 balls
    var vDiffX = ball1_mc.x - ball2_mc.x;
    // Calculate the difference in the y positions of the 2 balls
    var vDiffY = ball1_mc.y - ball2_mc.y;	
    // Use pythagoras theroem to work out the distance between the two balls
    // Basically this mathematical formula calculates the length of the side of a
    // right angled triangle opposite the right angle
    // If the 3 sides of the triangle are named
    // Side 1
    // Side 2
    // Side 3 <- side opposite right angle
    // The formula says
    // Side1 squared + Side2 squared = Side2 squared
    // Therefore if we know the length of Side1 and Side 2, we can calculate
    // the length of Side3
    // (Side3 x Side3) = (Side1 x Side1) + (Side2 x Side2)
    // Hence Side3 = Square root of (Side1 x Side1) + (Side2 x Side2)
    // In my code, vDistance is basically Side3
    // vDiffX is Side1
    // vDiffY is Side2
    var vDistance = Math.sqrt(vDiffX * vDiffX + vDiffY * vDiffY);
    // For the 2 balls to be touching, the distance between their center points
    // must be less than the sum of their radii
    if (vDistance <= (ball1_radius + ball2_radius)) {
         trace ("WE HAVE A COLLISION!!");     
    What I am having a problem with is dealing with this collision. I am treating one ball as a stationary ball so in effect it is like bouncing off an angled line.

    So I tried the following code

    // Calculate angle between the center points of the 2 balls
    var angle = Math.atan2(vDiffY, vDiffX);		
    // Store the cosine and sine of this angle in temp variables
    var cosa = Math.cos(angle);
    var sina = Math.sin(angle);
    // Project the horizontal velocity and vertical velocity of the moving ball onto
    // the line of action i.e the line that passes through the center points of both
    // balls 
    var vyip = (cosa * ball1_vertical_velocity) - (sina * ball1_horizontal_velocity);
    // Project the horizontal velocity and vertical velocity of the moving ball onto
    // the line that is at a right angle to the line of action
    var vxip = (cosa * ball1_horizontal_velocity) + (sina * ball1_vertical_velocity);
    // Reverse vyip to get final velocity along line of action
    var vyfp = -vyip;
    // Leave velocity along the line at a right angle as it is
    var vxfp = vxip;
    // Project the above 2 final velocities back along the x and y axes
    var vxf = vxfp * cosa - vyfp * sina;
    var vyf = vyfp * cosa + vxfp * sina;
    ball1_horizontal_velocity = vxf;
    ball1_vertical_velocity = vyf;
    The above code looks ok on paper but I have made a really simple test flash movie with it and it just does not work. The balls vertical velocity just does not appear to reverse.

    For example I tried putting ball1_mc directly above ball2_mc and giving it a vertical velocity of 1
    The collision is detected perfectly but it does not reverse the vertical velocity. It should I would have thought simply reversed it so it becomes -1

    For larger vertical velocities, I need to ensure that ball1 does not move inside ball 2. If anyone can help me with this too I would be most grateful. Though the important thing is to get the first bit working.

    I really hope someone can at least explain why my code is not working

    Here is a link to the fla


    Last edited by chuckylefrek; 12-07-2006 at 05:50 AM.
    Paul Steven

  9. #9
    the cheesy child bounceboy's Avatar
    Join Date
    Dec 2008
    I want to make something like the foon.co.uk game its a fact

    I can't becayse nobody can be nice enough to give us the code!

  10. #10
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    who is this? a word of friendly advice: FFS stop using AS2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width

HTML5 Development Center