Physics Tools Extension (Beta Release 4)

merrak

  • *
  • Posts: 2738
Physics Tools Extension

"Physics Tools Extension" provides some additional blocks for working with the Box2D environment. It includes tools for precision trajectory calculations and collision detection on a curved path.


The purpose of the "Physics Tools Extension" is to make it easy to perform routine calculations. Some example problems include:

  • You want an actor to jump exactly 6 tiles across and 4 tiles high. What should you set the velocity and angle to?
  • You want an enemy actor to fire at your player using a curved path. Will it be able to, or is there an obstacle in the way?
  • You want an enemy actor to jump at your player. How much force should it use to jump?
  • You want to draw an arc that shows what path a projectile may follow.

Download

The extension can be downloaded here (.zip)here (.tar.gz), or from my site (.zip or .tar.gz).

A sample project file can be downloaded here or from my site.

Old Releases are available from my Archives Page

Please see: How to Install and Update an Extension.

Quick Reference

Documentation is provided below. The documentation can also be found on my site.


Returns direction of motion


Push the actor with enough force to clear the specified distance in pixels (e.g. jump)


Push the actor with enough force to clear the specified distance in tiles (e.g. jump)


Returns the first actor on the trajectory from x, y at given velocities. The time interval (see illustration below) will specify the section of the arc to search for a collision within.




Returns a list of all actors on the trajectory from x, y at given velocities. The time interval will specify the section of the arc to search for a collision within.


Returns the projected x or y position of the actor n seconds into the future.


Returns the velocity with specified horizontal and vertical speeds


Returns the projected x or y coordinate of a moving body from starting x or y coordinate and velocity n seconds into the future


Returns the x or y speed necessary to launch a body from (x1,y1) to (x2,y2) at an angle of n degrees.



Note: "angle" is used to determine the highest point the projectile will reach before it descends. The image below illustrates the effect "angle" has on the path. A rectangular box is drawn, the width of which is the horizontal distance between the projectile and target. The angle between the two diagonals determines the height.

To form a trajectory that is as high as is far, select an angle of 45 degrees.



Launch an actor to a specified point. The "angle" parameter behaves as explained above.


Returns true if the "angle" parameter, as used above, is sufficient.



In some cases, the "angle" parameter may be too low for the computed trajectory height to reach the intended target. In this case, two errors may be seen if a launch is executed: "PhysicsTools.launchVelocity: Negative discriminant." or "PhysicsTools.doLaunch: Angle insufficient to reach target height."

This block allows the developer to check for this condition before attempting a launch.


Returns the horizontal speed (x) or vertical speed (y) component of velocity in a given direction.


Returns the angle formed by the diagonal connecting (x1,y1) and (x2,y2).


Returns the first actor on an arc from (x1,y1) to (x2,y2) at specified velocity.


Returns a list of all actors on an arc from (x1,y1) to (x2,y2) at specified velocity.


Returns the angle of motion to hit a target at (x2,y2) at a specified velocity.


Version History

2015-10-24:  (Beta Release) Added 10 blocks

Beta release notes: These ten blocks solve the problem I had in my own game (that motivated this extension) and some problems I remember from browsing the forums over the past few months. I can add more if requested.

Please note that this is the beta release. There may be a few bugs that slipped through the cracks. Please let me know if you find any  8)

One block you will not see is a simple "launch from point A to point B". The reason is that more information is needed to determine a trajectory: either the length of time the projectile will take to complete its flight, or the highest point the projectile will reach. I chose to implement the latter, in the form of the "angle" parameter, since it should be easier to visualize.

"angle" essentially determines the aspect ratio of a box the arc path will reside in. (e.g. a 45-degree angle gives a 1:1 ratio of distance horizontal to distance vertical).

2015-10-26:  (Beta Release 2) Added two blocks: ("x/y-speed with given velocity" and "aspect angle")

2015-10-29:  (Beta Release 3) Two updates:
1. Added one block: ("all actors on arc")
2. Converted * 10 and / 10 constants to proper functions (toPixelUnits and toPhysicalUnits)

2015-11-24:  (Beta Release 4) Added three blocks (requested by Liberado)
1. First actor on arc from (x1,y1) to (x2,y2) at angle A
2. All actors on arc from (x1,y1) to (x2,y2) at angle A
3. Angle of velocity to hit a target from (x1,y1) to (x2,y2) with given velocity V

« Last Edit: November 23, 2015, 10:39:50 pm by merrak »

havana24

  • *
  • Posts: 508
Seems amazing, thanks! :)
My Website: www.havana24.net

SadiQ

  • Posts: 1795
You linked the tar.gz file in the link you posted here.
Great job with this one! It will come in handy.
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

LIBERADO

  • *
  • Posts: 2720
Thank you! Another useful gem for my collection.
I'm spanish, excuse me for my bad English.
I'm not a private teacher. Please, post your questions in the public forum.

LIBERADO

  • *
  • Posts: 2720
I'm spanish, excuse me for my bad English.
I'm not a private teacher. Please, post your questions in the public forum.

MichaelPel

  • *
  • Posts: 458
Oh my god this is perfect! You have solved countless potential problems for many, and I myself could use it as well! Thank you so much for making this masterpiece

LIBERADO

  • *
  • Posts: 2720
I am really surprised by the accuracy and good performance of this extension when drawing paths.
It is an excellent extension. I only miss the block that I mentioned in my previous post.

http://static.stencyl.com/games/32401-0.swf

« Last Edit: September 24, 2016, 02:38:41 pm by Irock »
I'm spanish, excuse me for my bad English.
I'm not a private teacher. Please, post your questions in the public forum.

merrak

  • *
  • Posts: 2738
You linked the tar.gz file in the link you posted here.
Great job with this one! It will come in handy.

Thanks! I added a direct link to the .zip file.

I can add more if requested.
I miss this block: 

I updated the package today. It now includes this block.  8)

LIBERADO

  • *
  • Posts: 2720
I updated the package today. It now includes this block.  8)
Really? Thank you very much!!! :D

(downloading)
I'm spanish, excuse me for my bad English.
I'm not a private teacher. Please, post your questions in the public forum.

Nikkita31

  • Posts: 131
Yo Merrak.

This is absolutely helpful! I'm really glad you shared this wonderful stuff.   8)

-Renegade Games

LIBERADO

  • *
  • Posts: 2720
How can we download the Beta Release 2 ?
When I use the links in your first post, the old Beta Release 1 is downloaded and the new blocks don't appear.

« Last Edit: October 26, 2015, 09:36:58 pm by LIBERADO »
I'm spanish, excuse me for my bad English.
I'm not a private teacher. Please, post your questions in the public forum.

merrak

  • *
  • Posts: 2738
Whoops!-- I refreshed the files the links point to, so it should work now.

I also need to change the rest of the 10 constants to the proper functions-- toPhysicalUnits and toPixelUnits. It shouldn't be an issue for now, but if Stencyl ever changes the constant as defined in the engine, then the beta release 1 and 2 versions won't work.

Glad to hear it's been helpful!

LIBERADO

  • *
  • Posts: 2720
Whoops!-- I refreshed the files the links point to, so it should work now.
Yes, it works now, thanks!

I also need to change the rest of the 10 constants to the proper functions-- toPhysicalUnits and toPixelUnits.
Please, let us know when they are changed. I would not like this useful extension to stop working someday.

Glad to hear it's been helpful!
It is VERY helpful.  ;)

« Last Edit: October 26, 2015, 10:02:00 pm by LIBERADO »
I'm spanish, excuse me for my bad English.
I'm not a private teacher. Please, post your questions in the public forum.

LIBERADO

  • *
  • Posts: 2720
@merrak, Oops! After uninstalling the Beta Release 1 and installing the Beta Release 2, the sample game shown in my previous post doesn't work, and "Clean Project" doesn't solve the problem.

Stencyl throws this error when I try to test the game:


« Last Edit: October 26, 2015, 11:12:59 pm by LIBERADO »
I'm spanish, excuse me for my bad English.
I'm not a private teacher. Please, post your questions in the public forum.

merrak

  • *
  • Posts: 2738
Dang  :-[  Easy fix... just a misplaced }

I was about to ask myself how that slipped through the cracks, since it compiled fine on my end when I tested it. I realized I must have packaged the wrong file, since it also had the angles for the two new blocks in radians--instead of degrees. Sorry about that. I updated the link again. The correct .hx file has the angles for the two new blocks in degrees. You should be able to drop the new .hx file in without restarting Stencyl. I didn't need to update the blocks.xml file.