To steer an object with the direction of a force?

Hey guys,

I want to steer a tiny car with the accelerometer event. The user should have a top view of an environment and a car with a given velocity. When the user tilt the devide the car should turn left or right by holding the velocity constantly. 

Could it be a good way to steer the car by changing the angle of a linear force of the car object. The angle should change in relation to accelerometer event. Or is there any better way? Any ideas?

Thanks in advance!

It really depends how your car is built and what the mechanism you use to drive it is. If you’re just pushing it along with applyForce it won’t look very realistic when you steer it. I would drive it from the wheels. There’s a number of demo’s like this around if you take a look on google.

Thank you!

I forgot to write that it could be another object like an animal as well.

My solution so far is to relate the movement of the object to the rotation like this:

local object = display.newImage("object.png"); local rotateSpeed = 10; local rotateAngle; local xChange; local yChange; local currentRotation; local currentRotationRadiant; local generalSpeed = 10; local function rotate(inEvent) rotateAngle = inEvent.yGravity \* rotateSpeed \* -1; object:rotate(rotateAngle); end local function move(inEvent) currentRotation = object.rotation; currentRotationRadiant = (math.pi/180) \* currentRotation; xChange = math.sin(currentRotationRadiant) \* generalSpeed; yChange = math.cos(currentRotationRadiant) \* generalSpeed \* -1; object.x = object.x + xChange; object.y = object.y + yChange; end Runtime:addEventListener("enterFrame", move); Runtime:addEventListener("accelerometer", rotate);

It works fine and its sufficient.

It really depends how your car is built and what the mechanism you use to drive it is. If you’re just pushing it along with applyForce it won’t look very realistic when you steer it. I would drive it from the wheels. There’s a number of demo’s like this around if you take a look on google.

Thank you!

I forgot to write that it could be another object like an animal as well.

My solution so far is to relate the movement of the object to the rotation like this:

local object = display.newImage("object.png"); local rotateSpeed = 10; local rotateAngle; local xChange; local yChange; local currentRotation; local currentRotationRadiant; local generalSpeed = 10; local function rotate(inEvent) rotateAngle = inEvent.yGravity \* rotateSpeed \* -1; object:rotate(rotateAngle); end local function move(inEvent) currentRotation = object.rotation; currentRotationRadiant = (math.pi/180) \* currentRotation; xChange = math.sin(currentRotationRadiant) \* generalSpeed; yChange = math.cos(currentRotationRadiant) \* generalSpeed \* -1; object.x = object.x + xChange; object.y = object.y + yChange; end Runtime:addEventListener("enterFrame", move); Runtime:addEventListener("accelerometer", rotate);

It works fine and its sufficient.