Using physics

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
Zeggy
Prole
Posts: 13
Joined: Sun Nov 30, 2008 4:38 pm

Using physics

Post by Zeggy »

Hello, well, I've got an idea for a game so I'm going to try to make it :)

I have come across a problem though, relating to the physics.

You can say the game is sort of 'top-down' view, so I don't need gravity.
There are two 'circles' controlled by players.

I want each dot to have a continuous forward velocity, that velocity doesn't change. However, the player can change the angle of the velocity by pressing left/right arrow keys.

How can I code this angle change?


I've tried experimenting, and setAngle doesn't change anything. setSpin affects the angle after a collision, but nothing affecting the direction of velocity itself.

I have a feeling it might have to do with 'impulse', force or 'torque'. I don't know what they are :)
User avatar
Kaze
Party member
Posts: 189
Joined: Sat Jul 19, 2008 4:39 pm
Location: Dublin, Ireland

Re: Using physics

Post by Kaze »

Code: Select all

angle = 0
turn_speed = 0.05
speed = 1

function update( dt )
    velx = math.sin(angle) * speed
    vely = math.cos(angle) * speed
    player:applyImpulse(velx, vely)
end

function keypressed( key )
    if key == love.key_right then
        angle = angle + turn_speed
    elseif key == love.key_left then
        angle = angle - turn_speed
    end
end

Like that.
Zeggy
Prole
Posts: 13
Joined: Sun Nov 30, 2008 4:38 pm

Re: Using physics

Post by Zeggy »

Thanks Kaze, that works great!

However, I have a problem:
When you press left/right, the object doesn't respond immediately, instead it takes a while for it to turn around because it still needs to overcome the force in its current direction.

How can I make it so that it will immediately turn without needing to slow down first?
User avatar
Kaze
Party member
Posts: 189
Joined: Sat Jul 19, 2008 4:39 pm
Location: Dublin, Ireland

Re: Using physics

Post by Kaze »

Zeggy wrote:Thanks Kaze, that works great!

However, I have a problem:
When you press left/right, the object doesn't respond immediately, instead it takes a while for it to turn around because it still needs to overcome the force in its current direction.

How can I make it so that it will immediately turn without needing to slow down first?
Use setVelocity instead of applyImpulse
Zeggy
Prole
Posts: 13
Joined: Sun Nov 30, 2008 4:38 pm

Re: Using physics

Post by Zeggy »

Hehe, I used that, except strange things happen when there's a collision between to objects. Ie. they don't collide (properly).

What I'd like is to have the same acceleration like when impulse is used, except when turning, no velocity is lost.
Other than that, the objects can still slow down when colliding into something, maybe even move backwards from the force. But eventually the constant acceleration will make them continue moving again.
Post Reply

Who is online

Users browsing this forum: Google [Bot], slime and 175 guests