effect of gravity moving up hill

I have a circle that I move up hill (30 deg) and gravity set to 9.8.

The circle has density of 2 and little bounce,friction…
The hill was created with points from physic editor.
AS I move the circle up the hill gravity doesnt effect the circle much, even if I increase the gravity setting and/or density of the circle.

I touch a button and move the circle 2 pixels at a time in just X direction which easily gets up the hill. It cant get up the hill at 1 pixel at a time.

How can I more realistically simulate moving an object up a hill?

[lua]physics.start()
physics.setGravity(0,10) ;

local platform={}

local scaleFactor = 1.0
local physicsData =(require “trifloor”).physicsData(scaleFactor)
platform[1] = display.newImage(“trifloor.png”)
physics.addBody( platform[1],“static”, physicsData:get(“trifloor”) )
platform[1].y= _H- platform[1].height/2 ;
platform[1].x=_W/2 +120;
platform[1].myName = “p1”;
physics.addBody (myballImg, {bounce=.3, density=10, radius=16,friction=.3})[/lua] [import]uid: 138547 topic_id: 25809 reply_id: 325809[/import]

The only way I solved this was to use Physics for movements up/down hill.

I had to work out the F=M*A where A=g*sin(theta) where this is angle of the hill . On uphill this is a retrding force.

Then I had to work out where on the hill the object was like uphill or downhill to add/subtract force.

Every hill needs to be a separate object so when colsions occur you can more easily work out the hill dimensions on what you are on.
This is a lot of work and is there an easier way as the Physics engine was too limited. [import]uid: 138547 topic_id: 25809 reply_id: 104777[/import]

Just curious… What method did you originally use to move the ball to the right? You mention 1 or 2 pixels, so did you use a loop to add 1-2 pixels to its X position? If you can post some more code, I might be able to suggest other methods.

Brent Sorrentino
Ignis Design [import]uid: 9747 topic_id: 25809 reply_id: 104780[/import]

originally i just used a simple loop that calls a function to move the image 0.1 (to account for 30fps otherwise the object is to fast)
when it reaches a slope the speed stays constant so it doesnt look real.

local function gameLoop(event)

player:move()

end

Runtime:addEventListener(“enterFrame”, gameLoop)
The way I can solved this has been quite a lot of work and I have to have every slope a different image. [import]uid: 138547 topic_id: 25809 reply_id: 104787[/import]

Did you try some of the physics-based APIs like “applyForce” or “setLinearVelocity”? I have used the first one in a Runtime listener to apply a “build-up” force on an object if it remains within a certain boundary (like a blowing vent). The second is more useful to set a constant velocity, and it could also be used in a Runtime listener as you have tried.

Let me know if you’ve tried these or not. I recall at some time in the past, I faced a similar dilemma with an object moving up a slope, for example a 2D platformer where the player presses a button to move a character around on flat AND sloped surfaces. I seem to recall that the best solution was to apply a constant force to the character while the “move” button was pressed.

Brent
[import]uid: 9747 topic_id: 25809 reply_id: 104788[/import]

if I set setLinearVelocity or applyForce then does the physcis engine automatically slow an object down when moving up hill.
Is the setLinearVelocity a flat speed which doesnt change?

I have tried these before without success but i will try again [import]uid: 138547 topic_id: 25809 reply_id: 104789[/import]

Both methods will slow down when the object reaches a hill/slope, or encounters some other factor (massive friction?) which alters its speed.

If you desire a more constant speed as the object moves up a slope, then you will probably need to set this constant linear velocity during a Runtime loop, while the object is on the slope. I realize that this is somewhat tedious, but it’s easier than calculating different slope “factors” for each slope object.

This constant force could be applied via a user-initiated action, i.e. a button that moves the object left or right. If your app doesn’t utilize that, then you could begin the Runtime listener any time the moving object hits any “slope” object, and end the listener when the object “ends” its collision phase on that slope.

Does this help? Let me know, and maybe I can suggest even more alternative approaches.

Brent Sorrentino
[import]uid: 9747 topic_id: 25809 reply_id: 104791[/import]

if i use this, the body has a constant speed but when it hits a slope it will just stop.
myshape:setLinearVelocity( 30, 0 );

the object cant move any further
I cant see a way around making an object look real under gravity with hills.
you dont know if you are moving up or down a hill unless you keep track of velocity and acceleration [import]uid: 138547 topic_id: 25809 reply_id: 104793[/import]

