dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Need help with my physics app (AS 2)

  1. #1
    Likes Pankakes
    Join Date
    Mar 2008
    Location
    Germany
    Posts
    59

    Need help with my physics app (AS 2)

    Hi guy,

    First off, my English isn't perfect so bare with me , also, this could be quite a wall of text...sorry. I've been working on this physics application in Flash 8 again lately and I have a few problems. I'm quite new to actionscript since I'm more of an animator. Still, I'd like to pull this off, but I'm going to need some help. Anyways, here's what I'm trying to do:

    I'm trying to simulate the path an electron (or proton) would take if it were shot into an electric field (in this case, a parallel plate capacitor). I've programmed all the necessary things for the particle to move in the direction of the x-axis, and move in the direction of the y-axis too once it enters the field. The charge, mass, Voltage connected to the capacitor and the distance between the two plates make up the acceleration which should be applied to the particle when it enters the electric field. Right now my program is just working with natural numbers for the speeds (like 3 or 4) so it wont be too fast. When I have things working with those, I can have the user enter realistic values and flash will calculate with those and transform them into values it can work with so you can actually watch the path of the particle. I'm not quite there yet since I have a problem. Right now, once the particle enters the magnetic field it is applied the velocity v_y in the direction of the y-axis which creates a path that looks like this:



    So there is no acceleration at work here yet. Just constant speeds in both directions. The velocity in the direction of x stays constant throughout the entire experiment. What it should look like though, is like this:



    As you can see the path of the electron is a parabola (sp?). If it helps, the formula for this path can be derived from a = (Q * U) / (m * d) and y = 0.5 * a * t^2. I hope these formulas are clear, if you want me to explain them if it will help you understand it, then I will, but for now I wont. The formula for the parabola type path is: y = (Q * U * x^2) / (2 * m * d * v_x^2). v_x would be the speed in the direction of x (see images).

    How can I code it in flash so that the speed in the direction of y is not constant, but indeed influenced by the acceleration, giving the path of the particle the form of a parabola? Everytime I try doing it with functions or in another way, it doesn't work. I would need a function that always calculates the new speed and gives the output right aways, everytime the position in the direction of x changes (see formula). Any ideas guys? I would really appreciate it. If it would help, I can send the fla file for anyone who wants to have a look at it. Remember it's Flash 8.

    Cheers,
    Chris!

  2. #2
    Likes Pankakes
    Join Date
    Mar 2008
    Location
    Germany
    Posts
    59
    All of my code is in an action layer...thought I might aswell post it, seeing as it could help:

    Code:
    //Variablen:
    
    var v_x = 0;
    //Geschwindigkeit in x-Richtung
    
    var v_y = 0;
    //Geschwindigkeit in y-Richtung
    
    var a_y = 0;
    //Beschleunigung in y-Richtung
    
    var U_c = 0;
    //Kondensatorspannung
    
    var d = 0;
    //Plattenabstand d
    
    var l = 180;
    //Laenge des Kondensators
    
    
    //Konstanten:
    
    var Q = 1.602176462*Math.pow(10, (-19));
    //Elementarladung
    
    var m_e = 9.10938188*Math.pow(10, (-31));
    //Masse Elektron
    
    _root.start_btn.onRelease = function() {
    	v_x = Number(_root.v_x_input.text);
    	U_c = Number(_root.U_c_input.text);
    	d = Number(_root.d_input.text);
    	//a_y = (Q*U_c)/(m_e*d);
    };
    
    onEnterFrame = function () {
    	_root.electron._x += v_x;
    	if (_root.electron.hitTest(_root.e_feld)) {
    		_root.electron._y += 1;
    	}
    	
    	_root.Platte_1._width = _root.l;
    	_root.Platte_2._width = _root.l;
    	_root.e_feld._width = _root.l;
    };
    Cheers!

  3. #3
    Senior Member
    Join Date
    May 2006
    Location
    Manhattan
    Posts
    246
    you have everything you need. however, right now you're using an implied timestep of 1 / framerate. you might want to make sure that all of your metrics are as expected (you probably want to express things in seconds and meters as opposed to framerate and pixels). so you have acceleration in the y direction:

    a_y = (Q*U_c)/(m_e*d);

    and you have a y velocity term:

    var v_y = 0;

    now, just as you're integrating your x velocity, so should you integrate y acceleration twice: acceleration -> velocity -> position.

    v_y += a_y;
    _root.electron._y += v_y;

    my guess is that you actually want to do your hitTest conditional such that you're only applying an accelerative force- not updating position. because you're y velocity is zero at start, it won't move in the y direction until an acceleration is applied:

    electron._y += 0 + 0 = electron._y

    hth!

  4. #4
    Likes Pankakes
    Join Date
    Mar 2008
    Location
    Germany
    Posts
    59
    Yea I disabled that piece of code for the acceleration because the values wouldn't have fit yet and it wouldn't work. Could you maybe explain what you did there with that little piece of code which applies the acceleration force to the velocity in the direction of y?

    Cheers,
    Chris

  5. #5
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    Both position and velocity both need to be integrated in time.

    You've got this one down pat:

    Code:
    velocity = (time rate of change of) position
    velocity = (new_position - old_position) / (timestep)
    OR
    new_position = old_position + timestep*velocity
    But you also need:
    Code:
    acceleration = (time rate of change of) velocity
    OR
    new_velocity = old_velocity + timestep*acceleration


    Putting it all together:
    Code:
    var acceleration_y : Number = (QV)/(DM) 
    //you have this part correct already - Q=charge of electron, 
    // V=potential difference between plates, D = seperation 
    // between plates, M = mass of electron)
    velocity_y += acceleration_y;
    position_y += velocity_y;
    
    // and now the x part - no forces along x, so no acceleration to integrate
    position_x += velocity_x;
    Any help now?

  6. #6
    Likes Pankakes
    Join Date
    Mar 2008
    Location
    Germany
    Posts
    59
    yea it seems to be working...thanks for the help guys. Now all I have to work on is allowing the user to input realistic values so the whole thing will actually be useful.

    Cheers,
    Chris!

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