Jump to content
Sign in to follow this  
mindstorm

Attachto issue

Recommended Posts

I'm trying to write a script that attaches an object to another object based on the position in the 3den editor (object to a vehicle). Basically a-team style pimp my vehicle without having to sort out all the specific attachment offsets.

 

So far I have come up with the following script, which works but it has a two issues;

 

1. The vectorDir and up aren't super accurate. The objects are slightly rotated and titles

2. Sometimes, at random, some objects are way off on the z-axis, while the same object on a different spot is where it should be. The change is only on the z-axes and can be minus or plus what it should be.

 

This is the script:

object init: [this, vehicle] call fnc_edenAttachTo;

if!(isServer) exitWith {};
params ["_object","_attachToObject"];
_object disableCollisionWith _attachToObject;
_this spawn
{
	private ["_attachToObject", "_object", "_originalPos", "_offset", "_attachOffset", "_truckheight", "_objheight", "_v", "_vectorDirUp", "_vectorUp", "_dirOffset", "_timeout", "_attached", "_offsetAfterAttach", "_offsetDiff", "_offsetToOriginal"];
	params ["_object","_attachToObject",["_offset",[]],["_attachOffset",[]]];
	
	waitUntil {time > 5};
	waitUntil {_attachToObject getVariable ["canStartAttach", true]};
	_attachToObject setVariable ["canStartAttach", false];
	
	//Without delays vectorDirUp only works half of the time
	sleep 0.2;
	_attachToObject enableSimulation false;
	_object enableSimulation false;
	_originalPos = position _object;
	_object setVariable ["originalPos", _originalPos];
	
	if(count _offset == 0) then
	{
		_offset = _attachToObject worldToModel (position _object);
	};
	
	if(count _attachOffset > 0) then
	{
		_offset = _offset vectorAdd _attachOffset;
	};

	_truckheight = (getPos _attachToObject) select 2;
	_objheight = (getPos _object) select 2;

	_v = _attachToObject worldToModel [0,0,0]; 
	_vectorDirUp = [
		_attachToObject worldToModel vectorDir _object vectorDiff _v,
		_attachToObject worldToModel vectorUp _object vectorDiff _v 
	];
	
	_object attachTo [_attachToObject, _offset];

	//diag_log format ["%1: Setting to %2", _object,  _vectorDirUp];
	_timeout = time + 3;
	sleep 0.1;
	waitUntil {
		_attached = if (!isNull attachedTo _object) then { if ((attachedTo _object) isEqualTo _attachToObject) exitWith { true }; false } else { false };
		(_attached || (time > _timeout))
	};
	_object setVectorDirAndUp [[0,0,1],[0,0,1]];
	sleep 0.05;
	_object setVectorDirAndUp _vectorDirUp;

	//Do a check if the new offset is what we predicted, if not there's an issue with the central attachmentposition of the attachObject, calculate offset and save for future attachments
	if(count _attachOffset == 0) then
	{
		_attachToObject enableSimulation true;
		_object enableSimulation true;
		_offsetAfterAttach = _attachToObject worldToModel (position _object);
		_offsetDiff = _offsetAfterAttach vectorDiff _offset;
		_offsetToOriginal = _originalPos vectorDiff (position _object);
		//diag_log format ["Obj: %5 Offset: %1 After attach: %2. Diff: %3. Vs original: %4", _offset, _offsetAfterAttach, _offsetDiff, _offsetToOriginal, _object];
		//_attachToObject setVariable [format ["attachOffset%1", typeOf _object],_offsetDiff vectorMultiply -1];
		if(vectorMagnitude _offsetDiff > 0.1) then
		{	
			[_object, _attachToObject, _offset, _offsetDiff vectorMultiply -1] spawn fnc_edenAttachTo;
		};
	};
	_attachToObject enableSimulation true;
	_object enableSimulation true;
	_attachToObject setDamage 0;
	_attachToObject setVariable ["canStartAttach", true];
};

I'm hoping someone can point me in the right direction on how to fix the issues i'm having. I've been trying several things but not successful so far.

Share this post


Link to post
Share on other sites

Sorry, until now i dont know what the script is exactly for.

Could u explain what the object is which should be attached and which object it should be attached to. A real example could me help to understand what u like to do.

Another question is why u need to manipulate vectordir and vector up. Because if u have to do that every frame then I think its useless to attach the object cause the advantage of attachTo is that u dont need to worry about those vectors. if that is needed you could setpos instead.

 

 

EDIT:

The following line is definetly wrong. There must be an angle of 90 degree at every time between vector dir and vector up. In that code they r pointing in the same direction which is not allowed.

_object setVectorDirAndUp [[0,0,1],[0,0,1]];
it could be

_object setVectorDirAndUp [[0,0,1],[0,1,0]];
or

_object setVectorDirAndUp [[0,1,0],[0,0,1]];

What is that line for? getting worlds center position in model space? for me it seems to be without of sense.

_v = _attachToObject worldToModel [0,0,0];
Edited by sarogahtyp

Share this post


Link to post
Share on other sites

Late reply, been sick.

 

What I want to achieve is that the objects I place in the editor are attached to a vehicle in the way I place them in the editor.

This is the editor view for example:

d228b8c53b.jpg

 

And here is how it looks now, as you can see the vectordirup is slightly off. Also the position of some object is also off (which seems to be at random).

7535da9de9.jpg

 

The _v = _attachToObject worldToModel [0,0,0]; part is something I found in the Get Wrecked code. They use it to attach stuff to vehicles as well. They don't use vectorUp so I guess that's why it works for them?

Share this post


Link to post
Share on other sites

That's basically the same version I'm using (changed it to worldToModel

  for testing) . But for some reason the rotation is off by a bit. And with some rotations it completely messes things up (when I rotate objects >90 degree mainly). 

 

For now It seems to happen when rotations are >90 degrees and especially with some objects. I'm just going to avoid using those I suppose.

 

Thx for the reply!

Share this post


Link to post
Share on other sites

You obviously doing something wrong. Just tested both functions in the link I gave you, everything works as expected under any angle.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×