Best way for moving physics based charactrers on screen?

I wonder which is the best way to move physics objects on the screen?

I want something like this:

A map based game with some obstacles (physics objects)

Characters on the map (also have physics)

Now I want to move the characters by themselves… for which I normally are using the transition.to functions.

But now the characters have physics and can get into each others way or hit another physics based object on the map.

What is the best way to handle character movement for physics characters in a game?

Can you be a little more descriptive, or perhaps drawn a concept image?

Is your game from top-down perspective or is it a side-scrolling platfromer, or something completely different? The answers to each will vary greatly.

top down game.

When using transition.to with physics characters they push away other standing characters in their way. When different characters are moving at the same time they move through each others like if they were normal non-physics objects.

I would like to know how to handle movement of physics objects in a best way, so I have total control over when and which other physics objects are pushed away.

The transition.to seems not to be the best way to approach this.

https://docs.coronalabs.com/api/type/Body/bodyType.html

The  best way to handle the movement of physics objects is to let the physics engine handle all dynamic objects and not to touch them otherwise.You can freely transition and otherwise move static and kinematic bodies around. The key with physics bodies is that dynamic bodies are really the only ones that interact with anything else. If you are transitioning dynamic bodies, then you are fighting the physics engine itself

Now, you still weren’t that descriptive about your needs and I don’t know what kind of game were are talking about, so I don’t know if you can use static, dynamic or kinematic objects. With a racing game, it’d be important to have dynamic bodies for all cars. For some other types of games, you might only need a dynamic body for the player character, etc.

You didn’t mention what you actually wanted to happen if two objects collide. If you are using transitions and you just want them to stop, then pause or cancel the transitions on collision. If you want them to collide and bounce off into separate directions (like in real collisions), then you’d need to move the objects via linear velocity or impulse, for instance. If you don’t have linear damping or gravity in your game, then the objects won’t slow down unless they collide with something.

This is a common question, here’s one answer: https://forums.coronalabs.com/topic/74900-how-to-permanently-set-bodys-x-position/?p=395111

Thanks for your help!

Regarding the type of game your race car sample works. I would need moving cars which can collide or are moving others aside.

The main question remains: How can I move the cars? Normally I would use transition.to or object.x=object.x+1 or something like this. But this would “fight” the physics engine. So how can I do this the right way?

How can I move the cars exactly to a given position for example using the physics engine? Give a car a goalposX and goalposY and make it possible to reach exact this pos in case it doesn’t collide with another car before?

A simple game would be: I have moving cars on screen, and one I want to move manually by clicking on the screen somewhere.

Now the car should move to the clicked position, but in case it collides it moves the other car to the side or is moved by the other car to the side (based on the type of physics collision, angle, etc. … means let physics handle the outcome.)

There’s quite a number of ways to accomplish that. Especially with cars.

Here’s a simple example of what you could do:
 

local physics = require("physics") physics.start() physics.setDrawMode( "hybrid" ) physics.setGravity( 0, 0 ) -- create the car local car = display.newRect( display.contentCenterX, display.contentCenterY, 30, 20 ) physics.addBody( car, "dynamic", { density=1 } ) car.isFixedRotation = true -- change the car's direction local carSpeed = 40 local function randomDirection() local randomDirection = math.random( -180, 180 ) car.rotation = randomDirection car.angularVelocity = 0 randomDirection = math.rad( randomDirection ) car:setLinearVelocity( math.cos(randomDirection)\*carSpeed, math.sin(randomDirection)\*carSpeed, car.x, car.y ) end timer.performWithDelay( 2000, randomDirection, 0 ) -- create obstacles local obstacle = {} local obstacleCount = 25 for i = 1, obstacleCount do obstacle[i] = display.newCircle( math.random( 20, 460 ), math.random( 20, 300 ), 12 ) physics.addBody( obstacle[i], "dynamic", { density=0.2, radius=12 } ) end

This is just a simple example. “Proper cars” would require more work as they aren’t just boxes propelled from their centre, but instead they are pulled by their wheels. Meaning the cars can slide sideways, their rotation isn’t locked, they either have front wheel, back wheel or four wheel dive, etc. This just just meant to give you and idea.

Some other steps that you can take, if you want to “fake transitions” is to send an object in some direction and calculate how long it would take that object to reach the destination or you could add an invisible small circle for the object to collide with, which would be located at the destination, etc.

Happy coding!

Thanks!

I have experimented using a TouchJoint which is bringing the result I want. Just one problem:

When setting the position of the rouch joint the attached object (character) is moved to the position of the joint target very fast and I need a slow movement. How can I do this? Is it even possible?

One more thing I would like to achieve:

When having some dynamic bodies on screen (still, without any gravity or force) and a moving body (moved by moving a touch joint) the moving body is moving the other bodies away.

