Hardon Collider Help

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.
Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

Hardon Collider Help

Post by Ethan-Taylor »

Hey guys,
I'm going to ask a really noobish question so bare with me.

So I have a function set up like this :

function onCollide(dt, a, b)

end

I need to correct the player position (movement.x and movement.y)
when the player touches a collider called "square".

player and square are both registered and have collision in them.
Anyone willing to help?
Muzz
Citizen
Posts: 54
Joined: Sun Jun 28, 2015 1:24 pm

Re: Hardon Collider Help

Post by Muzz »

Code: Select all

function onCollide(dt, a, b)

if a.ID = "player" then
     x,y = player:GetPos()
     player:SetPos(x+100,y+100)
end

end
I have no idea how you have your player setup, so this is just the sort of thing you would do. This will move the player somewhere when you touch something, though instead you'll probably just want to kill the players velocity.
Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

Re: Hardon Collider Help

Post by Ethan-Taylor »

Muzz wrote:

Code: Select all

function onCollide(dt, a, b)

if a.ID = "player" then
     x,y = player:GetPos()
     player:SetPos(x+100,y+100)
end

end
I have no idea how you have your player setup, so this is just the sort of thing you would do. This will move the player somewhere when you touch something, though instead you'll probably just want to kill the players velocity.
I can do that. But it needs the illusion of the player hitting the box and staying there when you try to move over the box.
I don't want to move the player away from the box.
User avatar
FruityLark
Prole
Posts: 28
Joined: Sat Jun 13, 2015 8:18 pm

Re: Hardon Collider Help

Post by FruityLark »

You are missing 2 useful parameters in your on_collide.

function on_collide(dt, shape_a, shape_b, dx, dy)

"dx and dy define the separating vector, i.e. the direction and magnitude <<<shape_one>>> has to be moved so that the collision will be resolved. [Note that if one of the shapes is a point shape, the translation vector will be invalid.]"

if shape_a.type == "player" then
player:move(dx, dy)
end
Muzz
Citizen
Posts: 54
Joined: Sun Jun 28, 2015 1:24 pm

Re: Hardon Collider Help

Post by Muzz »

Then just do

player.velx = 0
player.vely = 0
Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

Re: Hardon Collider Help

Post by Ethan-Taylor »

Ok, I have one more problem.
Some of the collision is a bit off in the game.
So the player has a bit of an offset when it collides with other things.

The collision scripts for the square are on line 19 in main.lua
and the ones for the player are line 95 in player.lua

if you run the game you'll see what I mean, controls are WASD and try colliding with the screen edges,
and the square in the middle.

btw the player gets locked onto the sqaure in the middle, don't worry I'll fix it later.
Attachments
game.love
(116.33 KiB) Downloaded 139 times
Muzz
Citizen
Posts: 54
Joined: Sun Jun 28, 2015 1:24 pm

Re: Hardon Collider Help

Post by Muzz »

I probably should have expanded on the original thing, the reason why you get stuck is that when we set the velocity to zero, which means that the state that fired the collision event is still happening.

You need to work out the position of the objects and move them back to where it was the frame before it fired.

Also, i would really recommend you learning about the local variable identifier, and to declare your object variables inside the table that holds the object. Declaring everything as a global is fine for a tiny game but it can't scale.
Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

Re: Hardon Collider Help

Post by Ethan-Taylor »

Muzz wrote:I probably should have expanded on the original thing, the reason why you get stuck is that when we set the velocity to zero, which means that the state that fired the collision event is still happening.

You need to work out the position of the objects and move them back to where it was the frame before it fired.

Also, i would really recommend you learning about the local variable identifier, and to declare your object variables inside the table that holds the object. Declaring everything as a global is fine for a tiny game but it can't scale.
There is another problem with the collision, please run the game.
Muzz
Citizen
Posts: 54
Joined: Sun Jun 28, 2015 1:24 pm

Re: Hardon Collider Help

Post by Muzz »

I did, the wall collision worked fine?
User avatar
FruityLark
Prole
Posts: 28
Joined: Sat Jun 13, 2015 8:18 pm

Re: Hardon Collider Help

Post by FruityLark »

In your custom border collision code, you are dividing the circle radius by 2 as if it were the diameter. There will be no shape intersection if you use 30 (half the width of your circle) instead of 30/2.

Also you should have player:draw('line') instead of player:draw('line',50) on main.lua:103
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests