Making a curved surface and detect collision correctly (Lunar Landar Game)

TrueBypass

Member
Licensed User
Longtime User
Hello everyone :)
I thought of making a Lunar Lander game in Basic4Android.
(In case you don't know the game: Play LunarLander Game )
I have a general idea of how the movement would work, but I cant figure out how to make a curved surface where I can detect collision correctly (As using ImageView or the like would not be affected by the image itself, the view itself would still be rectangular)

Any help?
 

thedesolatesoul

Expert
Licensed User
Longtime User
This is a very interesting topic...atleast for me :)
I wish there was some kind of collision detection library but ofcourse thats asking too much!
As Erel said, you can use the GetPixel value of a canvas, but that will not work if you have multiple objects of the same color. you can create 'layers' of moving objects but if you have too many objects then that will be too slow.
If you have an arbitary shaped object (lets say coming from a bitmap) it gets quite hard but if you can break it down into a geomtrical shape using the Path object, then you should be able to write a routine to do collision detection using AABB (axis-aligned bounding box collision).

Lets say if the ground surface is a circle with known radius and centre, and your lunar lander has a flat bottom, then checking each bottom edge pixel's (or you can approximate) distance from the edge of the circle. (its getting hard to explain without a diagram).

For the y bounds we check:

box.bottomedge > circle.y - r*sin((box.leftedge-circle.x)/r)
which comes from
box.bottomedge > circle.y - r sin(theta)

there is not much to check on x bounds i think since the angle takes care of that.

maybe i am complicating it too much though
 

Attachments

  • a.JPG
    a.JPG
    5.7 KB · Views: 237
Upvote 0

TrueBypass

Member
Licensed User
Longtime User
You can use a Path object to create complex figures.

One possible way to detect a collision is to check Canvas.Bitmap.GetPixel value and see if it is of a specific color.
Than you would have to do it with every possible pixel (if i do not mistake), and that would be pretty slow.
This is a very interesting topic...atleast for me :)
I wish there was some kind of collision detection library but ofcourse thats asking too much!
As Erel said, you can use the GetPixel value of a canvas, but that will not work if you have multiple objects of the same color. you can create 'layers' of moving objects but if you have too many objects then that will be too slow.
If you have an arbitary shaped object (lets say coming from a bitmap) it gets quite hard but if you can break it down into a geomtrical shape using the Path object, then you should be able to write a routine to do collision detection using AABB (axis-aligned bounding box collision).

Lets say if the ground surface is a circle with known radius and centre, and your lunar lander has a flat bottom, then checking each bottom edge pixel's (or you can approximate) distance from the edge of the circle. (its getting hard to explain without a diagram).

For the y bounds we check:

box.bottomedge > circle.y - r*sin((box.leftedge-circle.x)/r)
which comes from
box.bottomedge > circle.y - r sin(theta)

there is not much to check on x bounds i think since the angle takes care of that.

maybe i am complicating it too much though
(I haven't learned about sines yet - I just graduated from middle school. XD)

If the lunar lander had a flat bottom and I would measure the distance from the lander's edges in order to see if it is in the circle, this following situation would be problematic:
21eyujl.png

But if the bottom was shaped like this:
sls5s1.png

It would have been solved. So thanks for your help.

However, this is a solution for this problem only, so in a different variation - it might not be that easy, I would rather looking for a different solution.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I am not aware of your requirements (to shapes and sizes), so you will have to decide in the end.
If you are looking for a general solution just google 'axis aligned bounding box' or even just 'collision detection'. All of them will involve maths. It is all based on coordinate systems, trigonometry, vectors, projections and surfaces.

If you want a cooked-up solution ask someone to port a Physics engine like Box2D or chipmunk to B4A in the Wishlist section of the forum. They are fast and almost all iphone/android based games are based on them.

Ofcourse, Erel gave you the easiest solution. And it may not be as slow as you think. You just need to check the bottom edge, rather than the whole rectangle. How much can that be? 480 pixels? And if you are smart, then you will figure out which pixels have the highest chance of getting intersected first.
 
Upvote 0

kanaida

Active Member
Licensed User
Longtime User
I would make a few squares, roughly approximating a circle so it's not so cpu intensive. If they X/Y coordinates of an object land inside any box, then it becomes a hit. You could probably define about a good 5 or so small boxes to do that. It doesn't have to be exact, but the more boxes, the more accurate. It's much less intensive than checking hundreds of pixels.

If memory serves me right, you need to do this stuff *before* you draw it.
All of your objects should have basic shapes or little boxes representing game objects in the background, then things are drawn after in a Draw() sub usually. Although it would be very precise to do it with pixels, i'm not convinced it will be very efficient when lots of stuff is going on. I could be wrong, but I know it's less work the other way.

If you think it's the easiest way, see if you can perform matrix operations to do those kinds of checks. They're probably more optimized, and hopefully calculate things in parallel like a GPU does.
 
Last edited:
Upvote 0

TrueBypass

Member
Licensed User
Longtime User
Ofcourse, Erel gave you the easiest solution. And it may not be as slow as you think. You just need to check the bottom edge, rather than the whole rectangle. How much can that be? 480 pixels? And if you are smart, then you will figure out which pixels have the highest chance of getting intersected first.

I think i'd start working with that solution and see it's responsiveness.

Thanks for all the help, I hope to post the results soon.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
I think you need a combination iirc...

1) Is the object near another?

2) If it is, check pixel colour of static item, has it changed? If so, collision.
 
Upvote 0
Top