several objects being "pulled" into a single point

  • This is the situation:

    several objects all over the screen, then they get "sucked" or "pulled" into a single point (it's like a black hole or it's like Magneto from X-Men attracting all metal objects from everywhere into himself).

    How do I code the "sucking in/pulling in" effect?

  • If you are using physics, you can just apply force towards position on the objects. Recommended that you put them all in the same family first though.

  • Sumyjkl

    I'm not using physics. any other method without using behaviors? or at least not using physics?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You can just move toward position. A formula could be used also to slow down over time.

    For example (just a guess): move toward (x,y) distance of: distance / 200 or sqrt(distance)

    Apart from that, you would need to use move at angle, with an angle starting from self, ending at the target location: angle(self.x,self.y,target.x,target.y)

  • a simple verlet integration scheme with decaying energy would work to get an almost physically accurate "gravity" effect.

    you'll have to create a few instance variables, i put them in square brackets for clarity, i named the point which stuff moves towards point so its position would be point.x, point.y

    { for each object: }

    • set [tempx] to: self.x
    • set [tempy] to: self.y
    • set object position to:

    {

    >X: self.x+(self.x-lastx)*0.95+(point.x-self.x)*0.1

    >Y: self.y+(self.y-lasty)*0.95+(point.y-self.y)*0.1

    }

    • set [lastx] to: tempx
    • set [lasty] to: tempy

    you could also try something like:

    { for each object: }

    • set [dist] to: distance( self.x , self.y , point.x , point.y )
    • set [tempx] to: self.x
    • set [tempy] to: self.y
    • set object position to:

    {

    >X: self.x+(self.x-lastx)*0.95+(point.x-self.x)*1/dist

    >Y: self.y+(self.y-lasty)*0.95+(point.y-self.y)*1/dist

    }

    • set [lastx] to: tempx
    • set [lasty] to: tempy

    or something like:

    { for each object: }

    • set [dist] to: distance( self.x , self.y , point.x , point.y )
    • set [tempx] to: self.x
    • set [tempy] to: self.y
    • set object position to:

    {

    >X: self.x+(self.x-lastx)*0.95+(point.x-self.x)*2/(dist^2)

    >Y: self.y+(self.y-lasty)*0.95+(point.y-self.y)*2/(dist^2)

    }

    • set [lastx] to: tempx
    • set [lasty] to: tempy

    play with the constant multipliers that appear in these equations like *0.95, *0.1, *1, *2 to vary energy conservation, spring force, traction force, gravity strength respectively, try to do the same thing to the X Y equations or you'll get strange behavior.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)