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
- In
obj_control
, open the code block in the Step event. - 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 magnet. Add some objects, hold down the spacebar, and see what happens.
How it works
Applying a force to a physics-enabled object in GameMaker will add a given value to the direction, rotation, and speed of said object. Force...