Z axis gravity

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Djent
Prole
Posts: 27
Joined: Sun Jan 20, 2013 3:22 pm
Location: Rhode Island

Z axis gravity

Post by Djent »

This may seem redundant seeing as Love is a 2D graphics library, but is there a way to get a bird's eye view of a ground with entities on it? I'll preface this by stating everything is still going to be 2D.

I'd like to design a pool table using love physics, but there is only x gravity and y gravity when setting the world. Would it be easier to just program my own physics(which I'd like to think I'm capable of doing)?

I've tried setting gravity on both to zero - but friction no longer functions.

If there's no (easy) way to either add downwards gravity to love physics, any suggestions on writing my own force, inertia, momentum, friction stuff?

If you think I'm overreaching the community's support, feel free to not reply.
10$man
Citizen
Posts: 77
Joined: Sun Apr 22, 2012 10:40 pm

Re: Z axis gravity

Post by 10$man »

Hey, why do you need gravity for a pool game in the first place?
I'm not sure how it works in Love, but just set the friction for X and Y directions to both be the same.
You don't need gravity on the Z axis unless you can cause the ball to hop up and down or something (That's up and down relative to the pool table).


Creating your own velocity and friction is relatively easy.

Here's some psuedo code as I suck at giving explanations:

Code: Select all


--Check for Collision
--If so, split the velocity between the two objects (Most likely giving more to the ball that was collided with).


if Velocity_X < 0 then 
Velocity_X = Velocity_X + Friction
else
Velocity_X = Velocity_X - Friction
end

--Repeat for Y

Ball_X = Ball_X + Velocity_X
Ball_Y = Ball_Y + Velocity_Y
That would be a rather simple implementation.
User avatar
Djent
Prole
Posts: 27
Joined: Sun Jan 20, 2013 3:22 pm
Location: Rhode Island

Re: Z axis gravity

Post by Djent »

10$man wrote:Hey, why do you need gravity for a pool game in the first place?
I'm not sure how it works in Love, but just set the friction for X and Y directions to both be the same.
You don't need gravity on the Z axis unless you can cause the ball to hop up and down or something (That's up and down relative to the pool table).


Creating your own velocity and friction is relatively easy.

Here's some psuedo code as I suck at giving explanations:

Code: Select all


--Check for Collision
--If so, split the velocity between the two objects (Most likely giving more to the ball that was collided with).


if Velocity_X < 0 then 
Velocity_X = Velocity_X + Friction
else
Velocity_X = Velocity_X - Friction
end

--Repeat for Y

Ball_X = Ball_X + Velocity_X
Ball_Y = Ball_Y + Velocity_Y
That would be a rather simple implementation.
Love physics requires a x direction gravity and a y direction gravity when creating a physics "world." This makes it act sort of like a sidescroller. I don't want my pool balls falling anywhere because of gravity - only moved by a player's shots. Creating a physics world with an x and y gravity of 0 makes the balls have no friction at all and continue moving at a constant speed until they hit something. This is obviously not what I'm looking for.

I'm making this as part of a physics project I have to do, so the physics need to be as close to realistic as possible (disregarding small factors like wind resistance that would just clutter my code and make less than a pixel's worth of difference when running).

Right now, I'm working to make my own friction and speeds and whatever, completely separate from love physics, unless I get a better idea from you or someone else.
User avatar
xXxMoNkEyMaNxXx
Party member
Posts: 206
Joined: Thu Jan 10, 2013 6:16 am
Location: Canada

Re: Z axis gravity

Post by xXxMoNkEyMaNxXx »

kikito made a top down world camera, if that helps... I think that you're stuck writing your own physics though :|
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Z axis gravity

Post by substitute541 »

Um, here's a little thing : Don't rely too much of Love2D's physics engine. Try researching around on how to implement your OWN physics engine. Here's a helpful page by Eric Leong about circles colliding angled lines. There is also a page for multiple circles colliding. You just have to basically implement it in code (and infact, Eric Leong implemented most of his math stuff in Java).

Edit : Um, I want to be more helpful so, here's some code example in Lua. This is my second time explaining how to create your own basic physics engine.

You can move an object in a constant speed by adding it's coordinates to a velocity vector. Vectors are sorta like arrows pointing in a direction. A simple velocity vector can be this :

Code: Select all

velocity = {}
velocity.magnitude = 0
velocity.angle = 0
Often you want to know the x/y components of the vector. To do that, you need to use some mathematical formulas. But don't run away yet. Here are the formulas.

Code: Select all

velocity.x = math.cos(velocity.angle) * velocity.magnitude
velocity.y = math.sin(velocity.angle) * velocity.magnitude
Okay, now, to do the opposite, converting the components to the magnitude and angle, just do this :

Code: Select all

velocity.magnitude = math.sqrt(velocity.x^2 + velocity.y^2) -- equivalent is (velocity.x^2 + velocity.y^2)^0.5
velocity.angle = math.atan2(velocity.y, velocity.x)
Now, to change the position of an object, just add it's coordinates to the x/y components of the velocity vector. To make things the same for any frame-rate, there's a "dt" variable in love.update. Just multiply (total) velocity by the dt variable first before adding.

