character velocity (Solved)

This is my first day with Corona, however I have quite the experience with UDK, unfortunately not with LUA. I went through all the Docs from Resources and a few random tutorials trying to figure this out.

What I’m trying to do are a few, hopefully, simple things, at least they seem simple enough.

I have my gravity currently set at 10, but the number isn’t important. This also reminds me of a new question actually sorry. I just created a random background that’s 320x480 and added an addEventListener so that when I touch the background it does the onTouch function, my question regarding this is, if there’s something over the background and I touch there will the function not work? Only when the background get’s touched? If so, how would I do it whenever the person touches the screen no matter what he’s touching.

Anyway back to my other question. So gravity is set to 10. I want to create it so on touch the gravity is set to -10 so that the player goes the other way until he’s not touching the screen anymore. Going up and down at the same speed.

I also been trying to look into player creation, but I just want the bare minimum for the player to exist currently with no collision, how is this:

local function createPlayer(x, y, width, height, rotation)  
 local p = display.newRect(200, 150, 50, 50)  
 p:setFillColor(0, 0, 0)  
 p.objectType = "player"  
 physics.addBody ( p, "dynamic", playerBodyElement )  
 p.isSleepingAllowed = false  
  
 return p  
end  

Sorry again for the newbie question, thanks in advance for any help, really appreciate it. I wish you a nice weekend. [import]uid: 77199 topic_id: 23082 reply_id: 323082[/import]

Touch events propagate through objects, but you can “return true” to control it:

local function onTouch(event)  
 -- Do something  
  
 return true --Any objects below this won't receive the event now  
end  

As for your gravity question, I think you’re looking for something along these lines (though I haven’t tried it myself).

local function onPlayerTouched(event)  
 if (event.phase == "began") then  
 -- Flip gravity  
 physics.setGravity(0, -10)  
  
 -- Set the touch focus so that the user can drag anywhere on the screen  
 -- and continue touching the object  
 display.getCurrentStage():setFocus(event.target);  
 elseif (event.phase == "ended") then  
 -- Return it to normal when the user releases  
 physics.setGravity(0, 10)  
 -- Release the touch focus so anything can be touched again  
 display.getCurrentStage():setFocus(nil);  
 end  
end  
  
local yourPlayer = createPlayer(...)  
yourPlayer:addEventListener("touch", onPlayerTouched)  

Your createPlayer function looks fine, if you want to manage collisions check out the following doc page:
http://developer.anscamobile.com/content/game-edition-collision-detection

And especially this page for getting the hang of collision filtering:
http://developer.anscamobile.com/forum/2010/10/25/collision-filters-helper-chart [import]uid: 87138 topic_id: 23082 reply_id: 92289[/import]

Haha thank you very much Revaerie for your answer, it’s spot on. I actually was writing a reply to this thread saying I found it out finally and posting my code but you beat me to it, but still can’t thank you enough for your help.

I pretty much did the same thing

local function onScreenTouch( event )  
 if event.phase == "began" then  
 -- make character jump forward  
 physics.setGravity( 0, -10 )  
 elseif (event.phase == "ended" or event.phase == "cancelled")then  
 physics.setGravity( 0, 10 )  
 end  
  
 return true  
end  
  
Runtime:addEventListener( "touch", onScreenTouch )  

However maybe gravity isn’t what I’m looking for and its velocity…? I may be severely wrong though. The thing about gravity is, he holds the touch down and he goes up, if he goes up a lot and let’s go he will still go up a little more before falling. And same thing the other way around, if he’s been falling for half the screen when you touch the screen there is some resistance before he starts going up. My question is how do I remove this resistance? How do I make him INSTANTLY go up on touch and instantly down when he’s not touching anymore?
Am I looking into something else besides gravity?

Anyway, thank you so so much for your reply and writing so much, including the links. I will look over them and continue working on all of this. Thank you once again!! [import]uid: 77199 topic_id: 23082 reply_id: 92290[/import]

I have tried a few other commands I have seen:
applyLineareImpulse
applyForce
Velocity

but they were quite simple, nothing complex. the thing about all of them is that the more you click the speed keeps getting amplified, which i dont want. and also only one click works, i guess it applies the force/impulse/velocity to it once on click (probably how its supposed to work hehe) and then stops, thats why even if im still touching it doesnt go up. gravity would work perfectly except if there wasnt resistance or whatever it is. im still confused on how to do this exactly, would a velocity that loops work the way I want? If someone could point me in the right direction and tell me what syntax I am looking for Id very much appreciate it so I can continue messing with it till I make it work, would be much appreciated. sorry for the trouble and the wall of text. have a great weekend.

