Jump to content

maths script


Buffalo

Recommended Posts

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

^^^ 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
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
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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...