Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Need help with box/line collision response (2d)

Author
Message
Ranietz
AGK Gold Backer
20
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 28th Sep 2011 18:05
I'm trying to figure out how to reposition a box when it collides with a line segment (in 2d). I got the collision detection working but I don't know what to do next.

Does anybody have a suggestion on how to reposition the box when there is a collision?

Here's what I got so far (needs advanced2d and matrix1util)
Da_Rhyno
14
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 29th Sep 2011 03:41
Just set it back to the previous X and Y it was at before collision.
Ranietz
AGK Gold Backer
20
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 29th Sep 2011 04:02
That's not exactly what I want. Maybe "sliding collision" is a better description of what I'm after.
Diggsey
19
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 29th Sep 2011 19:07
If it is a horizontal line, only reset the Y component to the last value. If it is a vertical line, only reset the X component to the last value.

[b]
Ranietz
AGK Gold Backer
20
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 29th Sep 2011 19:57
No, the line is not horizontal or vertical.

I'll be using this function for a platformer game where the ground is not horizontal. The box will be the player and the line will be the ground.

I know I could use the box2d plugin (which I have bought), but I couldn't figure out how, since I want gravity (for jumping/falling) in the game but I don't want the player to slide down slopes.
29 games
19
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 30th Sep 2011 00:19
Hi Ranietz, I think I get what you want.

First off, the collision code you came up with doesn't really do what you want it to do. It's really just a box on box collision (i.e. the line is being dealt with like a box).

What you need to do is use the equation of line, which is:

(y-y1)/(y2-y1) = (x-x1)/(x2-x1)

Rearrange this to get:

y = (x-x1)*(y2-y1)/(x2-x1) + y1


x1,y1 and x2,y2 are two points that describe the line (in this case the end points).

x and y are the coordinates of a third point on the the line (in this case x is the x coordinate of the player and y is where the ground is beneath the player).


The demo below gives a quick example of what this all means. Use the arrow keys to move left and right and space bar to jump.




For a better explanation of equations for a line see:

http://en.wikipedia.org/wiki/Linear_equation#Point.E2.80.93slope_form

Hope this helps.
Ranietz
AGK Gold Backer
20
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 30th Sep 2011 01:48 Edited at: 30th Sep 2011 01:49
Thanks 29 games.

I'm not sure how the code works but I'll guess I'll figure it out after a while. The thing is: it uses only one x point, which could work for circles but I'm not sure how well it will work with a box...

My current collision code do actually work but it's only for collision detection, not collision response. It treats the line as a box at first for a quick collision test and if there is a collision I check to see if the four corner points of the box are on the same side of the line. If they are not, then the box and line has collided.
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 30th Sep 2011 02:48 Edited at: 30th Sep 2011 03:41
^Well, circles are easier to handle.
http://forum.thegamecreators.com/?m=forum_view&t=179370&b=6

if you really really want box->line collision system, you might as well handle polygon->line collision, and thats not all that simple. My advice: use circle collision, or learn to use box2d!

29 games
19
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 30th Sep 2011 14:36 Edited at: 30th Sep 2011 14:40
@ Ranietz, I had to rem out the lines of code that started "p1 = sgn..." as for some reason they didn't work. I've not got any plugins for DBP and probably don't have the most up-to-date version so this might be the problem.

I'm not sure that circles are easier than boxes in all situations.

Boxes are easier providing they're parallel with the x and y axis. If the box isn't parallel with the x and y axis then, yes, I would say circles take less effort but I wouldn't say the maths and code logic is actually easier.

In Ranietz's case, if the player box is parallel to the x and y axis then all that needs to be done is check where the ground is beneath the left and right bottom corner of the box, essentially doing to calculations and checks rather than one. I've put together some diagrams in the attached pdf to explain how I would go about doing this.

It also depends on what you can tolerate in the game. A bit of sprite overlap may not actually matter that much. Chances are if the game play is fast and furious, with lots of bullets and enemies whizzing around the place, no one's going to notice if the player isn't quite on the line all the time.

Attachments

Login to view attachments
Ranietz
AGK Gold Backer
20
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 30th Sep 2011 16:23
@Neuro Fuzzy: I know circle->line collision is easier (I already figured out how to do that ), but I want to use a box in this case so I don't need to have different collision shapes for different situations (collision with enemies and so on).
And I should learn to use box2d, but it was just frustrating using it in a platform game (it's probably me who don't know how to use it).

@29 games: Thanks again. The sgn command is from the matrix1 plugin. It returns -1 if a number is less than 0 and 1 if a number is greater than 0.
And thanks for the pdf example . I started doing something similar last night and almost got it working. I'll be using tile based collision for most cases and using lines for slopes only. I'll post an example later today if I get it working.
Ranietz
AGK Gold Backer
20
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 30th Sep 2011 19:47 Edited at: 30th Sep 2011 19:53
I finally got it to work! Thanks for helping me out everyone (especially 29 games )

Here's a simple working example. I made it so there's no need for any plugins (at least I think so)
Edit: arrow keys to move. Space to jump.

29 games
19
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 2nd Oct 2011 23:00
That works really well, glad you got it sorted.

Login to post a reply

Server time is: 2025-06-08 04:27:09
Your offset time is: 2025-06-08 04:27:09