How to make a flying enemy with gravity on?

How do you make an enemy fly or float along horizontally (for a side scroller), with gravity turned on?

Basically, I need to have gravity for this game, and also need the enemy to be “dynamic”. I know that setting the enemy to kinematic, it will avoid gravity thus I can use “setLinearVelocity” to move it. BUT, if I make my enemy “dynamic”, even setting it’s “setLinearVelocity” will NOT make it fly or float. My first solution was to create a runtime event listener and run a function to keep the enemy / object up in the air. But this solution is giving me issues (specifically memory leak issues), especially when I put it within a for loop for creating multiple enemies.

My question is, is there another or more efficient way to make “dynamic” objects “fly” or “float” horizontally (on the x-axis)? I hoped there would be a way to disable gravity for just an object, but I’ve already tried and also read a few threads that say, nope…

Here’s psuedo code for what I tried to do (which doesn’t seem to be the most efficient way IMO):

  1. set GRAVITY to (0,10)
  2. set “setLinearVelocity” of the object / enemy to (0,-10) - this will make your object go to the left
  3. put the “setLinearVelocity” into a function, that is triggered by a runtime event listener

Any suggestions, links to other threads, or help?

THANKS [import]uid: 129334 topic_id: 24222 reply_id: 324222[/import]

Is the enemy/object constantly moving? As in, does it appear at one side of the screen and fly across and out the other side?

If so, a transition may be the easiest solution here. [import]uid: 52491 topic_id: 24222 reply_id: 97875[/import]

Hello Peach,

Yes, the enemy/object is constantly moving from the far right side of the screen, to the left side (flies across). I can do a transition, but that still doesn’t solve the ‘gravity’ issue, as in the objects fall down…

I could make the enemies ‘kinematic’, but I need them to be ‘dynamic’…

Let me keep experimenting, maybe ‘kinematic’ is the way to go, and I just try to adjust my game to it… [import]uid: 129334 topic_id: 24222 reply_id: 97886[/import]

Sorry, I think I misunderstood, I thought you meant that the object flying from right to left was falling down, which a transition would fix.

You have other objects on the screen you don’t want to fall then yes, kinematic may be one option with some tweaking :slight_smile: [import]uid: 52491 topic_id: 24222 reply_id: 97891[/import]

I’ve had serious trouble positioning physics objects with transitions because it’s absolute fighting the physics engine.

One thing to try would be weld joining the dynamic obj to a static obj and moving the static obj absolutely as it’s position in the physics world isn’t calculated. [import]uid: 8271 topic_id: 24222 reply_id: 97893[/import]

Hi horacebury,

I was actually trying that right now! (as a test, creating a square object that had the same x,y-axis of my “static” player… I will post up soon, how it goes… thanks [import]uid: 129334 topic_id: 24222 reply_id: 97894[/import]

If you want it to fly in a straight line, the other thing you could try is creating an anchor object (small, static, off screen object) and creating a piston joint parallel with the floor. Applying X force would push the object along the joint, as if it’s on a piston (the Box 2D “revolute” joint) and but not let it fall. If you want it to move up or down the screen, just move the anchor object. Objects landing on top of the flying object would cause it to sag a little, which is probably a nice effect if you’re looking to drop things on it and have it react. However, that effect of coming “off” the piston is a side-effect of the “mathematical approximation” plaguing other less desirable parts of the current implementation of Box 2D in Corona. Though I assume this would be fixed soon, so it might not be around long. Worth a try. [import]uid: 8271 topic_id: 24222 reply_id: 97900[/import]

horacebury,

Thanks, and currently with my limited knowledge, all of what you said just went over my head! I will probably go back to what you just said, later on, but for now, I did get what I was looking for:
Flying objects (using “kinematic”). I still need to fiddle with it, so that it works better, but it’s better than what I had before!

BTW, I initially learn best by example, and if you know of any code examples or tutorials of what you just told me, that would be awesome. Just reading that stuff made my head spin.

Cheers,
Mark A [import]uid: 129334 topic_id: 24222 reply_id: 97901[/import]

Ok, well, looks like I was kinda wrong in my last statement anyway.

Here is a little code which places a square which you can push along a piston joint (the box 2d “prismatic” - not revolute - joint.)

Tap anywhere to drop a circle and tap left/right on the square to push it left/right.

Be aware that the effect sagging off the piston is no longer there - I assume because Ansca fixed this and I just had not caught on yet.

But anyway, this is how to make a dynamic object move horizontally:

main.lua
[lua]physics = require(“physics”)
physics.start()
physics.setDrawMode(“hybrid”)

local anchor = display.newCircle( 100,100,5 )
physics.addBody( anchor, “static”, { density=1, friction=.1, bounce=.2, radius=5 } )

local box = display.newRect( 200,200,50,50 )
physics.addBody( box, “dynamic”, { density=1, friction=.1, bounce=.2 } )

– args: joint name, anchor obj, moving obj, anchored location x and y, piston direction x and y
local joint = physics.newJoint( “piston”, anchor, box, anchor.x, anchor.y, 10, 0 )

function box:tap(event)
if (event.x < box.x) then
box:applyForce(50,0)
else
box:applyForce(-50,0)
end
return true
end
box:addEventListener(“tap”,box)

function tap(event)
local circle = display.newCircle(event.x,event.y,25)
physics.addBody(circle,“dynamic”, { density=1, friction=.1, bounce=.2, radius=25 })
return true
end
Runtime:addEventListener(“tap”,tap)[/lua] [import]uid: 8271 topic_id: 24222 reply_id: 97904[/import]

I also posted this, here:

http://developer.anscamobile.com/content/game-edition-physics-joints#comment-19026

It’s worth knowing that using ‘isFixedRotation=true’ on an object stops the piston joint from being applied. [import]uid: 8271 topic_id: 24222 reply_id: 97905[/import]

Flying things are not well done in Corona SDK. I just did a flying app and it created all kinds of problems for me. I ended up making gravity (0,0) and then added linearForce to things that needed to fall.
The solution to this is for “kinematic” objects to generate collisions. According to the docs the only difference between kinematic and dynamic is kinematic is supposed to ignore gravity. But this isn’t the case. Kinematic ignores gravity and collisions.

I would hope at some point, Ansca would address this and allow kinematic things to generate collisions or allow physics objects to have their own gravity.

At one point I had setup a runtime eventlistener on “enterFrame” and was applying linearforce to try and counter gravity, but you would see the item fall down a bit then get pushed back up and it didn’t look all that good.
[import]uid: 19626 topic_id: 24222 reply_id: 97955[/import]

Hi horacebury,

Great idea - that’s awesome, I may just go with using anchors and joints! It works great. Thanks!

[import]uid: 129334 topic_id: 24222 reply_id: 98157[/import]