Code: Select all

obj.x = obj.x + velocity.x*dt
obj.y = obj.y + velocity.y*dt
Same is true for changing the velocity. Just add the acceleration components to the velocity components.

Code: Select all

velocity.x = velocity.x + accel.x*dt
velocity.y = velocity.y + accel.y*dt
Now, back to your problem of Friction, it's actually pretty easy. Friction is just acceleration pointing in the opposite direction of the velocity vector, though, it changes to 0 once the object stops. Since this can be a pain to think about it yourself (the object can go backwards), I added a sample code.

Code: Select all

local frictionX = math.cos(obj.vangle) * friction
local frictionY = math.sin(obj.vangle) * friction
if obj.vx > 0 then
    obj.vx = obj.vx + frictionX*dt
    if obj.vx < 0 then obj.vx = 0 end
elseif obj.vx < 0 then
    obj.vx = obj.vx - fricitonX*dt
    if obj.vx > 0 then obj.vx = 0 end
end

if obj.vy > 0 then
    obj.vy = obj.vy + frictionY*dt
    if obj.vy < 0 then obj.vy = 0 end
elseif obj.vy < 0 then
    obj.vy= obj.vy - fricitonY*dt
    if obj.vy > 0 then obj.vy = 0 end
end
In here, .vx/.vy is the object's own velocity variables, and friction is the friction force (in this code, the force MUST be negative, else you would end up accelerating the object.) You can move that in a function to prevent unnecessary duplicates. Now, that's pretty much it. I could go on and explain collisions with circles, but this code is all you need to know (the double for-loop).

Code: Select all

for i=1, #objArray-1 do
    local partA = objArray[i]
    for j=i+1, #objArray do
        local partB = objArray[j]
        -- COLLISION CODE HERE
    end
end
The comment there means that you put your collision code of comparing two objects if they collide, and what to do when they collide. Whew, this was a long post.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Z axis gravity

Post by kikito »

I know it's possible to have something very similar to what you want. Maybe not exactly the same.

I've seen top-down car simulations done with box2d. I remember someone made one in LÖVE, but I could not find it. So here's a javascript + html5 version:

http://www.banditracer.eu/carexample/

I suspect however that they use "tricks" to simulate the friction between the car and the ground; there's no real "Z axis".
When I write def I mean function.
User avatar
Djent
Prole
Posts: 27
Joined: Sun Jan 20, 2013 3:22 pm
Location: Rhode Island

Re: Z axis gravity

Post by Djent »

Thanks for the suggestions everybody! My own physics engine is coming along quite nicely despite being from scratch. I plan on having the entire simulation engine done is a day or two, and then maybe adding some artificial intelligence.
User avatar
Djent
Prole
Posts: 27
Joined: Sun Jan 20, 2013 3:22 pm
Location: Rhode Island

Re: Z axis gravity

Post by Djent »

substitute541 wrote: I could go on and explain collisions with circles, but this code is all you need to know (the double for-loop).

Code: Select all

for i=1, #objArray-1 do
    local partA = objArray[i]
    for j=i+1, #objArray do
        local partB = objArray[j]
        -- COLLISION CODE HERE
    end
end
The comment there means that you put your collision code of comparing two objects if they collide, and what to do when they collide. Whew, this was a long post.
I've now found myself stuck on the collisions lol. Collisions between a pool ball and a wall are easy enough. Ball to ball collisions seem a bit harder. I know that they will be elastic collisions, but they also have a coefficient of restitution. Also, all the examples illustrating 2D elastic collisions show one of the objects at rest, and the other moving. In this simulation, I will of course need to figure out collisions between two moving objects. Is there a simple formula I can use for this?
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Z axis gravity

Post by Inny »

> pool table

I think a lot of what you want to do can be done easy* with a custom physics engine. It's just 16 spherical collisions with 6 fixed line segments and 6 hotspots for removing a sphere from the working set.

In terms of how to graphically display such a thing, you can make a couple of assumptions about the perspective of the player. If they're directly above the table, then the billiards get larger when they are trick-shot into the air. If the player is standing in front of the table looking down at at it, then the balls making some air above the table are just visually moved on the Y axis by the sum of their Y and Z values.


* for certain definitions of the word "easy"
User avatar
Djent
Prole
Posts: 27
Joined: Sun Jan 20, 2013 3:22 pm
Location: Rhode Island

Re: Z axis gravity

Post by Djent »

Inny wrote:> pool table

I think a lot of what you want to do can be done easy* with a custom physics engine. It's just 16 spherical collisions with 6 fixed line segments and 6 hotspots for removing a sphere from the working set.

In terms of how to graphically display such a thing, you can make a couple of assumptions about the perspective of the player. If they're directly above the table, then the billiards get larger when they are trick-shot into the air. If the player is standing in front of the table looking down at at it, then the balls making some air above the table are just visually moved on the Y axis by the sum of their Y and Z values.


* for certain definitions of the word "easy"
I'm not programming trick shots into the game. You seem to be misunderstanding the thread.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 57 guests