2d Vector variables and operations?

0 favourites
  • 6 posts
From the Asset Store
FPS on Rails Template
$31.96 USD
20% off
Create a game inspired by great arcade classics as Operation Wolf
  • 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!

  • 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.

  • (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.

  • 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.

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)