# Gravity

### » Mon Jul 18, 2011 12:56 pm

Hi,
I need help again . How to make the object magnetic to other object with gravity? I want to do in the likeness of a black hole. thanks!
B
2
G
1
Posts: 10
Reputation: 470

### » Mon Jul 18, 2011 3:16 pm

Well I had done something like that with same logic. But for different reason. I had an arena which I wanted to give it depth. So if player go far from the center and don't move, it slowly goes to the center.

I did it like this way;

If object.x > center.x than
move 1pixels at 180 degrees

If object.x < center.x than
move 1pixels at 0 degrees

If object.y > center.y than
move 1pixels at 270 degrees

If object.y < center.y than
move 1pixels at 90 degrees
-------------------------------

It handles what you want a bit. But it's not perfect. Needs smth else. Beceause for example:

if object x is 40pixels far from center x and object y is 100pixels far from center y than. it adds 1 to x and y at the same time. So first it reaches to true x point than it contunious to y increasement. So it doesn't look good. Beceause first it goes up-right than it goes only right.
--------------------------------

There maybe nice solution for your need. I'm looking foward to learn it too. I can use it on my game to fix my problem :)
B
7
S
1
G
1
Posts: 76
Reputation: 904

### » Mon Jul 18, 2011 4:19 pm

Thanks for the comment, but this doesn`t work for me. At first I did it with the plug-Gravitation, but there were a lot of bugs, then I decided to write to the forum. As a result, I made it with the RTS movement.it does not work perfectly, but not bad.
B
2
G
1
Posts: 10
Reputation: 470

### » Mon Jul 18, 2011 5:53 pm

I meant this; I prepared a demo for you.

spacedemo.cap

http://hotfile.com/dl/124408509/d8c1d52/spacedemo.cap.html

You can control spaceship with arrows.Spider_hip2011-07-18 17:55:05
B
7
S
1
G
1
Posts: 76
Reputation: 904

### » Mon Jul 18, 2011 6:05 pm

I meant, it doesnt work for me, because I have a black hole appears randomly in the area, as in Geometry wars .So your idea will not work for me.
B
2
G
1
Posts: 10
Reputation: 470

### » Mon Jul 18, 2011 6:57 pm

Are you using any behaviors? If so there may be another solution that works more elegantly with your behavior, but this would work

Stuff ->move object toward position blackhole.x, blackhole.y, at whatever speed

Depending on whether you want an an event horizon where you have a minimum range, or infinitely decrease the pull the further you get, or constant pull all require slightly different formulas for the speed you choose
Spriter Dev
B
103
S
22
G
12
Posts: 3,286
Reputation: 17,153

### » Mon Jul 18, 2011 10:12 pm

Why not ? You don't have to use specific values for gravity. You can easily use your blackhole's x and y positions. So when you spawn or create them on random positions gravity position changes with them.

I prepared a second example for this;

spacedemo-0.01.cap

http://hotfile.com/dl/124424992/379f635/spacedemo-0.01.cap.html

I know it's not realistic as I mentioned on my previous post but you can make it more realistic with being a bit more specific with codes. I mean you can expand it. For example you can create a wide round object around your blackhole. And you can write like, if object collides with round object than start gravity. So if your map will be big, you can handle gravity areas for your blackholes. Than you can expand x+1 and y+1 codes. Like if y distance greater than x distance than y+2 x+1. So maybe you can prevent that issue. Or you can think about something else.
B
7
S
1
G
1
Posts: 76
Reputation: 904

### » Mon Jul 18, 2011 11:06 pm

you really don't need all that complexity, here's a simple example
if you don't recognize 'clamp' and 'lerp', don't get intimidated, just ask, it's pretty straightforward

http://dl.dropbox.com/u/1013446/blackhole.cap

you can safely ignore everything in start of layout and that loop, that's just to get a bunch of multicolored whatsits all over the place

just the always event matters for the blackhole
and the blackhole is drag and drop btwlucid2011-07-18 23:09:30
Spriter Dev
B
103
S
22
G
12
Posts: 3,286
Reputation: 17,153

### » Tue Jul 19, 2011 9:33 am

Omg, youre amazing. And you call mine complex :) Omg Yeah it maybe easy for you but I had never heard about clamp and lerp. I envy you. I wish I had learnt them when I was at school.

Our teachers taught us sin,cos,tan as reading book. I always asked them, how can I use them in reality, how can I see results if I use them. They never told me that I can use in game/animation programming. Now even jumping with sin , or making a round object with, sin sounds too weird for me.

Whatever :) It works perfect. Thank you very much.

I searched a bit about "clamp" and "lerp".

Lerp only interpolates between 0.0 and 1.0

Clamp a value between a minimum float and maximum float value. If value is higher than max value, than result equals to max value, same for min value.

I quite understand them in theory but It's still difficult for me to understand it on runtime.

Thank you, thank you, thank you. I hope you can share more examples like this if you have spare time.
B
7
S
1
G
1
Posts: 76
Reputation: 904

### » Tue Jul 19, 2011 9:45 am

I couldn't get distance function. How can you divide a value which includes 4 values to a value ?

distance(Sprite2.x,Sprite2.y,Sprite.x,Sprite.y)/global('maxdistance')

Is there any document that I can find all usable functions on construct with their meanings ?

Thank you very much.
B
7
S
1
G
1
Posts: 76
Reputation: 904

Next