I made a 2d math library called Point. maybe some of you will find it helpful? =)

I know theres that math2d library by roaminggamer but it was missing some stuff that i needed and there was no way to add functionality to it.

My library allows you to think of coordinates as one unit and do things like

local distance = yourPosition.distanceTo(otherPosition)

check it out :slight_smile:

https://code.coronalabs.com/code/point-2d-math-library-simplifies-vector-math

Hey tameen,

thanks for sharing.

I don’t want to nit-pick, but I think the library has great potential for optimization.

Here are some of my thoughts:

  1. I don’t get why you create new objects for vectors. After all it’s just to values (x and y) most of the directly retrieved from an display object or other already existing variables. If needed it’s easy for the user to create a holding table for them if he needs to. You use the vector object to call the functions upon, but after all you could directly use the library for this. There is no need for an object oriented approach, as there’s no value gained in this case.

  2. Localize math library calls like math.pi, math.atan2 etc.

  3. You have way to many internal function calls inside your library, mostly because of the OOP approach mentioned above.

  4. Some essencial function are missing, like rotating a vector, checking for intersections or standard functions like cross and dot products. On the other hand there are other functions quite useless, like rounded(), which users can achieve pretty easy themselfs.

I hope this came not harsh, but I thinks you can do way better :slight_smile:

Greetings

Torben

  1. I create new objects for vectors because It allows chaining of methods on the object.  the code is much more readable in my mind

    local speed = 5 local angle = 90 local vector = point.newFromAng(angle):multiply(speed) obj.x, obj.y = point.newFromAddition(obj, vector):getPosition()

compare that to this:

local speed = 5 local angle = 90 local angleRad = (math.pi \* angle) / 180 local normalizedX = math.cos(angleRad) local normalizedY = math.sin(angleRad) -- move object obj.x = obj.x + (speed\*normalizedX) obj.y = obj.y + (speed\*normalizedY)

it’s nice to be able to think of a position as a unit and run series of commands on them vs assigning x and y over and over.  Being able to chain methods is cleaner (to me) than assigning a variable for each step.

  1. yeah thats a good point.  I will localize that stuff.

  2. i disagree with this.  i’m using the library within my game and it’s working very well and is producing more readable code.  not sure why internal function calls are a bad thing.

  3. I added the stuff i thought people would use most.  In any math library there are probably some things missing.  But I’m open to suggestions.  rounded is particularly useful for pixel art games where you sometimes dont want things moving in floats.  obviously the user can accomplish any of these things without a library but the idea is to prevent repetitive code.

First point can be accomplished without creating objects. To stay with you example:

local speed = 5 local angle = 90 obj.x, obj.y = point.vecAdd(obj.x, obj.y, point.vecScale(point.angle2vec(angle), speed))

This peace of code achieves the same result without creating tables, assigning metatables and removing them at the end.

I used the function names from my library, so don’t get confused by them.

You can even localize this functions if you use them often. Which is not possible with a OOP approach.

I not questioning that your code is working, it’s just that I think a math library should be as fast as possible, as it is used frequently in game logic. And as function calls, creating tables and indexing tables are all quite slow actions (one slower as the other), they should be cut to a minimum.

Thats cool.  Personally I think my example is a little more readable but maybe yours performs better. One of those things where its performance vs readability…  I like being able to chain methods on the same object vs passing one function into many other functions.

Yeah, that’s might be the case.

Despite all, I’m glad you shared this, as Corona is in need for a great 2D math library (I know there is the plugin, but it’s limited and there is no source code access).

Yeah exactly.  I would’ve just went with that but I wanted to add some my own functionality to it and could not access the source code… =/

Hey tameen,

thanks for sharing.

I don’t want to nit-pick, but I think the library has great potential for optimization.

Here are some of my thoughts:

  1. I don’t get why you create new objects for vectors. After all it’s just to values (x and y) most of the directly retrieved from an display object or other already existing variables. If needed it’s easy for the user to create a holding table for them if he needs to. You use the vector object to call the functions upon, but after all you could directly use the library for this. There is no need for an object oriented approach, as there’s no value gained in this case.

  2. Localize math library calls like math.pi, math.atan2 etc.

  3. You have way to many internal function calls inside your library, mostly because of the OOP approach mentioned above.

  4. Some essencial function are missing, like rotating a vector, checking for intersections or standard functions like cross and dot products. On the other hand there are other functions quite useless, like rounded(), which users can achieve pretty easy themselfs.

I hope this came not harsh, but I thinks you can do way better :slight_smile:

Greetings

Torben

  1. I create new objects for vectors because It allows chaining of methods on the object.  the code is much more readable in my mind

    local speed = 5 local angle = 90 local vector = point.newFromAng(angle):multiply(speed) obj.x, obj.y = point.newFromAddition(obj, vector):getPosition()

compare that to this:

local speed = 5 local angle = 90 local angleRad = (math.pi \* angle) / 180 local normalizedX = math.cos(angleRad) local normalizedY = math.sin(angleRad) -- move object obj.x = obj.x + (speed\*normalizedX) obj.y = obj.y + (speed\*normalizedY)

it’s nice to be able to think of a position as a unit and run series of commands on them vs assigning x and y over and over.  Being able to chain methods is cleaner (to me) than assigning a variable for each step.

  1. yeah thats a good point.  I will localize that stuff.

  2. i disagree with this.  i’m using the library within my game and it’s working very well and is producing more readable code.  not sure why internal function calls are a bad thing.

  3. I added the stuff i thought people would use most.  In any math library there are probably some things missing.  But I’m open to suggestions.  rounded is particularly useful for pixel art games where you sometimes dont want things moving in floats.  obviously the user can accomplish any of these things without a library but the idea is to prevent repetitive code.

First point can be accomplished without creating objects. To stay with you example:

local speed = 5 local angle = 90 obj.x, obj.y = point.vecAdd(obj.x, obj.y, point.vecScale(point.angle2vec(angle), speed))

This peace of code achieves the same result without creating tables, assigning metatables and removing them at the end.

I used the function names from my library, so don’t get confused by them.

You can even localize this functions if you use them often. Which is not possible with a OOP approach.

I not questioning that your code is working, it’s just that I think a math library should be as fast as possible, as it is used frequently in game logic. And as function calls, creating tables and indexing tables are all quite slow actions (one slower as the other), they should be cut to a minimum.

Thats cool.  Personally I think my example is a little more readable but maybe yours performs better. One of those things where its performance vs readability…  I like being able to chain methods on the same object vs passing one function into many other functions.

Yeah, that’s might be the case.

Despite all, I’m glad you shared this, as Corona is in need for a great 2D math library (I know there is the plugin, but it’s limited and there is no source code access).

Yeah exactly.  I would’ve just went with that but I wanted to add some my own functionality to it and could not access the source code… =/