2d Vector variables and operations?

Get help using Construct 2

Post » Thu Jan 10, 2013 2:34 pm

Hi, Construct2 noob here (only been playing with it a week but very impressed by the possibilites, so thanks!)

I'm trying to implement sprite movement that involves resolving various forces that are represented as 2d vectors. As an example, think of the classic BOIDs flocking example (http://www.red3d.com/cwr/boids/), in which the movement of each object is influenced by combining forces reflecting desire for alignment, cohesion, and separation.

To do this, I'd like to be able to assign arbitrary vector variables to an instance and then perform standard functions on those vectors (addition, multiplication, normalisation, dot and cross products etc.) in order to calculate the appropriate acceleration to apply. However, it doesn't seem possible to assign or manipulate vectors - the only types of instance variables available are Boolean, Text, and Number (and I really don't want to have to separate out my vectors into separate X and Y components...)

I see that the Custom Movement behaviour exposes properties such as dx/dy, acceleration, and Angle etc., which means I guess that Construct 2 must already have a 2d vector library internally that has all these functions, but am I correct in saying that it's not exposed anywhere else?

What would be the best way to implement this? A plugin? (has anybody already done one?)

Any advice would be gratefully received!
B
8
S
2
G
3
Posts: 83
Reputation: 2,668

Post » Thu Jan 10, 2013 3:17 pm

To my knowledge you can't indeed have vector type variables. I suspect Ashley prevented this for performances reason (there's a paragraph about vectors in this article he wrote about JS performances/optimisation).

You can probably use the "Function" plugin, write all your manipulation in it and "hide" the necessity to use several variables.

You can also use array or dictionary to hold several values.

I hope it helps.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,786

Post » Thu Jan 10, 2013 4:37 pm

(Ack! Wrote a long reply to this and then lost it. Edited version follows)

Thanks very much for the reply, and the link to the article.

Can the Function() object send back arrays/dictionaries as return values? I couldn't see how to do that, and if it can only return a single value I'm not sure if it will help other than to tidy up the event sheet slightly.

I'm not sure I agree with Ashley completely in that article. Seperating out the X and Y components in anything other than a trivial vector calculation generates a lot of extra work that probably more than offsets any gain from reducing object creation (IMHO). And performance isn't everything - I'd rather my code be understandable and use established vector computations rather than using an arcane method just to squeeze a little but more performance out of it!

Anyway, having established I'm not missing some "Add Vector2D plugin", I'm going to try different approaches to solving the problem and will post back here if I find anything useful to share. Thanks again.
B
8
S
2
G
3
Posts: 83
Reputation: 2,668

Post » Thu Jan 10, 2013 5:16 pm

The function won't return a reference or a full object, so no it won't return an array or dictionary.

You can "cheat" and return lists as string.

Like, have an instance variable "Vector" to your object and fill it with "X;Y" and float(tokenat(object.Vector, 0, ";")) to get X out of it, float(tokenat(object.Vector, 1, ";")) to get the Y. (float() and tokenat() are system expressions)
Not as straight forward either, but can be helpful.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,786

Post » Thu Jan 10, 2013 6:56 pm

I made some some boid examples here:
http://www.scirra.com/forum/boids-tests_topic59833.html

In the third example I used the function object to add all the vector operations and used an array to store all the vectors. I then made any variable be a vector by storing an index of the array, and only modifying the variable with the functions.
B
79
S
24
G
52
Posts: 4,724
Reputation: 39,711

Post » Thu Jan 10, 2013 8:05 pm

R0j0hound - thanks so much for pointing that link out - I completely missed that post when I first scanned through the forums. In fact, your series of three BOIDs examples nicely exemplifies many of Kyatric's suggested workarounds (and the reason why I'd rather work with vector math, as I'm sure you did!)

I think I've decided to go the custom behaviour plugin route - having had a quick look at the example behaviours it seems (relatively) straightforward to write a plugin, and a whole lot easier on the eye by encapsulating the logic in a separate code file (where I'm free to do vector math - mwa ha ha!) rather than on the event sheet.

My intention is to try to encapsulate several of Reynolds' steering behaviours - not just flocking as shown by the BOIDs example here, but seeking/fleeing/arrival/leader-following/queueing, for example.

If I get it to work, I'll let you know. Thanks!tanoshimi2013-01-10 20:06:35
B
8
S
2
G
3
Posts: 83
Reputation: 2,668


Return to How do I....?

Who is online

Users browsing this forum: Governador, tarek2, Thelegendvinny and 20 guests