What is the best way to move back all the moved bodies to their original position when the moving body is through?

Like for example a still crowd of bodies and one is pushing them aside but when it is through they should move back to the original position before they were pushed aside. Which joint is the best to achieve this?

Can you be a little more descriptive, or perhaps drawn a concept image?

Is your game from top-down perspective or is it a side-scrolling platfromer, or something completely different? The answers to each will vary greatly.

top down game.

When using transition.to with physics characters they push away other standing characters in their way. When different characters are moving at the same time they move through each others like if they were normal non-physics objects.

I would like to know how to handle movement of physics objects in a best way, so I have total control over when and which other physics objects are pushed away.

The transition.to seems not to be the best way to approach this.

https://docs.coronalabs.com/api/type/Body/bodyType.html

The  best way to handle the movement of physics objects is to let the physics engine handle all dynamic objects and not to touch them otherwise.You can freely transition and otherwise move static and kinematic bodies around. The key with physics bodies is that dynamic bodies are really the only ones that interact with anything else. If you are transitioning dynamic bodies, then you are fighting the physics engine itself

Now, you still weren’t that descriptive about your needs and I don’t know what kind of game were are talking about, so I don’t know if you can use static, dynamic or kinematic objects. With a racing game, it’d be important to have dynamic bodies for all cars. For some other types of games, you might only need a dynamic body for the player character, etc.

You didn’t mention what you actually wanted to happen if two objects collide. If you are using transitions and you just want them to stop, then pause or cancel the transitions on collision. If you want them to collide and bounce off into separate directions (like in real collisions), then you’d need to move the objects via linear velocity or impulse, for instance. If you don’t have linear damping or gravity in your game, then the objects won’t slow down unless they collide with something.

This is a common question, here’s one answer: https://forums.coronalabs.com/topic/74900-how-to-permanently-set-bodys-x-position/?p=395111

Thanks for your help!

Regarding the type of game your race car sample works. I would need moving cars which can collide or are moving others aside.

The main question remains: How can I move the cars? Normally I would use transition.to or object.x=object.x+1 or something like this. But this would “fight” the physics engine. So how can I do this the right way?

How can I move the cars exactly to a given position for example using the physics engine? Give a car a goalposX and goalposY and make it possible to reach exact this pos in case it doesn’t collide with another car before?

A simple game would be: I have moving cars on screen, and one I want to move manually by clicking on the screen somewhere.

Now the car should move to the clicked position, but in case it collides it moves the other car to the side or is moved by the other car to the side (based on the type of physics collision, angle, etc. … means let physics handle the outcome.)

There’s quite a number of ways to accomplish that. Especially with cars.

Here’s a simple example of what you could do:
 

local physics = require("physics") physics.start() physics.setDrawMode( "hybrid" ) physics.setGravity( 0, 0 ) -- create the car local car = display.newRect( display.contentCenterX, display.contentCenterY, 30, 20 ) physics.addBody( car, "dynamic", { density=1 } ) car.isFixedRotation = true -- change the car's direction local carSpeed = 40 local function randomDirection() local randomDirection = math.random( -180, 180 ) car.rotation = randomDirection car.angularVelocity = 0 randomDirection = math.rad( randomDirection ) car:setLinearVelocity( math.cos(randomDirection)\*carSpeed, math.sin(randomDirection)\*carSpeed, car.x, car.y ) end timer.performWithDelay( 2000, randomDirection, 0 ) -- create obstacles local obstacle = {} local obstacleCount = 25 for i = 1, obstacleCount do obstacle[i] = display.newCircle( math.random( 20, 460 ), math.random( 20, 300 ), 12 ) physics.addBody( obstacle[i], "dynamic", { density=0.2, radius=12 } ) end

This is just a simple example. “Proper cars” would require more work as they aren’t just boxes propelled from their centre, but instead they are pulled by their wheels. Meaning the cars can slide sideways, their rotation isn’t locked, they either have front wheel, back wheel or four wheel dive, etc. This just just meant to give you and idea.

Some other steps that you can take, if you want to “fake transitions” is to send an object in some direction and calculate how long it would take that object to reach the destination or you could add an invisible small circle for the object to collide with, which would be located at the destination, etc.

Happy coding!

Thanks!

I have experimented using a TouchJoint which is bringing the result I want. Just one problem:

When setting the position of the rouch joint the attached object (character) is moved to the position of the joint target very fast and I need a slow movement. How can I do this? Is it even possible?

One more thing I would like to achieve:

When having some dynamic bodies on screen (still, without any gravity or force) and a moving body (moved by moving a touch joint) the moving body is moving the other bodies away.

What is the best way to move back all the moved bodies to their original position when the moving body is through?

Like for example a still crowd of bodies and one is pushing them aside but when it is through they should move back to the original position before they were pushed aside. Which joint is the best to achieve this?