Multi-Resolution Game Development With GameBuilder Studio Check it out!
NEW Version 0.9.7 has arrived! New Blazing Fast Particle Engine & More Game Actions. Check it out
Get angle from mouse x mouse y
  • Hi, as the title says.. If i want to spawn an object and have it move to the mouse x & y, do i use change velocity or move to ?
    How do i get the angle out of the mouse x & y ?
  • 3 Comments sorted by
  • @Andreas, Using the velocity to move an object is the ideal method. You can do this easily with the change velocity action and it will apply a constant speed in a given direction. You can also change the velocity of an object manually by setting a point object on the linearVelocity property of a spatial component using the point helper method in an expression (i.e. setPoint(50, 0) ).

    To get the angle of the mouse relative to another object's position you can use the rotationOfAngle() helper method in an expression:

    rotationOfAngle( setPoint( Game.Mouse.x, Game.Mouse.y ), Self.Spatial.position)

    This helper function/method accepts an x/y point object so you will need to use the setPoint(X, Y) method to create a point object with the X and Y values. The position property on a spatial is already a read-only point object so you can pass the position of a spatial component directly.

    If you want to move an object along an angle or in a certain direction you can look at the firing turret demo. The bullet being fired by the canon moves in the direction of the canon's rotation at a certain speed.

    I just found a bug in the change velocity action so I've attached an updated plugin file which needs to be copied to the GameBuilder Studio installation folder on your mac. Unzip the .swc file go to Applications or wherever you installed GBs and open the GameBuilder Studio install folder. Right click on the app and select "Show Package Contents". Copy the unzipped .swc file to Contents -> Resources -> plugins. This fix will be included in the next update automatically.
  • Thanks Lavon, i'm trying to make a canon shoot canonballs at the position of the mouse x and y.
    I can't download the attached file. When clicking on it nothing happens and right-click only give me the option to save the image…

  • @Andreas, If you want to make a cannon shoot you should just look at the firing turret example project that comes with GBs. It does just that. Here is the file link directly

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion