Black hole physics

0 favourites
From the Asset Store
A simple fun and stylish endless scroller game ready to be customized and published.
  • 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.

    Help Please...

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

  • Set a maximum force and minus the distance

    so set force to 500-distance?

  • so set force to 500-distance?

    Better be max(500-distance,0) so you don't get pulled away if farther than 500

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 7Soul yep but his condition was if it's below 500 so :p

  • 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

    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: add to xVector cos(angle((Ship.X,Ship.Y,Hole.X,Hole.Y))*Ship.mass*Hole.mass/distance(Ship.X,Ship.Y,Hole.X,Hole.Y)^2
        ->System: add to yVector sin(angle((Ship.X,Ship.Y,Hole.X,Hole.Y))*Ship.mass*Hole.mass/distance(Ship.X,Ship.Y,Hole.X,Hole.Y)^2
      +System: Every Tick
        ->Ship: Physics Apply Force : G*xVector,G*yVector

    Should work

  • Man, Yann! You never cease to amaze me...    Thanks everyone - that should work great.

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

  • 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

  • Thanks again Yann. Looks like I was using wrong mass :)

    A very good example for physics. This capx could be useful for many projects.

  • rexrainbow has made a "Gravitation" Behavior that allows for a similar effect. It will behave differently because the force is constant and not calculated based on the distance between objects or the mass of the objects. Heavier objects will not pull harder, but they will still resist being pulled as quickly due to inertia. Even though it behaves differently, it is similar and quite easy to try out.

    You can find the Gravitation Behavior here:

    scirra.com/forum/behavior-gravitation-for-physics-behavior_topic53035.html

  • Cowdozer

    Post Diggers ... :(

  • Lol! If you start a new post, you get bounced to this one.

    If you raise a thread from the dead, you get called up on it.

    Can't win with some people <img src="smileys/smiley1.gif" border="0" align="middle" />

    I for one hadn't seen this thread, so thanks!

  • LOL, I've never been chastised for digging up an old thread! There's a first time for everything.

    Anyway, this post is referenced in the FAQ, so I would argue that it's something that people are going to see even though it's an old post, and it's worthwhile to direct people to an alternate solution.

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