# Black hole physics

Get help using Construct 2

### » Sat Jan 21, 2012 4:30 am

This had to have come up already, but I cant find it...

I want a black hole to change the physics path of a moving object.

I have an Sprite moving across screen, passing near black hole using physics:

Every Tick
-> "SPEED"=Distance(0,0,Sprite.physics.velocityX,Sprite.physics.velosityY)
-> "DISTANCE"=Distance(BlackHole.x, Blackhole.y, Sprite.x, Sprite.y)

Sprite.Distance <500
-> Sprite Apply "FORCE" at angle(sprite.x, sprite.y, Blackhole.x, blackhole.y)

QUESTION - How do I calculate "Force" so that it is stronger proportionally to the distance of the hole as the sprite gets closer. I have racked my brain, but cant get it.

B
192
S
6
G
7
Posts: 213
Reputation: 10,178

### » Sat Jan 21, 2012 4:37 am

You need a constant. Say the force at 500 pix from the center was 500. Force- distance at that point would be 0, 1 pixel closer would be 500 - 499, or 1.
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

### » Sat Jan 21, 2012 4:38 am

Set a maximum force and minus the distance

so set force to 500-distance?
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

### » Sat Jan 21, 2012 4:49 am

[QUOTE=keepee]so set force to 500-distance?[/QUOTE]
Better be max(500-distance,0) so you don't get pulled away if farther than 500
B
83
S
21
G
15
Posts: 1,039
Reputation: 15,134

### » Sat Jan 21, 2012 4:52 am

@7soul yep but his condition was if it's below 500 so :p
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

### » Sat Jan 21, 2012 5:41 am

Physics says:
Gravity : G*mass1*mass2/d^2

That's basically the formula I used in MoonShield (see the demo page)
So you can put some mass instance variable in the ship and the black holes (never hurts to have too much parameter to tweak (: )
And then
[code]Global Variable G=10 //dunno just a constant to tweak
+System: Every Tick
local Variable xVector=0
local Variable yVector=0
+System: Every Tick
->System: set xVector to Ship.Physics.VelocityX
->System: set yVector to Ship.Physics.VelocityY
+System: Foreach Hole
+System: Every Tick
->Ship: Physics Apply Force : G*xVector,G*yVector[/code]
Should workYann2012-01-21 05:44:02
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

### » Sat Jan 21, 2012 7:17 am

Man, Yann! You never cease to amaze me...    Thanks everyone - that should work great.
B
192
S
6
G
7
Posts: 213
Reputation: 10,178

### » Sat Jan 21, 2012 7:23 am

Their might be a mistake in it, not sure that I should start with the velocity or 0... probably 0, so you just have to delete the "set" lines and just keep the foreach hole part and the apply force part.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

### » Sat Jan 21, 2012 7:30 pm

Ok, Yann. I removed the set events, but I am not getting a result. In my game I have Global gravity off and a constant 5 force down on the sprite. Does that effect it? what did I do wrong? Here is a capX to tweak...

Black Hole Example
B
192
S
6
G
7
Posts: 213
Reputation: 10,178

### » Sat Jan 21, 2012 7:59 pm

Last edited by Yann on Sun Sep 14, 2014 12:34 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Next