O'Reilly logo

GameMaker Cookbook by Brandon Gardiner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Applying force via magnets

Remember playing with magnets in science class when you were a kid? It was fun back then, right? Well, it's still fun; powerful magnets make a great gift for your favorite office worker. What about virtual magnets, though? Are they still fun? The answer is yes. Yes, they are.

Getting ready

Once again, we're simply going to modify our existing physics environment in order to add some new functionalities.

How to do it

  1. In obj_control, open the code block in the Step event.
  2. Add the following code:
    if keyboard_check(vk_space)
    {
    with (obj_dynamicParent)
        {
        var dir = point_direction(x,y,mouse_x,mouse_y);
        physics_apply_force(x, y, lengthdir_x(30, dir), lengthdir_y(30, dir));
        }
    }  

Once you close the code block, you can test your new ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required