Buffalo Posted March 23, 2008 Share Posted March 23, 2008 I have a problem with maths, i need to attach an object to the front of car: local x, y, z = getElementPosition ( source ) local xo, yo, zo = getElementPosition ( source ) local theVehicle = getPlayerOccupiedVehicle ( source ) local a,b,r = getVehicleRotation ( theVehicle ) x = x - math.sin ( math.rad(r) ) * 20 y = y + math.sin ( math.rad(r) ) * 20 permRamp [source] = createObject ( 1655, x, y, z, 0, 0, r ) -- <-- creates in front of it attachElementToElement(permRamp [source] , source, x-xo, y-yo, 0, 0, 0, 0) -- <-- attaches on different pos it creates a ramp in front of vehicle, but attaches it every time on different sides of it. Link to comment
DiSaMe Posted March 23, 2008 Share Posted March 23, 2008 I think you have to change y = y + math.sin ( math.rad(r) ) * 20 to: y = y + math.[b]cos[/b] ( math.rad(r) ) * 20 Link to comment
tma Posted March 23, 2008 Share Posted March 23, 2008 ^^^ There's that too but it's not the main reason it's incorrect. You're confusing your math or confusing what you want to do. If you want to just stick something to the front of the car then you only need the attachElement call really. I just tried your code and made this change : attachElementToElement(permRamp[player] , theVehicle, 0,5,0,0,0,0) Note: your code was using "source" but I assume that's wrong ? Not seeing the whole code I assume that's the player (you get the occupied vehicle for source). You don't need to move the glueing object into position first - I just make them slightly above the target (vehicle/player) and then attach. The above change sticks a ramp to the front of your vehicle - at the same angle each time (this is was attachElementToElement() does). However, if you're wanting to glue objects to others at arbitrary arbitrary angles/distances you need to change your math. You need to rotate the vector from the player/vehicle to the attaching object, around the player/vehicle position. Link to comment
Buffalo Posted March 24, 2008 Author Share Posted March 24, 2008 I think you have to change y = y + math.sin ( math.rad(r) ) * 20 to: y = y + math.[b]cos[/b] ( math.rad(r) ) * 20 Ahh, it was correct, likely messed up while copy pasting code, as i deleted few things And yes, source is definetely a player as function getPlayerOccupiedVehicle says that Yes , i want to just stick something to the front of the car, but it needs good math's count, as now: attachElementToElement(permRamp [source] , source, 0, y-yo, 0, 0, 0, 0) you just increased y, what means it would only work while driving vehicle to the north, i tested that, but i need that it would stick to the front of vehicle in that pos the object was created, and would stay sticked. Link to comment
tma Posted March 24, 2008 Share Posted March 24, 2008 i tested that, but i need that it would stick to the front of vehicle in that pos the object was created, and would stay sticked. Then you need to do as I suggested - rotate the vector from the player/car to the object around the player/car position. This creates the correct xyz offset. The correct attaching angles are determined from the object rotation, the Z rotation being the difference between the objects and the player/vehicle. Link to comment
Buffalo Posted March 24, 2008 Author Share Posted March 24, 2008 Yeah, thats maths, and my prob too, as i cant do it, and asking for help in maths Link to comment
erorr404 Posted March 24, 2008 Share Posted March 24, 2008 Why are you using xo and yo in attachElementToElement? You just need to specifiy the offset of the element, not it's world position. Link to comment
Buffalo Posted March 25, 2008 Author Share Posted March 25, 2008 Yes, i know that, it calculates offset of the element, not it's world position. But its meanless while it calcs wrong offset. Link to comment
erorr404 Posted March 25, 2008 Share Posted March 25, 2008 I'm not sure what you're trying to do here. Do you want it to be attached to the front of the vehicle? Or do you want it to be attached at some other angle? Link to comment
tma Posted March 25, 2008 Share Posted March 25, 2008 He wants to stick objects together at arbitrary angles/distance - that's why he needs to rotate the relative vector around the origin (of the player/car). Just google for "2d point rotation" or similar and you should find source code. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now