physics based collision detection not working?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- gladius2metal
- Prole
- Posts: 26
- Joined: Thu Nov 22, 2012 2:08 am
physics based collision detection not working?
Hi,
my physics objects (all "dynamic" bodies+shapes+fixtures) don't collide according to the visuals and world:getContactCount(). Although applyForce() works fine. Since the tutorial works fine and I checked both codes several times I guess it is a either a stupid error or something non-obvious for noobs
just did a few more tests, actually now very rarely a collision occurs, which is just more confusing...
I thought it was a "representation problem" (objects displayed at wrong locations) at first, but I tried to draw them only with the physics data (see code in SpaceObject:draw()) and the collisions didn't happen either.
.love attached
thx in advance
gladius
my physics objects (all "dynamic" bodies+shapes+fixtures) don't collide according to the visuals and world:getContactCount(). Although applyForce() works fine. Since the tutorial works fine and I checked both codes several times I guess it is a either a stupid error or something non-obvious for noobs
just did a few more tests, actually now very rarely a collision occurs, which is just more confusing...
I thought it was a "representation problem" (objects displayed at wrong locations) at first, but I tried to draw them only with the physics data (see code in SpaceObject:draw()) and the collisions didn't happen either.
.love attached
thx in advance
gladius
- Attachments
-
- second.love
- (4.82 KiB) Downloaded 219 times
Re: physics based collision detection not working?
Hey, I'm not a pro but I dedicated quite some time to solving your problem.
First of all, you are doing a hundred things wrong . You should NOT use applyForce with a dt inside it. You only pass it to world:update.
Also, you DON'T want to use a variable dt in world:update() as it may cause loss of speed of objects in your game.
Next, make sure you set a proper Meter size. You had 64, which made your 20px space ship a micro machine. I set it to 10, so now your spaceship has some 2 meters in size .
Now, about the error. You made a mistake in the line when you create a shape. A shape is always attached to the center of the body, so you should write 0,0 instead of x,y.
Moreover, you should not rotate your object like you do, you should use applyTorque.
I corrected all these things for you (except the world:update(dt), which is tricky), some heavy code manipulation, hope you gonna recognize it . Hope this helps.
First of all, you are doing a hundred things wrong . You should NOT use applyForce with a dt inside it. You only pass it to world:update.
Also, you DON'T want to use a variable dt in world:update() as it may cause loss of speed of objects in your game.
Next, make sure you set a proper Meter size. You had 64, which made your 20px space ship a micro machine. I set it to 10, so now your spaceship has some 2 meters in size .
Now, about the error. You made a mistake in the line when you create a shape. A shape is always attached to the center of the body, so you should write 0,0 instead of x,y.
Moreover, you should not rotate your object like you do, you should use applyTorque.
I corrected all these things for you (except the world:update(dt), which is tricky), some heavy code manipulation, hope you gonna recognize it . Hope this helps.
- Attachments
-
- second2.love
- Second2
- (6.74 KiB) Downloaded 228 times
- gladius2metal
- Prole
- Posts: 26
- Joined: Thu Nov 22, 2012 2:08 am
Re: physics based collision detection not working?
wow, thx a lot!!!
setmeter was originally at 2, but prior to sending the attachment I changed it.
gonna try out the new code tomorrow!
setmeter was originally at 2, but prior to sending the attachment I changed it.
gonna try out the new code tomorrow!
- gladius2metal
- Prole
- Posts: 26
- Joined: Thu Nov 22, 2012 2:08 am
Re: physics based collision detection not working?
first, thx a lot the code works great.
I have a few questions:
1) why "alpha" and is there no built-in value in body/shape?
2)
I am not quite sure if I am off the tracks again or?
3)
your code:
my old code:
I have a few questions:
1) why "alpha" and is there no built-in value in body/shape?
Code: Select all
spaceObject.alpha = 1 -- angular acceleration
do you mean using dt within love.update()? world:update() needs an argument, also world:update(dt) is taken straight from the tutorial.Also, you DON'T want to use a variable dt in world:update() as it may cause loss of speed of objects in your game.
I am not quite sure if I am off the tracks again or?
3)
I can't find that in the code!?Now, about the error. You made a mistake in the line when you create a shape. A shape is always attached to the center of the body, so you should write 0,0 instead of x,y.
your code:
Code: Select all
spaceObject.body = love.physics.newBody(world, x, y, "dynamic")
spaceObject.shape = love.physics.newRectangleShape(spaceObject.image:getWidth(), spaceObject.image:getHeight())
spaceObject.fixture = love.physics.newFixture(spaceObject.body, spaceObject.shape, spaceObject.density) -- A higher density gives it more mass.
Code: Select all
spaceObject.physics.body = love.physics.newBody(world, x, y, "dynamic")
spaceObject.physics.shape = love.physics.newRectangleShape(x, y, spaceObject.image:getWidth(), spaceObject.image:getHeight(), spaceObject.angle)
spaceObject.physics.fixture = love.physics.newFixture(spaceObject.physics.body, spaceObject.physics.shape, spaceObject.density) -- A higher density gives it more mass.
Re: physics based collision detection not working?
http://en.wikipedia.org/wiki/Angular_accelerationgladius2metal wrote:first, thx a lot the code works great.
I have a few questions:
1) why "alpha" and is there no built-in value in body/shape?Code: Select all
spaceObject.alpha = 1 -- angular acceleration
This property is just the desired acceleration, the rate of torque that you want to exert on the body. I put it in object constructor, but it might as well be outside, near the key controls.
linear and angular physics, a simple comparison:
Code: Select all
force = mass * acceleration
[newton] = [kg] * [m/s^2]
torque = inertia * angular_acc
[newton meter] = [kg*m^2] * [1/s^2]
when you write world:update(dt), some bad things may happen. For example, try accelerating your space ship and then grab the window with the mouse for 1 second, then release. You will see the game freeze and then the space ship losing its speed.2) do you mean using dt within love.update()? world:update() needs an argument, also world:update(dt) is taken straight from the tutorial.
I am not quite sure if I am off the tracks again or?
This happens because when you grab the window, the game is not updated. When you release it, the dt will equal 1 second instead of 1/60. The world:update() is designed for constant dt value. If it varies too much, the simulation will break down. Now, I haven't found an ideal fix for this, you may find some on the forum. For now I suggest doing this:
Code: Select all
world:update(math.min(dt, 1/60))
My code:3) I can't find that in the code!?
Code: Select all
love.physics.newRectangleShape(WIDTH, HEIGHT)
Code: Select all
love.physics.newRectangleShape(x, y, WIDTH, HEIGHT, ANGLE)
Hope it's clear.
- gladius2metal
- Prole
- Posts: 26
- Joined: Thu Nov 22, 2012 2:08 am
- gladius2metal
- Prole
- Posts: 26
- Joined: Thu Nov 22, 2012 2:08 am
Re: physics based collision detection not working?
one final(?) question, I just recognized that my ship actually seems to be limited to a maximum velocity, which I thought I needed to add "manually".
Any hints on how I can calculate the maximum velocity / "terminal velocity" of an object?
Any hints on how I can calculate the maximum velocity / "terminal velocity" of an object?
Re: physics based collision detection not working?
The slowing down of objects (both linear and angular) is currently achieved with:gladius2metal wrote:one final(?) question, I just recognized that my ship actually seems to be limited to a maximum velocity, which I thought I needed to add "manually".
Any hints on how I can calculate the maximum velocity / "terminal velocity" of an object?
Code: Select all
spaceObject.body:setLinearDamping(0.5)
spaceObject.body:setAngularDamping(3.0)
However, you would have to think what kind of physics reality are you trying to create here. If this is space, then there is no atmosphere, so nothing to hold your space ship back. The space ship would accelerate as long as you deliver fuel to the jet engine.
If you want to introduce atmosphere, then air resistance would come into play. The basic equation for air resistance (drag) is:
Code: Select all
drag = - object_velocity ^ 2 * drag_coefficient
How to find maximum velocity? At maximum speed the forces are equal:
Code: Select all
engine_force = v * v * drag_coefficient
v = sqrt(engine_force / drag_coefficient)
example: sqrt(1800 newtons / 2 kg/m) = 30 m/s
- gladius2metal
- Prole
- Posts: 26
- Joined: Thu Nov 22, 2012 2:08 am
Re: physics based collision detection not working?
thx, I tried that formular but it is about 3 times off, which is way better than before because it was about 100 times off
formular in pseudocode + code. I assume probably lineardamping and/or meter_pixel_ratio need to be changed/remove:
or maybe my calculation of current velocity is incorrect?
another possibility would be that I am not patient enough to wait til the speed reaches the maximum, but right now it accelerates up to 199 and then it stays at this value.
formular in pseudocode + code. I assume probably lineardamping and/or meter_pixel_ratio need to be changed/remove:
Code: Select all
force = acceleration * meter_pixel_ratio * mass / lineardamping
v = sqrt (force / lineardaming)
local max_v = math.sqrt( spaceObject.acceleration * METER_PIXEL_RATIO * spaceObject.body:getMass() / spaceObject.body:getLinearDamping())
Code: Select all
I use: v_current = v_linear_x * v_linear_x + v_linear_y * v_linear_y
local vx,vy = playerShip.body:getLinearVelocity( )
love.graphics.print("speed :"..math.floor(math.sqrt(vx^2 + vy^2)), 10, 40)
Re: physics based collision detection not working?
Remember that getVelocity returns pixels per second, not meters per second. I could not grasp the code you pasted, so I'm going to paste mine:gladius2metal wrote:another possibility would be that I am not patient enough to wait til the speed reaches the maximum, but right now it accelerates up to 199 and then it stays at this value.
(i'm writing from memory, there may be errors)
Code: Select all
function love.load()
m = 10
engine_force = 1600 --N
drag_coeff = 2 --kg/m
ship = newShip()
end
function love.update(dt)
world.update(math.min(dt, 1/60))
if love.keyboard.isDown("up") then ship.body:applyForce(ship.body:getWorldVector(engine_force * m, 0)) end
local vx, vy = ship.body:getLinearVelocity()
local dx = - vx * math.abs(vx) * drag_coeff
local dy = - vy * math.abs(vy) * drag_coeff
ship.body:applyForce(dx, dy) -- air resistance
end
--ship.speed = sqrt(vx*vx + vy*vy) / m
Who is online
Users browsing this forum: beef, Google [Bot] and 5 guests