I think I will use the vx, vy linearVelocity check. Also, If I am correct, I read that I should not check if the velocity is 0, rather a number that is very close to zero, like 0.05.
Plus, I will start looking more at the AskEd Code. Thanks!
I think I will use the vx, vy linearVelocity check. Also, If I am correct, I read that I should not check if the velocity is 0, rather a number that is very close to zero, like 0.05.
Plus, I will start looking more at the AskEd Code. Thanks!
It’s all good. I know it is a lot to remember especially when you are learning a lot of new things at once.
Tip: Calling getVelocity() a lot can be expensive, but if it is for just one object you should be OK.
Also, that is correct. 0 is not a good ‘stopped velocity’
This is better:
local vx, vy = obj:getLinearVelocity() local threshold = 1 -- make bigger to make 'stopped' a greater velocity. local stopped = ( (vx ^2 + vy ^2) \<= threshold )
I noticed the word ‘expensive’ gets thrown around a lot and that there are certain functions that can dent fps and memory more than other ones. Can you list a few or lead me to a tutorial that lists a few?
For instance, I assume the boolean expression:
local stopped = ( (vx ^2 + vy ^2) \<= threshold )
Is more expensive than an if statement…
Don’t worry too much about optimizing, especially if you’re new and especially early. Early optimization is one great way to kill your game/app.
Expensive - This is a pretty loose term and unfortunately, what is and is not expensive (costly in terms of memory and/or cpu cycles and/or bandwidth and or blocking accesses to resources/hardware) varies from situation to situation. Also, it is based on experience not a list of known offenders.
As for the specific question about that line of code… I’m not sure if one of these is more expensive or not…
local stopped = ( (vx ^2 + vy ^2) <= threshold ) … versus … local stopped if( (vx ^2 + vy ^2) <= threshold ) then stopped = true end
I did this:
local stopped = ( (vx ^2 + vy ^2) \<= 1 ) if stopped == true then local jumpVecY = -7 anim.gravityScale = 1 anim:applyLinearImpulse(0, jumpVecY \* anim.mass, anim.x, anim.y ) anim:setSequence("jump") anim:play() end
PS, got the jump to work.