You can still simulate slope movement by applying velocity while the object encounters a slope (any slope) and during the entire duration that it’s in contact with that slope. Consider it like this… any Box2D object that receives a velocity will act upon that velocity… but IF there is some other factor that enters the scenario, i.e. a slope, ground friction, an opposing “wind” force, etc., then that velocity will be affected appropriately.

So, imagine a car on a road. If you apply a linear velocity to it, the car will go forward in a constant rate, slowing down only by friction of the surface it contacts. Box2D works on the same premise. If this car encounters an upward slope, it will quickly stop by the force of gravity. If you wish the car to continue up the slope, then you need to start its “engine” again, which is effectively what you’re doing by setting velocity or force during a Runtime loop.

You probably wouldn’t even need to “detect” if the car is on a slope or not. You could just constantly run a listener that applies SetLinearVelocity(), because that’s a constant value.

Please comment if this doesn’t help,
Brent [import]uid: 9747 topic_id: 25809 reply_id: 104796[/import]

Hi ,

I understand your idea but Corona doesnt seem to work like this.SetLinearVelocity() is in a listener and this gets set 30fps
but when it comes to a slope it stops dead no matter what.

The object cant be restarted with SetLinearVelocity() with no y value anyway. You dont want a constant speed up hill as this wont look real.
That would be the same as adding an x value in a loop.

Unless someone can give me an example I dont think Corona will give you such real gravity effects where you control a body with a button
going left and right up/down hills with a real gravity effect.
[import]uid: 138547 topic_id: 25809 reply_id: 104804[/import]

Hi jagguy999,

I just tossed together a quick scenario using “applyForce()” instead of setting the exact linear velocity, during a Runtime loop. I think this is what you might consider “realistic” movement of an object up a 30 degree slope, but perhaps I’m just not understanding your ideal behavior.

Set up your “config.lua” resolution (or start a new project) using iPad resolution: 768 x 1024. Then run the following code. I hacked this together really quick but it exhibits the basic concept I was thinking about.

local physics = require("physics") ; physics.start() ; physics.setScale( 60 ) ; physics.setGravity( 0.0,9.8 ) ; physics.setDrawMode( "normal" )  
local playerFilter = { categoryBits=2, maskBits=1 }  
local solidFilter = { categoryBits=1, maskBits=2 }  
  
local platform = display.newRect( 0, 700, 330, 80 ) ; platform:setFillColor(255,255,255)  
physics.addBody( platform, "static", { density=1.0, friction=0.3, bounce=0, filter=solidFilter } )  
  
local slope = display.newRect( 330, 612, 330, 80 ) ; slope:setFillColor(255,255,255)  
slope.rotation = -30  
physics.addBody( slope, "static", { density=1.0, friction=0.3, bounce=0, filter=solidFilter } )  
  
local ball = display.newCircle( 50, 500, 30 ) ; ball:setFillColor(255,100,100)  
physics.addBody( ball, "dynamic", { density=1.0, friction=0.3, bounce=0, radius=30, filter=playerFilter } )  
  
local function moveBall()  
 ball:applyForce(2.5, 0, ball.x, ball.y)  
end  
  
local function startEngine()  
 Runtime:addEventListener( "enterFrame", moveBall )  
end  
  
local t = timer.performWithDelay( 1000, startEngine ) --start "engine" after slight delay  

Is this closer to what you envision? You could enhance this basic concept by assigning a necessary “engine power” to each slope object (or even just calculate it based on the angle of the slope). Then, using a collision function, you could increase the applied force values when the ball first touches the slope… sort of like “stepping on the pedal” if this object was a car. :slight_smile:

Brent
[import]uid: 9747 topic_id: 25809 reply_id: 104933[/import]

Hi,

Nice try with your example but it doesnt work with images.
The ball wont move at all when using images with apply force in same code.

I did get this to work but I had to place every different slope image as a different image and adjust the effect it has on gravity.

Another way is to place a whole shape and perform precollsions and detect the slope using maths.
For a game I will just calculate without exact physics if an object is on a slope and increase the X velocity

[import]uid: 138547 topic_id: 25809 reply_id: 105112[/import]

So you managed to solve it? I’m a bit curious why it wasn’t working with images. If you are so inclined, could you e-mail me a screenshot or a brief video of your app in action? I wouldn’t mind seeing how it’s configured. Up to you, of course… I just wouldn’t mind seeing what you have going at this point.

e-mail: brent (at) ignisdesign (dot) com

Best regards,
Brent Sorrentino
[import]uid: 9747 topic_id: 25809 reply_id: 105113[/import]

sure if you can improve it then let me know. I am still unsure how you make the motorbike game.

[import]uid: 138547 topic_id: 25809 reply_id: 105114[/import]