applyForce is too fast
So I'm using the applyForce code to give an object motion, at the moment it's like this:
ball:applyForce(0, 1, 0, 0)
Having it like this, the ball moves nicely. But if I change the 1 to 2 the ball moves really fast.
I saw an example from Corona where they used the applyForce method, and they had the 2nd number set to 20 and the ball moved in a nice pace. But if I set mine to 20 I wont even see the ball because it moves so fast... Does anyone know what the problem might be??
Replies
Add some density to your ball and see how it goes. Also play with the world scaling variable, it might do something. It's in the physics section of the docs.
Angular damping will help with the spinning.
@NahirC Force isn't everything. Experiment with density, friction, gravity, size of Your object. It all has impact on speed of an object.
Thanks for the help, the density helped a bit, though it's still accelerating too fast and the max speed is almost endless ..
Try linear damping.
Wait. Why is it accelerating? What friction are you using? Or do you have it in an enterframe listener?
You shouldn't. It should just be a jolt of energy. If you want to apply constant force use Apply linear force (or something like that).
I've got this code to control the ball with the Corona Remote
local function movement()
-- Get X & Y Accelerometer Input From Remote
local xAcceleration = remote.xGravity
local yAcceleration = remote.yGravity
-- Convert Input To Force
local xForce = xAcceleration * 35
local yForce = yAcceleration * 35
-- Apple Force To Ball
ball:applyForce( xForce , -yForce )
end
Runtime:addEventListener("enterFrame", movement)
---
Then I've got these properties set to the ball
physics.addBody(ball, "dynamic", { density=4.0, friction=18.5, bounce=0,
radius=ballSize, isSensor=true })
--
Is this the wrong way to do it?
Edit: I took these codes from various tutorials, the accelerometer controls I copied from the Corona Remote example, and in that example everything works fine. I have no idea why my ball just flys away
Edit2: angularDamping doesn't help either.. I think someone needs to take a look at my code....
@NahirC read this: http://techority.com/2010/11/19/control-an-actor-with-the-accelerometer/
It should just be a jolt of energy. If you want to apply constant force use Apply linear force (or something like that).
You got that kinda backwards. Forces are constant pressure, impulses are a sudden jolt.
@jocking you're totally right! My bad.
Looking at it I think the 35 is your problem. Since the accelerometer is supposed to be changing gravity, try it out with 10 instead. You might need to lower your density and friction numbers with that number.
I know I got it to work well with those values. If you want to slow it down further add linear dampening at about 0.8. Experiment and let us know what happens.
I set the density to 4.0 and the friction to 180.5 and this works fine. The density is good because I want the ball to reach it's max speed kind of quick, so you've got good control over it. The friction is good, it makes the ball turn from north to south kind of fast. But the max speed problem remains.. Can't find a solution to this anywhere. If I tilt the phone north long enough the ball reaches incredible speeds and flys away from the screen
180 o_O
:D
Fixed it, thanks! I realized I was using linearDamping wrong. I had it inside the addBody(), supposed to be outside :)
Fixed it, thanks! I realized I was using linearDamping wrong. I had it inside the addBody(), supposed to be outside :)
This is an example of the way I use to restrict the velocity of my object under physics:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ball.maxVelocity = 300 — Pick here the max velocity, you can change this — value depending on the level of dificulty without — changing any physics parameter. Works for me :) function ball:enterFrame(event) — Do here the apply force to the ball — Restrict the velocity to a max velocity local vx, vy = self:getLinearVelocity() local m = math.sqrt((vx*vx)+(vy*vy)) if (m>self.maxVelocity) then vx=(vx/m)*self.maxVelocity vy=(vy/m)*self.maxVelocity self:setLinearVelocity(vx,vy) end end Runtime:addEventListener( "enterFrame", ball ) |

And also, using that code I showed in my previous post, ball:applyForce(0, 1, 0, 0), the ball keeps spinning for some reason, and it spins fast too, even though I set everything except the Y-motion to 0.