Efficient collision detection

Discussion about using Moai SDK - post questions, issues, and ideas here.

Moderators: naturally, seebs

Efficient collision detection

Postby nilstastic » Thu Mar 07, 2013 2:21 pm

I am looking for the best way to animate sprites that bounce off the walls and off each other without using a physics lib.

Right now i am animating the props in my main loop with
Code: Select all
  1. for loop

  2. proplist[i]:moveLoc(dirlist[i].dx,dirlist[i].dy,0)



and checking collisions with the values i extract from proplist[i]:getLoc(). When a collision is detected i switch the dx,dy values of the properties in the dirlist table.

Code: Select all
  1. if circleColliding(ax,ay,bx,by) then

  2. dirlist[i],dirlist[ii] = dirlist[ii],dirlist[i]

  3. end



This seems extremly inefficient and i am only able to squeeze out 20 bouncing props at 60fps, but i feel a bit confused about how to go about this in any other way.
nilstastic
 
Posts: 7
Joined: Wed Dec 12, 2012 6:31 am

Re: Efficient collision detection

Postby tommo.zhou » Thu Mar 07, 2013 3:01 pm

Lua is not suitable for such math-heavy task (especially on mobile device), just use box2d or chipmunk.
tommo.zhou
 
Posts: 109
Joined: Thu Sep 29, 2011 12:28 am

Re: Efficient collision detection

Postby nilstastic » Thu Mar 07, 2013 3:59 pm

Agreed, but i feel that my approach on animating the prop with dx,dy is flawed. Animating with moveLoc(0,64,3.0) seems much more efficient but i do not understand how i could use it to create continuos motion in a certain direction.

Ah well. =)
nilstastic
 
Posts: 7
Joined: Wed Dec 12, 2012 6:31 am

Re: Efficient collision detection

Postby ibisum » Thu Mar 07, 2013 8:08 pm

In my opinion, if you're using MoveLoc and trying to do the math yourself, you're not using the physics library. What you should be doing is simply initializing the properties of the characters in your game with the proper physics constraints - dynamics, type, friction, density, etc. and then just letting the physics simulation run its course, updating the characters of your game according to such definitions.

With regards to using box2d, you then have the bonus of being able to use its collision-detection system and get it such that you really can do:

Code: Select all
  1.  

  2. -- handle collisions between items

  3. function handle_character_collision(event, fixtureA, fixtureB, arbiter)

  4.         -- two items have touched each other

  5.         if event == MOAIBox2DArbiter.BEGIN then

  6.                 if fixtureA.userdata and fixtureB.userdata then

  7.                         if fixtureA.name and fixtureB.name then

  8.                                 print(" fixtureA " .. fixtureA.name .. " fixtureB " .. fixtureB.name)

  9.                         end

  10.                 print( fixtureA.userdata.." collided with " .. fixtureB.userdata)

  11. -- &etc.

  12.  



with the setup code, of course being done on Fixture creation with masks:

Code: Select all
  1.  

  2. -- init of game characters ..

  3. character_bodies[1]:resetMassData()

  4. character_fixtures[1]:setCollisionHandler(handle_character_collision, MOAIBox2DArbiter.BEGIN + MOAIBox2DArbiter.END, PLAYER_CATEGORY)

  5. character_bodies[2]:resetMassData()

  6. character_fixtures[2]:setCollisionHandler(handle_character_collision, MOAIBox2DArbiter.BEGIN + MOAIBox2DArbiter.END, WALL_CATEGORY)

  7. character_bodies[3]:resetMassData()

  8. character_fixtures[3]:setCollisionHandler(handle_character_collision, MOAIBox2DArbiter.BEGIN + MOAIBox2DArbiter.END, BADDY_CATEGORY)

  9.  



.. and so on (see docs for setCollisionHandler masks.. its a simple mask op) ..
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info/
User avatar
ibisum
 
Posts: 1288
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Efficient collision detection

Postby nilstastic » Fri Mar 08, 2013 8:13 am

In my opinion, if you're using MoveLoc and trying to do the math yourself, you're not using the physics library.

Yes, that was the idea. Using box2d for simple bounces against a wall seems like it's a bit of an overkill. =)

The problem with using box2d is that it's a chore to make platformer physics that behave the same way as the classic 8/16 bit games do. I've used box2d before and achieved ok results by (i think) setting linearvelocity directly, ignoring friction etcetera but since i rolled my own physics in another game (a goat game, works fine on a laptop, works ok on mobile but lacks speed) i though that it should be possible to do this semi-efficiently in Lua.

I think the next step will be to simulate the environment and detect collisions without using the props, and when a collision is detected switch the directions of the props that intersect, using moveLoc and traveling dx*60 pixels in 1 second instead of moving in smaller steps (ie. 1 pixel on every frame). If it's not fast enough i'll try to move some stuff to c++ an see if that improves thing. =)
nilstastic
 
Posts: 7
Joined: Wed Dec 12, 2012 6:31 am

Re: Efficient collision detection

Postby ibisum » Fri Mar 08, 2013 12:21 pm

Perhaps you've seen this snippet?

http://www.moaisnippets.info/platformer ... 2d-physics

I think its a more than reasonable implementation, to be honest .. ;)
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info/
User avatar
ibisum
 
Posts: 1288
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Efficient collision detection

Postby nilstastic » Fri Mar 08, 2013 6:02 pm

Looks excellent, i'll check it out! =)
nilstastic
 
Posts: 7
Joined: Wed Dec 12, 2012 6:31 am


Return to Moai SDK

Who is online

Users browsing this forum: No registered users and 1 guest

x