Welcome, Guest.
Please login or register.
Learning Lua - Part 2
Forum Login
Login Name: Create a new account
Password:     Forgot password

Darkshade Forum    Blogs    Diddly  ›  Learning Lua - Part 2 Moderators: Bloggers
Users Browsing Forum
No Members and 1 Guests

Learning Lua - Part 2  This thread currently has 1 views. Print Print Thread
1 Pages 1 Recommend Thread
Diddly
August 25, 2010, 5:59pm Report to Moderator Report to Moderator

Noble
Posts: 1,231
Corona implements a Box2D physics engine. Using it, you can make some very impressive interactions. Just take a look at their sample code for Simple Pool and you'll see how easy it is to have objects moving, spinning, and colliding.

For Mobile Zombies I was mainly interested in the Collision detection. I was hoping to throw in some brick walls, and have the zombies alter their course when they encountered the obstacle. This technique worked well for Zombie Slaughter 3 (which never progressed beyond a tech demo), but required the ability to "look ahead". I would calculate the next position, see if it would collide with something, then alter accordingly. This gave the effect of zombies rushing past barricades like streams of water.

Unfortunately once we inject a proper physics engine into the mix, the Zombies get a little crazy. They start bouncing off things and spinning. Neither of which is an effect I desire.

Here's what I've learned so far about physics in Corona:

  • Always start the physics before setting gravity. Since Zombies is a top down view, I wanted to set gravity to 0, but Corona would abort as soon as it hit that command. Gravity may be a property, but physics needs to be running before it can be updated.
  • dynamic, kinematic, and static collision restrictions. A dynamic body type can collide with another dynamic body, or even a static, but no collision is registered when it encounters a kinematic object. Similarly, kinematic does not collide with static objects.
  • You can use getLinearVelocity() for self-propelled objects, to see if any additional force is required to keep moving. I don't know this is the best way to do things though, since I'm not quite happy with the motion of my Zombies yet.
  • zombie:applyForce( xspeed, yspeed, zombie.x, zombie.y ) will move the zombie straight. If you use different x and y coordinates for the center of your force, is gets calculated as spin. Boy did those zombies twirl when I tried the center of the screen!
  • A handy function for calculating your vectors is something that returns either 1 or -1 depending on the sign of the input.
    Quoted Text
    function sign(x)
    return (x<0 and -1) or 1
    end

    Here's an example usage, to throttle the maximum force applied horizontally.
    Quoted Text
    local xspeed = zombie.destx - zombie.x
    xspeed = sign(xspeed) * math.min( zombie.maxSpeed, math.abs( xspeed ) )





Attachment: 17_pm_9602.png
Size: 388.47 KB



Currently Reading:Next in Queue:
When Heavens CollideRed Mars - Kim Stanley Robinson
Logged Offline
E-mail E-mail Private Message Private message
1 Pages 1 Recommend Thread
Print Print Thread

Darkshade Forum    Blogs    Diddly  ›  Learning Lua - Part 2