Vector2 library as either plugin or behavior?

0 favourites
  • 5 posts
From the Asset Store
Strange Game Ambient Loops 2 library is a meticulously designed & game ready ambience loop library.
  • Hey all,

    I have a behavior for vector2 support. Its not ready to share, but before I put the work in to make it ready, I wanted to know if people would have any use for it and in what ways they would see themselves wanting to use it.

    Also, is there already a vector 2 math utility plugin or behavior out there that people use? (making this irrelevant?)

    Mine is a behavior just adds an x and y property to the object with getters and setters for changing components or the whole vector. It also has magnitude, magnitudeSquared, and some simple logic to ensure magnitude (and other properties) are never computed more than needed. I don't create any new objects at any time.

    If you wanted to calculate the unit vector for a vector you would do something like this:

    action: Set Vector (x,y) //if needed

    action: Normalize

    expression: x

    expression: y

    I have expressions/actions for the following:

    .normalized

    .normal

    .dot

    .projection

    .rotatePoint

    .rotateDirection

    .RotateVector

    .scale

    Would people find this sort of thing useful? Would it be better as a utility plugin rather than a behavior? Any thoughts, or suggestions before I flesh it out?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I built a vector2 set of functions. If I used them I ended up taking them apart for all the x,y calls all the time so in the end I am not sure how much code space it saved . If you are going to do it you may want sets of vectors for the parts of the moveto and pathfinder and collision outline type sets you end up working with.

    Find intersection point comes up a lot.

    I am not sure if you want a behavior or not, mine were just a bunch of stand alone functions.

    good luck in any case.

    yours

    winkr7

  • Good to know.

    The advantage of the behavior imo is often I'll be using it as a easy way to add a vector2d to the object. You can even add multiple ones.

    Since I'm already working with it, doing things like based on that vector means I only need 1 or two inputs, depending on the function.

    It also means, somethings, like get projection at angle, only takes one parameter, the angle. although I still have to read out the result property. Its all kinda of hassle no matter how you do it, but its more performant than construct functions, or js modules, since js blocks currently incur a huge overhead when used at volume.

  • I'm not superdeep into math stuff so I can't tell if I would need/use it. What is a specific usecase that this plugin solves that isn't in the base engine? Most plugins already have vectors and I can use atan2 (or angle expression in construct) to get the angle of the vector and vice versa.

    It sounds like a barebones movement plugin like the build in custom movement behavior.

  • ...What is a specific usecase that this plugin solves that isn't in the base engine? Most plugins already have vectors and I can use atan2 (or angle expression in construct) to get the angle of the vector and vice versa.

    It sounds like a barebones movement plugin like the build in custom movement behavior.

    Vector normals and projection are super cool and solve alot of issues very nicely in games. They aren't particularly complicated, but if you need to use them alot then it makes sense to have them wrapped up. A dot product is simply ax * bx + ay* by, but that can be verbose in a project - for example, to obtain a vector rotation, I might have to do something like this:

    localVarX = Sprite.instVarX * SomeotherSprite.InstVarX + Sprite.instVarY * SomeotherSprite.InstVarY;

    localVarY = Sprite.instVarX * SomeotherSprite2.InstVarX + Sprite.instVarY * SomeotherSprite2.InstVarY;

    (where SomeotherSprite represents a unit vector created earlier or replaced with sin/cos(a)

    Much better to simply have something like:

    Sprite.v2.TransformDirection(SomeOtherSprite.v2.x, SomeOtherSprite.v2.y);

    //Results stored in: Sprite.v2.x & Sprite.v2.y

    You can also do neat things with unitVectors (a vector whose length = 1) and the dot product and cross product. Also, multiplying a unit vector by a scaler is a simple oneliner: Sprite.v2.Scale(scaler); Since there is both expressions and actions that do the same thing, you can set x/y of the v2 while also retrieving the result. It just reduces verbosity, and imo improves clarity, because unlike the dot product shown above, used twice to calculate a rotation, the function is named what it does so no need to adda bunch of comments explaining what maths are being used.

    For example: If I had a boat, and I wanted to know it's velocity in the lateral direction in order to apply friction in that direction, I could do so with vector projection. Custom movement handlers are big candidates for needing vectors and vector math. But I even use it for input and a few other items. If you treat up,down,left,right input as a vector 2, you normalize it and then scale it by acceleration. Dealing with rotating gravity, identifying slope normals, and relative spaces/directions all need vector math.

    You are most correct though, with the last statement, as it is, many times the need involves custom movement.

    Oh, as an aside, you can use vectors to help infuluence, or steer, any other behavior. For example, using 8Dir, you could add up a bunch of influencing forces and calculate what direction to simulate to the behavior...

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