Gravity

This forum is currently in read-only mode.
  • Hi,

    I need help again <img src="smileys/smiley17.gif" border="0" align="middle" /> . How to make the object magnetic to other object with gravity? I want to do in the likeness of a black hole. thanks! <img src="smileys/smiley17.gif" border="0" align="middle" />

    <img src="http://screenshot.su/img/f4/02/02/f4020246e68efce23fd92731e7a506df.jpg" border="0" />

  • 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 :)

  • 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. <img src="smileys/smiley2.gif" border="0" align="middle" />

  • 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.

  • I meant, it doesnt work for me, because I have a black hole appears randomly in the area, as in Geometry wars <img src="smileys/smiley1.gif" border="0" align="middle" />.So your idea will not work for me.

  • 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

  • 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.

  • 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 btw

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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.

  • i didn't know clamp or lerp till construct

    but yeah clamp is like this:

    clamp(input,lowest,highest)

    it just gives you back the 'input' number, unless it's out of the range of highest and lowest. if it is, then it just forces it into that range

    clamp(5,0,10)==5 (not lower than 0 or higher than 10)

    clamp(3,0,10)==3   (not lower than 0 or higher than 10)

    clamp(-3,0,10)==0 (too low, force it into range)

    clamp(1000,0,10)==10   (too high, force it into range)

    lerp(a,b,t)

    lerp gives you a number between a and b

    and it uses t to determine how far along it is between a and b

    think of t like a percentage, but with a decimal in front of it

    t==0 is like 0%   (t==0)

    t==1 is like 100% (t==1.00)

    t==0.54 is like 54%

    t==0.3 is like 30%

    lerp(a,b,t)

    so if t is at 0 you get back 'a'

    if t is at 1 you get back 'b'

    and if t is anything in between 0 and 1, it's that far along the line between a and b

    lerp(0,4,0)==0

    lerp(0,4,0.25)==1

    lerp(0,4,0.50)==2

    lerp(0,4,0.75)==3

    lerp(0,4,1)==4

    I made a short tutorial about lerp that helps some people if the explanation didn't work, it's more interactive and visual:

    http://69.24.73.172/scirra/forum/viewtopic.php?f=8&t=8735

  • posted something just now without noticing this post, so read the post above if you're just reading this

    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.

    all the usable expressions you can get when you're in that expression editor(anywhere you can type these expressions)

    doubleclick on the system icon, and it gives you a list

    this list is also handy:

    http://sourceforge.net/apps/mediawiki/construct/index.php?title=System_Expressions

    the distance expression returns the distance between two points

    distance(point1x,point1y,point2x,point2y)

    it's the same as using the pythagorean theorem, but that doesn't matter, because you don't need the details to make it work. but it returns one value representing the distance between the first and second point

    so you can use it to find out how far away two objects are in pixels

    as far as why use the division problem,

    when you divide any value by some maximum value, you get a decimal value that represents what portion of the whole you have like

    let's say your max is 4

    0/4==0

    1/4==0.25

    2/4==0.5

    look familiar?   it works in alot of situations to give you that perfect value for t in

    lerp(a,b,t)

    we used clamp, because we want to force the values to stay between 0 and 1, so the speed we choose for things getting sucked in the black hole won't get blown away from the black hole when they get too far because

    lerp works with larger and smaller numbers for t

    like lerp(0,10,2.0)==20

    or lerp(10,0,2.0)==-10

    or lerp(0,10,-0.5)==-5

  • Wow thank you very much for spending your time to explain all these stuff to me. You have explained all of them perfect. I will read twice, three times, until It sticks in my mind :) Really amazing to know these expressions, functions and especially to use them in neccessary areas. If I had knew these lerp and clamp before, I'm prety sure that I couldn't think of using them for this gravity effect :)

    See you soon :) I'll try and play with these. Thank you for teaching me :)

  • glad it helped. enjoy!

  • i think this thread is kinda old, so i dont know if i should bump it. but i'm just gonna ask anyways i guess. can i?

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