0

like many others before, I'm coding a 2D platformer at the moment, or more precise on an Engine for that. My question is about collision detection and especially the reactions to collision.

It's vital to me to have surfaces that are NOT tile based, because the world of the platformer has urgend need of surfaces that are not straight.

What do you think is the best approach for implementing such?

I've already come to a solution, but I'm only about 95% satisfied with it.

My Entity is component based so at first it calculates all the movement. Horizontal position change. Falling. Jumping speed; all that. Then it stores these values temporarily; accessible to the Collision component.

This one adds 4 hitpoints to the entity. Two for the floor collision and two for the wall collision.

Like this

image
(source: fux-media.com)

I first check against the floor. When there IS collision, I iterate collision detections upwards until there is no more collision.

The I reset the Entity to this point. If both ground hitpoints collide, I reset the Entity to the lowest point to avoid the Entity floating in the air.

Then I check against the wall with the upper hitpoints. Basically, if it collides, I do the same thing as above, just horizontally.

If works quite well. Very heavy ascensions are treated like a wall and very low ascensions are just climbed. But in between, when the ascensions are around 45 degree, it behaves strange. It does not quite feel right.

Now, I could just avoid implementing 45 degree walls in my game, but that would just not be clean, as I'm programming an engine that is supposed to work right no matter what.

So... how would you implement this, algorithmically?

Community
  • 1
  • 1
Ephiarsis
  • 1
  • 1
  • I've had similar problems with angled walls. After a few attempts at hacky work-arounds and the dawning realisation that a good physics engine requires an iterative solver, I gave up and replaced my physics code with [Chipmunk](http://code.google.com/p/chipmunk-physics/). – Marcelo Cantos Sep 13 '11 at 22:25
  • Does “vector based” mean that all your surfaces are defined by straight/curved lines? – hugo Aug 12 '19 at 21:02

1 Answers1

1

I think you're on the right track with the points. You could combine those points to form a polygon, and then use the resulting polygon to perform collision detection.

Phys2D is a library that happens to be in Java that does collision between several types of geometric primitives. The site includes downloadable/runnable demos on what the library is capable of.

If you look around, you can find 2D polygon collision detection implementations in most common languages. With some study, an understanding of the underlying geometry calculations, and some gusto, you could even write your own if you like.

jefflunt
  • 33,527
  • 7
  • 88
  • 126