I've got a it all working, but there is one problem : The guy gets stuck in the yellow box, even when im sure that shouldn't happen, because i do it like :
1)calculate new posistion
2)check collision with new position
3)if no collision change actual position to new position
collision detection.. [SOLVED]
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: collision detection..
I'm no expert, but you are only updating the guy.position with newpos when collision returns false, this means that when a collision is happening the guy's position won't be updated.
So changing:
to
should do the trick. But this leads other problems if you wanted a more robust collision detection/response system...
So changing:
Code: Select all
if col() then guy.velocity = 0
else guy.position = newpos end
Code: Select all
guy.position = newpos
if col() then guy.velocity = 0 end
Re: collision detection..
that doesnt solve the problem at all.. you now can just walk into it
Re: collision detection..
Well, yes. You are only setting velocity.y to 0. So when your collision hits it from the sides it will just go through with velocity.y = 0. If you want it not to go through then you'll need to have velocity.x set to 0 when your collision happens mainly from left or right of the coin.
Something like this:
will work, but then you'll sort of get stuck when you hit the coin. What you need to do now is find a way to make your player be unstuck. This can be done by finding the minimum amount it has to move in order to not collide with the coin anymore...
Assuming you are only hitting from the bottom of the coin:
Now you're not getting stuck anymore, but only when you hit from the bottom. All you have to do is find a way to do this for the other sides too... And of course handle the resetting of velocities accordingly: when you hit from the sides you don't want velocity.y to be set to 0 and when you hit from bottom or top you don't want velocity.x to be set to 0.
This is the way I do it at least... But I'm sure there are better/simpler ways to achieve what you want.
Something like this:
Code: Select all
guy.position = newpos
if col() then
guy.velocity.x = 0
guy.velocity.y = 0
end
Assuming you are only hitting from the bottom of the coin:
Code: Select all
guy.position = newpos
if col() then--collision with box
guy.velocity.x = 0
guy.velocity.y = 0
guy.position.y = guy.position.y - 1
end
This is the way I do it at least... But I'm sure there are better/simpler ways to achieve what you want.
Last edited by adnzzzzZ on Tue Oct 25, 2011 2:46 pm, edited 1 time in total.
Re: collision detection..
it kinda works now, thanks .
but i want to keep doing it like this D: =
1)calculate new posistion
2)check collision with new position
3)if no collision change actual position to new position
but i want to keep doing it like this D: =
1)calculate new posistion
2)check collision with new position
3)if no collision change actual position to new position
Re: collision detection..
col() is checking the current position not the predicted position.
it should be
by the looks of it.
Just as a note, try and find a nice medium between brevity and clarity:
having two variables called "ap" and "as" may seem like less typing and so easier, but its much less clear than having a single variable containing all of the relevant variables for that object, eg just
Also, don't worry too much about optimisation while you are developing a project, as sometimes you can shoot yourself in the foot. (for example the speed increase from creating local variable inside a function vs the cost of having to create those local variables every frame)
it should be
Code: Select all
return newpos.x + as.x > bp.x and bp.x + bs.x > newpos.x and newpos.y + as.y > bp.y and bp.y + bs.y > newpos.y
Just as a note, try and find a nice medium between brevity and clarity:
having two variables called "ap" and "as" may seem like less typing and so easier, but its much less clear than having a single variable containing all of the relevant variables for that object, eg just
Code: Select all
guy = {
x = 0,
y = 0,
width = 20,
height = 40,}
Re: collision detection..
thanks kraftman
oh and anyone who wants it, its a coin box that ONLY gets activated when touched from the bottom :
oh and anyone who wants it, its a coin box that ONLY gets activated when touched from the bottom :
- Attachments
-
- jump.love
- (816 Bytes) Downloaded 84 times
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests