Improved attach_arrow

60 views
Skip to first unread message

Bruce Sherwood

unread,
Oct 11, 2021, 10:13:21 PM10/11/21
to Glowscript Users
From the Help on "Attach a Trail or Arrow", below are new capabilities of attach_arrow. Two improvements have been made: 

1) You can change the properties of an attached arrow after creating it.

2) There is negligible slowdown if you repeatedly execute the same attach_arrow statement, because it is recognized as referring to the same arrow as before.

attach_arrow

attach_arrow creates an arrow representing a vector attribute of an object, and the arrow is continually displayed at the current location of the moving object. This arrow contains some additional information compared to an ordinary arrow, such as what object it is attached to and which attribute of that object is being represented by the arrow. The following statement will automatically display a green arrow at the current location of the ball, as the ball moves. The arrow will have shaftwidth=0.3, and its axis is determined by scale*ball.axis:

attach_arrow(ball, "axis", scale=1e3,
         shaftwidth=0.3, color=color.green)

The default value of scale is 1, the default color of the arrow is the color of the ball, and the default shaftwidth is 0.5*ball.size.y. You can specify any vector attribute of the moving object, not just "axis". For example, if you continually update a vector quantity ball.velocity and want to continually display an arrow representing this quantity at the location of the moving ball (with scale=1 and the color of the ball), just say this:  

attach_arrow(ball, "velocity")

It is usually sufficient to attach an arrow to a moving object just once, before starting the motion. However, you can modify the appearance of the arrow later by doing this, using color as an example:

myarrow = attach_arrow(ball, "velocity", color=color.green)
...
myarrow.color = color.red # change the color of the arrow

The following scheme will also work, because specifying the moving object (ball) and the attribute of interest ("velocity") is sufficient for identifying the appropriate arrow to modify:

attach_arrow(ball, "velocity", color=color.green)
...
attach_arrow(ball, "velocity", color=color.red)


You can start and stop displaying the arrow:

myarrow = attach_arrow(ball, "velocity")
...
myarrow.stop()
...
myarrow.start()

Bruce Sherwood

unread,
Nov 21, 2021, 8:37:15 PM11/21/21
to Glowscript Users
The description here isn't quite right. see 


Bruce

Reply all
Reply to author
Forward
0 new messages