EDIT: I heard player velocity might be able to do what I want but It’s failing for me:

local characterVelocity = 0;  
physics.addBody( character, { friction=0, density=0, bounce=0, radius=35 } )  
character.isFixedRotation = true  
local function onScreenTouch( event )  
 if event.phase == "began" then  
 characterVelocity = 0.2;  
 character:applyLinearImpulse(0, -characterVelocity, character.x, character.y);  
 characterVelocity = characterVelocity + 0.01;  
  
 if(characterVelocity \> 1.5)then  
 characterVelocity = 1.5;  
 end  
 elseif (event.phase == "ended" or event.phase == "cancelled")then  
 characterVelocity = 0;  
 end  
  
 return true  
end  
  
Runtime:addEventListener( "touch", onScreenTouch )  

Velocity turns out to be just like gravity in a way, as in if the velocity is to high even on release he will still move a bit before he starts falling down. but in the simulator i have to click multiple times to get my guy to get up higher, since one click pushes him only once. i was hoping to touch and hold it itd constantly go up, but its only one push =( even if i created a loop i feel like itd be just like clicking multiple times meaning the speed would massively amplify and not give me my desired result. so in the end would gravity be the thing im looking for? at this point im just curious what syntax is best for me

kinda like this simple flash game to show you exactly what im talking about
http://www.addictinggames.com/action-games/helicoptergame.jsp [import]uid: 77199 topic_id: 23082 reply_id: 92411[/import]

I think your best bet is to turn off gravity, and just rely on velocity.

I haven’t made anything yet, so I am not sure of how the functions work in lua, but I am assuming that the event phases ‘began’ and ‘ended’ only run once, when you touch or let go.

I’m sure there is a function to tell if the user is holding down on the screen, and it should be there where you include your code for increasing character velocity.

Then ‘began’ can set the velocity to 0.2, and ‘ended’ can set the velocity to -0.2.

Sorry if my post isn’t clear and I’m not using code references, this is my first post and I haven’t messed around enough to know anything off by heart in this language. [import]uid: 130872 topic_id: 23082 reply_id: 92487[/import]

does physics.setGravity( 0, 0 ) count as turning gravity off?

I managed to get it somewhat closer to what I want but there’s still that little resistance, like if he falls down to much then you touch for him to go up it takes half a sec to actually start going up since he still falls down a bit more. Did I remove gravity the wrong way? I also removed density and friction from my ‘character’, they are of 0 value. im not completely sure how those work exactly yet. when I add density it just seems to make it worse although i havent thoroughly tested it. here is what i got after my second day of lua, any comments would be much appreciated as im running out of ideas =(

[code]
local characterVelocity = 0;
local dudeWasFlying = false;
local dudeIsFlying = false;

local physics = require “physics”
physics.start()
physics.setGravity( 0, 0 )

local character = display.newImage( “character.png” )
character.x = 70
character.y = 230
physics.addBody( character, { friction=0, density=0, bounce=0, radius=35 } )

function startHateLua()
characterVelocity = -0.01;
dudeIsFlying = true;
end

function cancelHateLua()
dudeIsFlying = false;
dudeWasFlying = true;
end

local function onScreenTouch( event )
if event.phase == “began” then
startHateLua()
elseif (event.phase == “ended” or event.phase == “cancelled”)then
cancelHateLua()
end

return true
end

local onEnterFrame = function( event )

if(dudeIsFlying == true)then

character:applyLinearImpulse(0, characterVelocity, character.x, character.y);

if(characterVelocity > 0.1)then
characterVelocity = 0.1;
end
elseif(dudeIsFlying == false)then

characterVelocity = 0.01;
character:applyLinearImpulse(0, characterVelocity, character.x, character.y);

if(characterVelocity > 0.1)then
characterVelocity = 0.1;
end
end
end

Runtime:addEventListener( “touch”, onScreenTouch )
Runtime:addEventListener( “enterFrame”, onEnterFrame )
[/code] [import]uid: 77199 topic_id: 23082 reply_id: 92538[/import]

@names, recall that both gravity and impulses are forces. In other words, they cause acceleration. So if you’re traveling in one direction at a certain speed and then you apply a counteracting force, first the current momentum will have to be overcome, the object will momentarily stop completely, then it will start moving in the other direction (assuming a constant application of the force [like gravity], of course :slight_smile: ).

This creates the lag time you’re noticing, which is undesirable for your case. It sounds like the game you want shouldn’t use gravity at all, since I presume you wouldn’t be using it for any other objects anyways?

Things like setLinearVelocity(x, y) allow you to control the exact trajectory of an object without messing with forces, I might try that. You can scale the speed up over time by continually incrementing it up to a certain value until the touch event is terminated.

Here’s my idea modified to your code:

local velTimer, characterVelocity;  
  
physics.addBody( character, { friction=0, density=0, bounce=0, radius=35 } )  
character.isFixedRotation = true  
  
local function onLoveLuaUpwards()  
 if (characterVelocity \> 20) then --Maximum UP speed  
 -- We're already at max speed, so let's cancel the timer for efficiency  
 timer.cancel(velTimer)  
 velTimer = nil  
 else  
 characterVelocity = characterVelocity + 1 --Go faster in the positive direction  
 character:setLinearVelocity(0, characterVelocity \* 5);  
 end  
end  
  
local function onLoveLuaDownwards()  
 if (characterVelocity \< -20) then --Maximum DOWN speed  
 -- We're already at max speed, so let's cancel the timer for efficiency  
 timer.cancel(velTimer)  
 velTimer = nil  
 else  
 characterVelocity = characterVelocity - 1 --Go faster in the negative direction  
 character:setLinearVelocity(0, characterVelocity \* 5);  
 end  
end  
  
local function onScreenTouch( event )  
 local delay = 20 --This determines the rate of acceleration  
 if event.phase == "began" then  
 if (velTimer) then  
 timer.cancel(velTimer)  
 end  
 velTimer = timer.performWithDelay(delay, onLoveLuaUpwards)  
 elseif (event.phase == "ended" or event.phase == "cancelled")then  
 if (velTimer) then  
 timer.cancel(velTimer)  
 end  
 velTimer = timer.performWithDelay(delay, onLoveLuaDownwards)  
 end  
  
 return true  
end  
   
Runtime:addEventListener( "touch", onScreenTouch )  

Fair warning, this is all completely conceptual, I didn’t debug/test any of this, though I believe the logic is sound. Just an idea to get you started…

EDIT: Oops, should have checked for new replies (yay for lots of tabs :). setGravity(0, 0) will indeed disable gravity. [import]uid: 87138 topic_id: 23082 reply_id: 92542[/import]

EDIT: Changed topic name hoping it will help others who search for velocity, not sure if it’s the best thread title though. Also love the onLoveLua in the code =] Thank you so much Revaerie

This is so weird Revaerie! I don’t even wanna try describing it because I would feel like a fool since it doesn’t sound too truthful.

All I gotta say is thank you unbelievably much, that is the exact syntax I was looking for all day. It works perfectly, exactly as I imagined and desired it. I learned that impulse is an acceleration type thing too, how dumb of me to not realize it sooner. You are nothing simply short of amazing, I can’t describe how awesome and how much insurmountable appreciation I have for people helping out newbies. This is my first coding language, well I did study UnrealScript for a few weeks for UDK where I actually had all this working, but I wasn’t advanced at all in it. If I ever get proficient in LUA I will try to pass on your helpfulness and the others who help out to other newbies. Thank you once again for taking the time to read what I wrote and then writing so much explaining and giving me code, ty ty ty!

Thank you very much once again.
Thanks nargalax very much for your help too, you advanced me further into my code. Wish you all a great weekend!
[import]uid: 77199 topic_id: 23082 reply_id: 92545[/import]

No trouble at all, glad I could help.

If I ever get proficient in LUA I will try to pass on your helpfulness and the others who help out to other newbies.

Exactly a reason I try to help out on the forums from time to time ^^ So many useful posts have (and continue) to help me out those countless times I find myself lost - I’d feel remiss not trying to return a small part of the favor where I’m able.

Lua is an intuitive language, and Corona has such a friendly community, it’s what makes this a great place to learn. Let the virtuous cycle continue! :slight_smile: [import]uid: 87138 topic_id: 23082 reply_id: 92552[/import]