Changing An Object's Direction On 'sensor Collision'.

Hi everyone.

 

I’m currently building a top-down racer, which is going well on the whole, but I have a technical problem that I can’t solve…

 

On my racetrack there’s a ‘power-up’, it’s a turntable (that’s constantly rotating) with an arrow on it… The idea is to add velocity to the ‘car’ in the direction of the arrow… So, if you hit the arrow when it’s pointing the wrong way, it fires you off in that particular direction.

 

I have no problem affecting the ‘car’s’ velocity, but I don’t know how to change the car’s direction based on the rotational value (angle) of the ‘turntable’. Any help here would be appreciated, I’ve been tinkering for about 6 hours but I’m getting no closer to a solution. So if you know of a ‘bit-of-script’, a tutorial or old thread that addresses a similar issue, I’ll be eternally grateful.

 

Mark

Each display object has a .rotation value. If you set the car’s .rotation value to the same as the turntable’s you should be fine.

Thanks horacebury, I can always count on you to step in where nobody elese will :-)… I’ve tried the solution, but it’s not quite right (for me anyway). In some instances, my car ‘drifts’ around the corners, which means the sprite doesn’t necessarily point in the direction-of-travel 100% of the time. I just want to change the direction based on the angle of the ‘turn table’ without relying on the ‘car’s’ rotation value. Is this possible?

 

 

I just found this code on the forum (maybe you recognise it :slight_smile:

 

[lua]

 

local pt = mathapi.rotateAboutPoint( {x=ship.x,y=ship.y-60}, ship, ship.rotation )
        local bullet = display.newCircle( bullets, pt.x, pt.y, 7 )
        bullet.class = “bullet”
        physics.addBody( bullet, “dynamic”, { friction=0, bounce=0, density=1, radius=7 } )
        bullet.isBullet = true
        bullet:applyForce( pt.x - ship.x, pt.y - ship.y, ship.x, ship.y )

 

[/lua]

 

I’ mgoing to have a play with it… I haven’t come across the “rotateAboutPoint” API ref’ until now.

You might find this useful: http://springboardpillow.blogspot.co.uk/2012/04/sample-code.html

 

If you want to rotate the car by the amount the turntable is turned, rather than snap the car to the same rotation, then you’ll need to identify the difference between their rotations and turn the car that amount. It’s trickier than it sounds, but my mathlib has a function for that: smallestAngleDiff

Would this be the case if using ‘isFixedRotation = true’ ? - I’ve got Sensors in the corners that call this variable if the ‘car’ comes in too fast.

isFixedRotation is a value for telling the physics engine not to apply rotation to an object when collisions would otherwise cause it to rotate. so, if you’ve set that to true I don’t know what the result of setting the objects .rotation property would do. Do you want the car to turn suddenly or have turning applied over, say, half a second?

It’s more fun if the turn-table has an immediate and unnatural effect, so a sudden jerk in the appropriate direction is what I’m trying to achieve… So, to clarify, the ‘car’ hits the turn-table and ‘fires off’, based on the angle of the turntable, without affecting the rotation value of the car, just a simple change of (x, y) direction of travel. (I can apply subtle changes to the car’s movement, to make it more realistic, after I sort out the initial headache)

body:setLinearVelocity()

This is what I started with, I just couldn’t figure out how to make it correspond to the angle (rotation value) of the turntable… iT’s a real headache

 

I’m now playing with

 

[lua]

local speed = 100

object:setLinearVelocity(math.sin(math.rad(object.rotation)) * speed, math.cos(math.rad(object.rotation)) * -speed)

[/lua]

Use my rotateTo function:

 

[lua]

 

local carVx, carVy = car:getLinearVelocity() – get car’s direction vector (x,y value representing speed and direction of the car)

local len = lengthOf( {x=0, y=0}, {x=carVx, y=carVy} ) – get the speed of the car

local rotated = rotateTo( {x=0, y=-10}, 90 ) – use a rotated point to move the speed/direction of the car to the direction of the turntable

car:setLinearVelocity(rotated.x, rotated.y) – set the new velocity of the car, without rotating the car

[/lua]

 

Please note: “rotateTo” would more correctly be named “rotateBy” - I will be updating my Code Exchange’d library soon!

Wow thanks, that’s genius

Each display object has a .rotation value. If you set the car’s .rotation value to the same as the turntable’s you should be fine.

Thanks horacebury, I can always count on you to step in where nobody elese will :-)… I’ve tried the solution, but it’s not quite right (for me anyway). In some instances, my car ‘drifts’ around the corners, which means the sprite doesn’t necessarily point in the direction-of-travel 100% of the time. I just want to change the direction based on the angle of the ‘turn table’ without relying on the ‘car’s’ rotation value. Is this possible?

 

 

I just found this code on the forum (maybe you recognise it :slight_smile:

 

[lua]

 

local pt = mathapi.rotateAboutPoint( {x=ship.x,y=ship.y-60}, ship, ship.rotation )
        local bullet = display.newCircle( bullets, pt.x, pt.y, 7 )
        bullet.class = “bullet”
        physics.addBody( bullet, “dynamic”, { friction=0, bounce=0, density=1, radius=7 } )
        bullet.isBullet = true
        bullet:applyForce( pt.x - ship.x, pt.y - ship.y, ship.x, ship.y )

 

[/lua]

 

I’ mgoing to have a play with it… I haven’t come across the “rotateAboutPoint” API ref’ until now.

You might find this useful: http://springboardpillow.blogspot.co.uk/2012/04/sample-code.html

 

If you want to rotate the car by the amount the turntable is turned, rather than snap the car to the same rotation, then you’ll need to identify the difference between their rotations and turn the car that amount. It’s trickier than it sounds, but my mathlib has a function for that: smallestAngleDiff

Would this be the case if using ‘isFixedRotation = true’ ? - I’ve got Sensors in the corners that call this variable if the ‘car’ comes in too fast.

isFixedRotation is a value for telling the physics engine not to apply rotation to an object when collisions would otherwise cause it to rotate. so, if you’ve set that to true I don’t know what the result of setting the objects .rotation property would do. Do you want the car to turn suddenly or have turning applied over, say, half a second?

It’s more fun if the turn-table has an immediate and unnatural effect, so a sudden jerk in the appropriate direction is what I’m trying to achieve… So, to clarify, the ‘car’ hits the turn-table and ‘fires off’, based on the angle of the turntable, without affecting the rotation value of the car, just a simple change of (x, y) direction of travel. (I can apply subtle changes to the car’s movement, to make it more realistic, after I sort out the initial headache)

body:setLinearVelocity()

This is what I started with, I just couldn’t figure out how to make it correspond to the angle (rotation value) of the turntable… iT’s a real headache

 

I’m now playing with

 

[lua]

local speed = 100

object:setLinearVelocity(math.sin(math.rad(object.rotation)) * speed, math.cos(math.rad(object.rotation)) * -speed)

[/lua]

Use my rotateTo function:

 

[lua]

 

local carVx, carVy = car:getLinearVelocity() – get car’s direction vector (x,y value representing speed and direction of the car)

local len = lengthOf( {x=0, y=0}, {x=carVx, y=carVy} ) – get the speed of the car

local rotated = rotateTo( {x=0, y=-10}, 90 ) – use a rotated point to move the speed/direction of the car to the direction of the turntable

car:setLinearVelocity(rotated.x, rotated.y) – set the new velocity of the car, without rotating the car

[/lua]

 

Please note: “rotateTo” would more correctly be named “rotateBy” - I will be updating my Code Exchange’d library soon!