Working (sort Of) Magnet Power Up In Buildbox 3d

Discussion in 'Buildbox 3.0' started by Ivan Perfetti, Jul 9, 2019.

  1. Ivan Perfetti

    Ivan Perfetti Boxer

    Joined:
    Sep 9, 2018
    Messages:
    34
    Likes Received:
    31
    @NikRudenko
    Hi guys, I've been trying for a while and I've got a working magnet in BB3, it is a very classic one, it moves gems along the path toward the player when the magnet power up is active, the problem is it only works while in the first level of a world (Start), after the first level gems are no more moved toward the player but moves in different directions.

    Here's the code I used in the update function inside the gem object (which has no physics enabled), it should be quite self explanatory.
    I tried two versions, a simpler one and one involving matrixes but the result is the same.

    function update(dt){
    if ((enabled)&&(counter<120)){
    counter += 1;
    tractor_speed = counter/500;
    gemPos = this.entity().position(); //Current gem position
    tempVec = gemPos.sub(Settings.playerPos); //Vector to follow
    this.entity().setPosition(gemPos.x-tempVec.x*tractor_speed,
    gemPos.y-tempVec.y*tractor_speed,
    gemPos.z-tempVec.z*tractor_speed);​
    }​
    }

    function update(dt){
    if ((enabled)&&(counter<120)){
    counter += 1;
    tractor_speed = counter/500;
    gemPos = this.entity().position(); //Current gem position
    tempVec = gemPos.sub(Settings.playerPos);
    let mr = Mat4.createRotation(Settings.playerRotQuat);
    let mt = Mat4.createTranslation(gemPos.x, gemPos.y, gemPos.z);
    let m = Mat4.multiply(mt, mr);
    let p = new Vec3(-tempVec.x*tractor_speed,
    -tempVec.y*tractor_speed,
    -tempVec.z*tractor_speed);​
    p = Mat4.transformPoint(m, p);
    this.entity().setPosition(p.x, p.y, p.z);​
    }​
    }

    Any idea of why it only works in first levels? Thanks!
     
  2. whitedogapp

    whitedogapp Avid Boxer

    Joined:
    Oct 8, 2015
    Messages:
    189
    Likes Received:
    29
    Can u add NOde or bbdoc to look?
     
  3. Ivan Perfetti

    Ivan Perfetti Boxer

    Joined:
    Sep 9, 2018
    Messages:
    34
    Likes Received:
    31
    So, it looks like I finally fixed it, there were two problems, first I had to consider the world position of the gem and not its space position.
    I tried before but not in the right way and unfortunately that misleaded me. Second: this won't work very well as it is for objects inside a Level Path but will work perfectly fine in other cases, you just have to place objects to be attracted outside of the Level Path.
    In such a case, the object is connected with the path and when you move it it will move along the path, so moving it in world space won't work (hope that makes sense).
    To improve on this it would be good to be able to understand if an object is inside a level path or not, I'm looking for a way, anybody?

    The code in the Magnet node will look something like this:

    var enabled = false;
    var gemPos;
    var gemWorldPos;
    var counter = 0;
    var id;
    var tractor_speed = 0.1;
    var tempVec = new Vec3(0,0,0);

    function init(){
    }

    function update(dt){

    if ((enabled)&&(counter<180)){
    counter += 1;
    tractor_speed = counter/180;
    gemPos = this.entity().position();
    gemWorldPos = this.entity().worldPosition();
    tempVec = gemWorldPos.sub(Settings.playerWorldPos);
    this.entity().setPosition(gemPos.x-tempVec.x*tractor_speed, gemPos.y-tempVec.y*tractor_speed, gemPos.z-tempVec.z*tractor_speed);
    }
    }

    function signal(name, value){

    if (value) enabled = value;
    }

    As it is it should work fine but you need to create a setting (Settings.playerWorldPos in this example) which will keeps track of your player (or other character/object) world position.
    If anybody is interested I can create a small simplified bbdoc to show how this works.
     
  4. whitedogapp

    whitedogapp Avid Boxer

    Joined:
    Oct 8, 2015
    Messages:
    189
    Likes Received:
    29
    I am interested bro
     
  5. Ivan Perfetti

    Ivan Perfetti Boxer

    Joined:
    Sep 9, 2018
    Messages:
    34
    Likes Received:
    31
    Hey @whitedogapp , check the bbdoc in attachment, here's a gif of it working.

    Magnet example.gif
     

    Attached Files:

    weboha and itzonator like this.
  6. whitedogapp

    whitedogapp Avid Boxer

    Joined:
    Oct 8, 2015
    Messages:
    189
    Likes Received:
    29
    if we edit it it willbe possible to make enemy sistem which chasing u
     
  7. Ivan Perfetti

    Ivan Perfetti Boxer

    Joined:
    Sep 9, 2018
    Messages:
    34
    Likes Received:
    31
    Yes, of course you can, feel free to do whatever you want with it.
     
  8. whitedogapp

    whitedogapp Avid Boxer

    Joined:
    Oct 8, 2015
    Messages:
    189
    Likes Received:
    29
    I dont know scripting, but can u make to also follow gem rotation to caacter like enemy face toface??
     
  9. Ivan Perfetti

    Ivan Perfetti Boxer

    Joined:
    Sep 9, 2018
    Messages:
    34
    Likes Received:
    31
    Hi, I've not understood, could you explain it better?
     
  10. whitedogapp

    whitedogapp Avid Boxer

    Joined:
    Oct 8, 2015
    Messages:
    189
    Likes Received:
    29
    for example if enemy stands back from u, it rotatating face to face and next chasing u,
     
  11. whitedogapp

    whitedogapp Avid Boxer

    Joined:
    Oct 8, 2015
    Messages:
    189
    Likes Received:
    29
  12. whitedogapp

    whitedogapp Avid Boxer

    Joined:
    Oct 8, 2015
    Messages:
    189
    Likes Received:
    29
    I found some error, I removed remove node and remove scrip, but after several second gems still disappeared, with big speed
     
  13. Ivan Perfetti

    Ivan Perfetti Boxer

    Joined:
    Sep 9, 2018
    Messages:
    34
    Likes Received:
    31
    I see, so you're looking for something a bit different, by the way this is just an example of how to obtain this kind of effect, it is oversimplified and far from being perfect or specific for the implementation you require; of course it could face the player any way you wish by rotating it accordingly but this require more code to be added.
     
  14. Bilz636

    Bilz636 Avid Boxer

    Joined:
    Oct 5, 2015
    Messages:
    289
    Likes Received:
    163
    if you get force 0.01 (just for enemy chase system) the gems get vanish at distance
     
  15. Ivan Perfetti

    Ivan Perfetti Boxer

    Joined:
    Sep 9, 2018
    Messages:
    34
    Likes Received:
    31
    @Bilz636 Hi, as i said this is an oversimplified implementation, I created this to show how to create a magnet in the most simple way, I stripped down the one I'm using heavily with the purpose of making it extremely simple and as easy as possible to understand how it works.
    By the way you might just go inside the magnet script and disable this line:

    //
    if (counter>60) this.emitSignal('Remove', true);

    (note: Adding two slashes in front of the line will disable it).

    This line automatically removes the gem after 60 cycles which means after 1 second, if you set attraction force very low the gem will disappear way before reaching the player.
    If you disable this line you'll have a tail of gems forming right behind the player.
    It all depends on what you wish to obtain, for example you might enable collisions and add score points when a gem is hit by the player or make it so it will deal damage or kill the player or maybe bounce away when it hits the player; there isn't any real limitation but as of now some coding is needed to get exactly what you want.
     
    Bilz636 and itzonator like this.
  16. whitedogapp

    whitedogapp Avid Boxer

    Joined:
    Oct 8, 2015
    Messages:
    189
    Likes Received:
    29
    yes, but gems still disappeared after several second, wait and u will see
     
  17. Ivan Perfetti

    Ivan Perfetti Boxer

    Joined:
    Sep 9, 2018
    Messages:
    34
    Likes Received:
    31
    Hi @whitedogapp, I don't see how this is relevant, by the way the reason you see the gems disappearing is because in the end a gem will end up being exactly in the middle of the player since it is being attracted; try setting the player color to a transparent one by setting its alpha channel to 128 and you'll see gems stacking inside the player cube.
     
  18. whitedogapp

    whitedogapp Avid Boxer

    Joined:
    Oct 8, 2015
    Messages:
    189
    Likes Received:
    29
    i changed gems position to see them forever, and suddenly i saw that they are despairing
     

Share This Page