# several objects being "pulled" into a single point

Get help using Construct 2

### » Thu Jun 26, 2014 12:36 pm

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?
B
16
S
7
Posts: 301
Reputation: 2,746

### » Thu Jun 26, 2014 1:13 pm

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.

Take a look at my itch.io games
B
45
S
14
G
3
Posts: 167
Reputation: 5,204

### » Thu Jun 26, 2014 2:53 pm

@Sumyjkl
I'm not using physics. any other method without using behaviors? or at least not using physics?
B
16
S
7
Posts: 301
Reputation: 2,746

### » Fri Jun 27, 2014 12:43 pm

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)

Take a look at my itch.io games
B
45
S
14
G
3
Posts: 167
Reputation: 5,204

### » Fri Jun 27, 2014 1:01 pm

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.
B
79
S
13
G
8
Posts: 1,977
Reputation: 9,947