I would like to make a game using physics. Collisions between objects are clear to me.
However, I don’t know how I could simulate different ground material.
Suppose I push an object and it moves from the bottom to the top of the screen.
For the object, I can set parameters like mass, friction, etc.
But how to set the material and friction for the surface (i.e. the background) on which the object is moving ? For example, an object will move differently on ice, differently on grass and differently on concrete.
local player = {
x = 400,
y = 400,
dx = 0,
dy = 0,
}
local friction = 0.02
local speed = 40
function love.update(dt)
player.x = player.x + player.dx * speed * dt
player.y = player.y + player.dy * speed * dt
player.dx = player.dx * (1 - friction)
player.dy = player.dy * (1 - friction)
local kx, ky = 0, 0
if love.keyboard.isDown('a') then kx = kx - 1 end
if love.keyboard.isDown('d') then kx = kx + 1 end
if love.keyboard.isDown('w') then ky = ky - 1 end
if love.keyboard.isDown('s') then ky = ky + 1 end
player.dx = player.dx + kx
player.dy = player.dy + ky
player.x = math.max(math.min(player.x, 1000), 0)
player.y = math.max(math.min(player.y, 1000), 0)
end
function love.draw()
love.graphics.rectangle('fill', player.x - 1, player.y - 1, 3, 3)
end
You should use the apply_force message and push in opposite direction of travel. You can use the linear_velocity property, normalize it and reverse it to get the opposite direction.
Yes, this is clear to me. If we have 2 elements as in the picture and treat the bottom element as our “floor” on which the other element moves, then the “type of floor” depends on what parameters we set for that object.
In my case, we have a top view, meaning the background is my floor.
How to set these parameters for the “background” ?
Another thing. If we let the puck go on the ice at a slight angle and give it some angular velocity, the puck will move in an arc (just like in curling). How to get such a trajectory of the object moving ?
If you have different kinds of backgrounds in the same level then use trigger collision objects to detect which background you’re on and apply the appropriate friction.
I have no idea how to simulate this. I think it is beyond the scope of what a physics engine will allow you to accomplish.
@britzl One more question. Can I dynamically change the value of the friction parameter ? I looked in the documentation and did not find such a possibility
While it might be difficult to realistically and accurately simulate this for all kinds of different objects, it would be quite easy to emulate it by applying a sideways force to the puck based on its angular velocity. Take the linear velocity of the puck, normalize it and get its perpendicular vector, multiply that by the angular velocity and a scale factor, and apply that force to the puck.
If you want to do more, you could try doing some internet research on how the physics of curling works.