Page 1 of 1

attempting to get updated position for car rotation.

Posted: Sun Jun 21, 2020 7:33 am
by kingarthur
so i have to cars. 1 is the car driven by the player and the other is updated by script to maintain its position relative to the car. im trying to adjust the position anytime the players car turns to maintain the second car in a relative position of the left side of the vehicle.

the current code i have will rotate the auto pilot car but it rotates the wrong way in comparison to the players car turning direction and tends to freak out when the player is facing south.

Code: Select all

	global.left_car.orientation = global.car.orientation
        local degrees = global.car.orientation*360
        local rad = (degrees*(math.pi/180))*-1
        local left_car_pos
        local old_x = global.left_car.position.x
        local old_y = global.left_car.position.y
        local pivot_x = global.car.position.x
        local pivot_y = global.car.position.y
        old_x = old_x - pivot_x
        old_y = old_y - pivot_y
        local left_x = (old_x*math.cos(rad) - old_y*math.sin(rad)) + pivot_x
        local left_y = (old_x*math.sin(rad) + old_y*math.cos(rad)) + pivot_y
        left_car_pos = {left_x,left_y}
        global.left_car.teleport(left_car_pos)
        global.left_car.speed = global.car.speed
how do i get it so that the autopilot car always stays on the vehicles left side even while rotated?

Re: attempting to get updated position for car rotation.

Posted: Sun Jun 21, 2020 8:18 am
by DaveMcW

Code: Select all

local distance = 5
local rad = global.car.orientation * 2 * math.pi
local x = global.car.position.x - distance * math.cos(rad)
local y = global.car.position.y - distance * math.sin(rad)
global.left_car.teleport{x, y}
global.left_car.orientation = global.car.orientation
global.left_car.speed = global.car.speed

Re: attempting to get updated position for car rotation.

Posted: Sun Jun 21, 2020 8:34 am
by kingarthur
DaveMcW wrote: Sun Jun 21, 2020 8:18 am

Code: Select all

local distance = 5
local rad = global.car.orientation * 2 * math.pi
local x = global.car.position.x - distance * math.cos(rad)
local y = global.car.position.y - distance * math.sin(rad)
global.left_car.teleport{x, y}
global.left_car.orientation = global.car.orientation
global.left_car.speed = global.car.speed
thanks. that seems to be working perfectly.