Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Magnetodynamics: magnetic physics in Unity [RELEASED]

Discussion in 'Assets and Asset Store' started by jpivarski, Aug 2, 2012.

  1. jpivarski

    jpivarski

    Joined:
    Mar 24, 2012
    Posts:
    15
    Hi everyone,

    I've created a package that simulates electric and magnetic forces between GameObjects, visualizes field lines, and turns ParticleSystems into charged plasmas. It's called Magnetodynamics in the Asset Store (Scripting > Physics > Magnetodynamics) and I have some web demos here:


    Magnets and electric charges are invisible drop-in objects so that you can embed them in your own game. The included demos and tutorials also include realistic models of magnets, paperclips, home-made solenoids and particle accelerators, a charged sheet (with static cling!), etc., which you can use as a starting point. The full documentation is available on the same website.

    Let me know if you run into any problems using it, and I'd love to see what people do with it.

    Cheers,
    -- Jim




     
  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,430
    Very interesting. I'll be watching this thread...
     
  3. Panhna_Cam_XG

    Panhna_Cam_XG

    Joined:
    Sep 27, 2012
    Posts:
    1
    how can i get that script ?
    ple !
     
  4. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    1,942
    You buy it from the asset store.
     
  5. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,646
  6. jpivarski

    jpivarski

    Joined:
    Mar 24, 2012
    Posts:
    15
    Thanks for posting the link! (When I wrote this, I thought that Asset Store packages could only be accessed through the in-Unity browser.)
     
  7. unityasoft

    unityasoft

    Joined:
    Oct 28, 2011
    Posts:
    249
    Bookmarked
     
  8. TiG

    TiG

    Joined:
    Feb 28, 2011
    Posts:
    308
    Wow, very nice, I just noticed this asset. Can be used in an educational software.
     
  9. Pascal-

    Pascal-

    Joined:
    Mar 23, 2013
    Posts:
    23
    Dear Jim,

    I enjoy playing around with your Magnetodynamics package, but I have a trouble trying to enable Vectrosity functionality. I removed the '//' but then I get an error message, in both unity 3 and 4 pro.

    "Assets/Magnetodynamics-v1.0/Magnetodynamics/ObjectsToPlaceInYourScene/_ScriptsThatMakeThemWork/FieldLineScript.cs(42,13): error CS0246: The type or namespace name `VectorLine' could not be found. Are you missing a using directive or an assembly reference?'

    Do you have any idea what I'm doing wrong. Is there a special way how to unpack the Vectrosity Extras, or something to do in or with the scripts?

    Thanks a lot in advance for your support,
    Pascal
     
  10. jpivarski

    jpivarski

    Joined:
    Mar 24, 2012
    Posts:
    15
    Hi,

    From the error message, I can see that you have the "#define I_HAVE_VECTROSITY" line uncommented, which is what you need to do from the Magnetodynamics side. On the Vectrosity side, you need to have that package installed somewhere where it is visible to C# scripts. I have Magnetodynamics and Vectrosity installed side-by-side. Vectrosity Extras is not relevant; that just has some documentation and demos, and Magnetodynamics only needs the core functionality (the VectorLine and LineType classes). Is it possible to use Vectrosity in other scripts?

    For instance, does the following work?

    public class TestVectrosity: MonoBehavior {
    private VectorLine line;
    void Start() {
    line = new VectorLine("MyLine", new Vector3[2], Color.green, null, 8.0f, LineType.Continuous);
    line.points3[0] = Vector3.zero;
    line.points3[1] = Vector3.up;
    }
    void Update() {
    line.Draw3D();
    }
    }

    (It should make a green line from the origin to (0,1,0) which will be visible if other things aren't covering up that region. The "null" in the VectorLine constructor is an optional Material.)

    Once this simple script works, FieldLines should work, because it doesn't rely on any other features. Although you're getting an explicit error message telling you there's a problem, if there wasn't an error message and you're just not seeing field lines, this mini-script would be a way to check for silent errors, to distinguish between no-Vectrosity and the possibility that the real fields lines are just not where you think they are. Since you're getting an error message, this mini-script could be a simpler way to track it down and let you know when the Vectrosity library is in scope. If, in your environment, you need an additional line at the beginning of the C# script ("using Vectrosity;"?) or if you need to explicitly set the Script Execution Order Settings (I didn't) to get this to work, then FieldLineScript.cs will need that line, too.

    Other than that, I can only refer you to the Vectrosity page and forum for help getting that set up. I'd think the forum would be more useful.

    By the way, if you find a solution, could you please post it here (in case anyone else is having this problem)?

    Thanks,
    -- Jim
     
    Last edited: Mar 24, 2013
  11. Pascal-

    Pascal-

    Joined:
    Mar 23, 2013
    Posts:
    23
    Hi Jim,

    I tried to use the script, but i have the same kind of missing message,

    Could I somehow verify if the VectorLine and LineType classes are available, I just tried to create a new project importing only Vectrosity and Magnetodynamics packages but I still have the same messages. if I unpack the plugin, demos and editor scripts of the Vectrosity packages, i can run the Vectrosity scenes.

    I'm using the last version of Vectrosity. Could that be the cause?

    Thanks,
    Pascal
     
  12. jpivarski

    jpivarski

    Joined:
    Mar 24, 2012
    Posts:
    15
    If you create a new project importing only Vectrosity and the mini-script "TestVectrosity" above (no Magnetodynamics), do you get the error?

    If the latest version of Vectrosity needs to be imported in a different way, I am unaware of it. I'll test it as soon as I can, but I can't do it on the computer I'm using right now.

    -- Jim
     
  13. Pascal-

    Pascal-

    Joined:
    Mar 23, 2013
    Posts:
    23
    Dear Jim,

    I solved my trouble. In fact with the new Vectrosity package, we should simply add "using Vectrosity;" in C#.

    So previous test code would be,

    Code (csharp):
    1. using Vectrosity;
    2.    
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class TestVectrosity: MonoBehaviour {
    7. private VectorLine line;
    8. void Start() {
    9. line = new VectorLine("MyLine", new Vector3[2], Color.green, null, 8.0f, LineType.Continuous);
    10. line.points3[0] = Vector3.zero;
    11. line.points3[1] = Vector3.up;
    12. }
    13. void Update() {
    14. line.Draw3D();
    15. }
    16. }
    and for the FieldLineScript header, simply add the same,

    Code (csharp):
    1.  
    2. #define I_HAVE_VECTROSITY
    3. //#define SHOW_VECTROSITY_ERRORS
    4.  
    5. using UnityEngine;
    6. using System;
    7. using System.Collections;
    8. using Vectrosity;
    9.  
    Thanks for your support,
    Pascal
     
    Last edited: Mar 25, 2013
  14. jpivarski

    jpivarski

    Joined:
    Mar 24, 2012
    Posts:
    15
    Thanks for letting me know--- I'll test it on different versions and update the package so that others don't run into the same difficulty.

    -- Jim
     
  15. Play_Edu

    Play_Edu

    Joined:
    Jun 10, 2012
    Posts:
    714
  16. jpivarski

    jpivarski

    Joined:
    Mar 24, 2012
    Posts:
    15
    The solenoid in Magnetodynamics doesn't look quite like that, but it has solenoids and they are visualized by field lines. The best thing to do is to look at my website where I have a live demo of the package with some sample scenes:

    http://www.coffeeshopphysics.com/magnetodynamics/

    A solenoid is included on this page, though it has infinitely thin wires, unlike the thick wires in the link you posted. The advantage is that the thin-wire case can be solved with an analytic equation (solved by NASA in the 1960's), rather than a numerical PDE, so that it can update at game speeds. The image in your post might have taken a long time to solve and render. Also, the field lines in the image you pointed to are the subset that occupy a plane through the solenoid; the field lines in Magnetodynamics can be anywhere in 3D space (you set up as many as you'd like to see, based on performance considerations). Also, rather than foggy lines that indicate field strength in addition to direction, the field lines in Magnetodynamics only trace out the direction of the path.

    Note that the field lines require a third-party package: you'd have to get Magnetodynamics and Vectrosity (see earlier messages in this forum). That's the only feature in Magnetodynamics that has a third-party dependency.

    If you're interested in the look of the image you posted, but not its physical meaning, note that Magnetodynamics also simulates plasmas. In the live demo, drag the "Handheld particle accelerator" toward the magnets. This is not simulating field lines--- it's simulating the paths of charged particles in a magnetic field. They bend, focus, and spray in response to the Lorentz forces of electric and magnetic fields just like a real particle beam (see the Van de Graaff example for electric forces) and have the same kind of foggy look as the image you posted. The Penning Trap demo does some realistic physics with plasmas: it demonstrates an electric and magnetic field configuration that is typically used to trap antimatter in bottles.

    The plamsa feature does not require a third-party package, and it's based on Unity particle streams that can be customized in more interesting ways than I have here. Also, all of the sample scenes on the website are included in the package for your use.
     
  17. Play_Edu

    Play_Edu

    Joined:
    Jun 10, 2012
    Posts:
    714
    hi,


    Thank you for replay
     
  18. gecko

    gecko

    Joined:
    Aug 10, 2006
    Posts:
    1,815
    I just purchased this; it's super cool. I'm trying to figure out how to create solar prominences, which follow magnetic lines, like shown here.. I know the particle effect is up to me, but I can't figure out how to set up your stuff to create this effect. Can you give me any suggestions?

    thanks!
    Dave
     
  19. jpivarski

    jpivarski

    Joined:
    Mar 24, 2012
    Posts:
    15
    Unfortunately, I tried something like that and found that there wasn't enough granularity in the ParticleSystem to do it honestly. I was trying to make Van Allen belts, in which particles effectively follow field lines by making tight spirals along the lines. (Charged particles are always travelling at right angles to the field line, so when it appears that they're following a line, they're actually doing these tight loops that trap them in two dimensions and allow them to drift along a third--- hence the hazy streaks.) To make the effect real, there would have to be so many particles that they become a blur, but Unity's ParticleSystem cleverly emulates that by making the individual particles blend into one another. When I added close to enough particles, the simulation slowed down too much.

    However, there are some things you could do. It would be easier (fewer particles) to use an electric field instead of a magnetic field. The particles wouldn't be following tight loops around the field lines, they'd be stricly following the field lines. Then you can use fewer of them, going in both directions (both charges), and you can make them big and blurry. To set up an electric dipole, put two StaticCharges close together, and use electric FieldLines as a diagnostic to be sure you're setting them up where you want them. Release the particles from a point near the electric dipole. If you pick a few consolidated locations, they'll make cleaner streaks. If you release them randomly, they'll be a diffuse blob (less interesting; you could do that without any forces).

    Then set the colors right, make a sun-ball, and it ought to look pretty good, even if it's a bit of a cheat. Good luck! (I'm curious to see the final result. If you get it into a playable demo, could you link to it here? Thanks!)
     
  20. gecko

    gecko

    Joined:
    Aug 10, 2006
    Posts:
    1,815
    Thanks for the help. I've got this set up, but the plasma is not following the electric lines -- see screenshot. I've moved the plasma object around, but it never hooks into the field line. Is there something else I need to do? Or is the field not strong enough (in which case, what do I do about that)?
     

    Attached Files:

  21. jpivarski

    jpivarski

    Joined:
    Mar 24, 2012
    Posts:
    15
    It's probably not strong enough. Almost always, you have to gradually tune the field strength or charge up by orders of magnitude to find the scale that is neither insignificantly weak nor overpoweringly strong. The StaticCharges have a "strength" setting: you should change them both by the same amount if you want it to remain a pure dipole. Also, the plasma script has a "strength" (or "charge?") setting, in case you want to change the affected object rather than the source. In the end, the force is the product of StaticCharge field times plasma charge.

    Look for the "strength" parameter. I remember making a sweep across the whole product to make the name of this parameter uniform. It has different meanings in different contexts (e.g. electric field rather than magnetic field), but they all scale with "strength".

    -- Jim
     
  22. gecko

    gecko

    Joined:
    Aug 10, 2006
    Posts:
    1,815
    Thanks for the tips. I had some trouble setting this up fresh , but then I modified your van de Graaf scene and got something pretty quickly. Still working on the particles, but it's coming along!
     

    Attached Files:

  23. gonzalitosk8

    gonzalitosk8

    Joined:
    Jun 30, 2013
    Posts:
    2
    this system could be used in a racing game?
    I mean that when a car skids, the magnet is activated and gives you more control fluid. I think that made ​​in Need For Speed ​​Carbon!
    any ideas?