1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Plotting a helix round a straight line segment with particles

Discussion in 'Development' started by Sandertv, Dec 4, 2016.

  1. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Hello!
    I'm experimenting with some particle shapes, circles, spheres, other shapes, and I found myself in a sticky situation. So what I want is this: I want to make a spiral using the direction vector from a player. Basically when a player hits the ground with for example a stick, particles spiral to about... well... I'm not exactly sure, it doesn't matter exactly how far it has to go.
    I have made a couple attempts to do this (all failed however, too badly) but I couldn't really find a good way. I've tried to do it using something like this, which did work in some way: (I forgot what exactly I did as I changed it later)
    PHP:
          $pos $p->getPosition();
          
    $direction $p->getDirectionVector();
          for(
    $i 0$i <= 360$i += 2) {
            
    $pos $pos->add($direction);
            
    $x = (cos(deg2rad($i)) * 0.5) + $pos->x;
            
    $z = (sin(deg2rad($i)) * 0.5) + $pos->y;
            
    $p->getLevel()->addParticle(new FlameParticle(new Vector3($x$pos->1$z)));
          }
        }
    This did did make the right shape (I think, as I could only see a curve), but it went out so far that I could only see a curve in the line of particles, which is because there is so much space between the particles thanks to the getDirectionVector(), which gives like a block space in between cause it uses the block the player is looking at. I know my code is not efficient and well... Not working, but it's pretty much a first timr for me and I don't know too much about this.
    I hope someone can help me with this, thanks in advance!
     
    SOFe likes this.
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    If you are referring to this shape (helix):
    helix.jpg
    where the axis is of any direction (including oblique) such that the helix also rotates along with the axis, I would make use of cross product.
    cross-product.png
    I would present my calculation mathematically (I calculated this a long time ago, and I forgot to define some variables, and now I already forgot what those variables represent, and right now I'm not in the mood to calculate it again):
    vector.png
    EDIT: This image is wrong. In the last equation, (x / |x|) sin(2nk[pi]) should be appended with a [cross] v.

    In code it would be like this:
    PHP:
    function helix(Vector3 $aVector3 $bfloat $radiusfloat $cycles, callable $spawnParticle) {
      
    $density 0.2;
      
    $x $b->subtract($a);
      
    $length $x->length();

      
    $otherCross = new Vector3(100);
      if(
    $otherCross->cross($x)->lengthSquared() == 0// if parallel
        
    $otherCross = new Vector3(010);
      
    $v $otherCross->cross($x)->normalize()->multiply($radius);
      
    $u $v->cross($x->normalize());

      for(
    $i 0$i <= $x$i += 0.1) {
        
    $k $i $length;
        
    $p $a->add($x->multiply($k))
          ->
    add($v->multiply(cos($cycles $k M_PI)))
          ->
    add($u->multiply(sin($cycles $k M_PI)));
        
    $spawnParticle($p);
      }
    }
    Again, if you found any mistakes, point them out. I wrote down my calculations a few months ago, and I have already forgotten what I was doing with u and v.
     
    Last edited: Dec 5, 2016
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    After thinking again, I think v is a vector of r long perpendicular to x, and u is a vector of r long perpendicular to both x and v. Hence x cross v would result in a vector perpendicular to both, with length |x| |v|, so I divide it with |x| to get back the length of r.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.