Physics/Geometry Calculation

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
DiegoG
Prole
Posts: 20
Joined: Sat Apr 29, 2017 2:43 am
Location: Venezuela

Physics/Geometry Calculation

Post by DiegoG »

Welp, I've been working on a game for about 3 months now (After spending 2 months making several tiny projects to get used on Game Developing and LÖVE); and this is really the first time I decide to go on the forums and ask a question so I'm not sure if this is even the correct place to ask this. Anyway...

We all know the love.physics module; and we all know (As explicitly said in the wiki) it's a "Ten-ton hammer designed for heavy-lifting (er...hammer...lifting?)"; so naturally I decided to make my own (partially) physics implementation
I used HC to save me the trouble of making a collision system and focus on solving those collisions; and finally here's the question:

How do I calculate force direction?, and just to make sure I use the correct method: How do I express it as a vector (specifically, coordinates)?

Any helpful input is very appreciated; and thanks in advance! Sorry if I seem like a noob.
function earth:destroy()
00count=5000
00while count>0 do
0000lg.draw(explosion,math.random(0,600),math.random(0,800))
0000count=count-1
00end
00earth = nil
00print("Earth has been destroyed.")
end
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Physics/Geometry Calculation

Post by raidho36 »

Force is applied at a normal between colliding faces, i.e. orthogonal to the separation line. Calculate the separation line, then rotate it 90 degrees (pro tip: just swap out X for -Y and Y for X).

You can represent it as vector by storing X fraction of direction vector in X component and its Y fraction in Y component. Programmatically, you can use a table with .x and .y fields. Note that it'll work a good bit faster if you use array indices instead of literal indices, but not if you use FFI - then it's the same, but with literal indices it's a lot easier to set up and use.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Physics/Geometry Calculation

Post by airstruck »

If your bodies can also rotate, you'll need to calculate torque as well as force for it to feel halfway decent. There are tons of articles on this, just google something like "torque rigid bodies."

As for storing the vectors, keep in mind that functions can return multiple values in Lua, so another option is simply storing x and y in two separate variables. In any case you can write your vector functions like this, so they don't need to know how your vectors are stored. For example if you store them in a table you can write t.x, t.y = func(t.x, t.y), and if you decide to store them differently later, your vector functions don't need to change (they also don't need to be concerned with in-place ops vs. copying). This allows you to store multiple vectors in the same table, for instance individual components of "velocity" and "acceleration" vectors can be stored directly in an "entity" table instead of needing an entity table plus another table for each vector.

Of course, if you're already using a library of vector functions that expects vectors to be stored in some particular way, you should store them however the library wants them.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 96 guests