Quantcast

Jump to content

» «
Photo

Move Objects After Spawn (C++)

3 replies to this topic
HadesOfGTA
  • HadesOfGTA

    Player Hater

  • Members
  • Joined: 25 Jun 2013

#1

Posted 28 June 2013 - 02:08 AM

Right now I am playing with objects, more precisely, trying to figure out how to rotate and move objects AFTER they have been spawned. Right now I am playing with an attached object to my face because it's easier to see and I don't have to go somewhere to test movement. Here is what I am doing at the moment (with no luck, and not even sure if I am on the right path):

CODE

  if(IsBitSet(GetKeyState(VK_CONTROL), 15)) {
   eModel pigeon = MODEL_CJ_PIGEON_06;
   Object newObj;
   f32 zForce = 0.0f;
   f32 yForce = 0.0f;
   f32 xForce = 0.0f;
   AttachObjectToPed(newObj = CreateObjectTemp(pigeon),GetPlayerPed(), BONE_HEAD, 0, 0, 0, 0, 0, 0, false);
   
   while(true){
    if(IsBitSet(GetKeyState(VK_CONTROL), 15)) {
     xForce += 0.1f;
     SetObjectRotation(newObj, xForce, yForce, zForce);
    }else if(IsBitSet(GetKeyState(VK_SHIFT), 15)) {
     yForce += 0.1f;
     SetObjectRotation(newObj, xForce, yForce, zForce);
    }else if(IsBitSet(GetKeyState(VK_MENU), 15)) {
     zForce += 0.1f;
     SetObjectRotation(newObj, xForce, yForce, zForce);
    }

    Wait(100);
   }
  }


I am not even sure if I am on the right track, so any help would be greatly appreciated. Thank you! biggrin.gif

nixolas1
  • nixolas1

    Creator/Destroyer

  • Members
  • Joined: 23 Dec 2008
  • None

#2

Posted 29 June 2013 - 07:04 PM Edited by nixolas1, 29 June 2013 - 07:07 PM.

Heh, well, it has to do with the way the game works. You cant set the normal ObjectRotation after you have attached it to a ped ( or anything else).

You will have to set the attach rotation to rotate it. AttachObjectToPed(newObj,GetPlayerPed(), BONE_HEAD, 0, 0, 0, rz, ry, rz, false);
change rx,ry,rz to change rotations. the value is given in radians.

For normal rotation the object must be unattached. then you can use SetObjectRotation, or (maybe) rotateObject.

To get some nice coordinates use GetOffsetFromCharInWorldCoordinates(ped, 0, 2,0, &x,&y,&z); which is 2m in front of ped.

And a simpler/more effective key event check is if (GetKeyState( VK_MULTIPLY ) < 0){

EDIT: rotateObject should in theory add rotation to the object from its current rotation values, while setObjectRotation resets the rotation to 0+yourValue.

HadesOfGTA
  • HadesOfGTA

    Player Hater

  • Members
  • Joined: 25 Jun 2013

#3

Posted 01 July 2013 - 12:49 AM

Sweet! Thank you for the response nixolas1 biggrin.gif

I am now able to rotate in-game biggrin.gif

HadesOfGTA
  • HadesOfGTA

    Player Hater

  • Members
  • Joined: 25 Jun 2013

#4

Posted 02 July 2013 - 05:17 AM

Nixolas1, Thank you again for your help in helping me to determine how to do this. I would like to post my code that I have developed just in case others come across this issue.

What this code does: Rotates and Moves an ATTACHED-TO-PLAYER object. Attach the object to the player, then pass the object and the ePedBone to the function. Please also note this function is best used offline as I didn't write in any checks when exiting this function in-game (i.e. deleting the object). Please also note, you will have to replace the hotkeys with functions that work for you. Simply use nixolas1's suggestion of GetKeyState(VK_KEY) < 0 accordingly.

CODE

void CustomFiberThread::AttachedObjectRotationAndMovementTest(Scripting::Object yourObject, Scripting::ePedBone myBone)
{
/*****************************************************************
CONTROLS:

NUMPAD*     Change Rotation/Movement modes

ARROW LEFT    Increase x/xrot
CTRL + ARROW LEFT  Decrease x/xrot

ARROW DOWN    Increase y/yrot
CTRL + ARROW DOWN  Decrease y/yrot

ARROW RIGHT    Increase z/zrot
CTRL + ARROW RIGHT  Decrease z/zrot

NUMPAD+     Display current coordinates (x,y,z)
NUMPAD-     Display current rotation (pitch,yaw,roll)
*****************************************************************/

f32 x=0.0f,y=0.0f,z=0.0f,xrot=0.0f,yrot=0.0f,zrot=0.0f;
bool rotMode = false;
PrintStringWithLiteralStringNow("STRING", "Starting in Movement Mode", 1000, true);
while(true) {
 Wait(100);
 if(IsBitSet(GetKeyState(HH_LEFT), 15)) {
  while(IsObjectAttached(yourObject)) {
   DetachObject(yourObject, true);
  }
 
  if(IsBitSet(GetKeyState(HH_CONTROL), 15))
   if(!rotMode)
    x-=0.05f;
   else
    xrot-=0.05f;
  else
   if(!rotMode)
    x+=0.05f;
   else
    xrot+=0.05f;

  AttachObjectToPed(yourObject, GetPlayerPed(), myBone, x, y, z, xrot, yrot, zrot, false);
 }
 else if(IsBitSet(GetKeyState(HH_DOWN), 15)) {
  while(IsObjectAttached(yourObject)) {
   DetachObject(yourObject, true);
  }
 
  if(IsBitSet(GetKeyState(HH_CONTROL), 15))
   if(!rotMode)
    y-=0.05f;
   else
    yrot-=0.05f;
  else
   if(!rotMode)
    y+=0.05f;
   else
    yrot+=0.05f;

  AttachObjectToPed(yourObject, GetPlayerPed(), myBone, x, y, z, xrot, yrot, zrot, false);
 }
 else if(IsBitSet(GetKeyState(HH_RIGHT), 15)) {
  while(IsObjectAttached(yourObject)) {
   DetachObject(yourObject, true);
  }
 
  if(IsBitSet(GetKeyState(HH_CONTROL), 15))
   if(!rotMode)
    z-=0.05f;
   else
    zrot-=0.05f;
  else
   if(!rotMode)
    z+=0.05f;
   else
    zrot+=0.05f;

  AttachObjectToPed(yourObject, GetPlayerPed(), myBone, x, y, z, xrot, yrot, zrot, false);
 }
 else if(IsBitSet(GetKeyState(HH_ADD), 15)) {
  char RotMessage[300];
  sprintf_s(RotMessage, 300, "x: %f y: %f z: %f", x, y, z);
  PrintStringWithLiteralStringNow("STRING", RotMessage, 1000, true);
 }else if(IsBitSet(GetKeyState(HH_SUBTRACT), 15)) {
  char RotMessage[300];
  sprintf_s(RotMessage, 300, "xrot: %f yrot: %f zrot: %f", xrot, yrot, zrot);
  PrintStringWithLiteralStringNow("STRING", RotMessage, 1000, true);
 }else if(IsBitSet(GetKeyState(HH_MULTIPLY), 15)) {
  rotMode = !rotMode;
  PrintStringWithLiteralStringNow("STRING", (rotMode==false ? "Movement Mode" : "Rotation Mode"), 1000, true);
 }else if(IsBitSet(GetKeyState(HH_DIVIDE), 15)) {
  PrintStringWithLiteralStringNow("STRING", "Exiting rotation mode", 1000, true);
  DeleteObject(&yourObject);
 }
}
}


If you find that when you display the coordinates that the time displayed isn't long enough for you, simply increase the 1000 to a number that suites you. Time is in milliseconds. 1second = 1000 milliseconds. 5 seconds = 5000 milliseconds.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users