Calculating displacement using Accelerometer and Gyroscope (MPU6050)

Hi, I am a computer science student and working on an electronics project that requires to calculate yaw, pitch, roll and X,Y,Z displacement. I want to attach an IMU in a gun and track its orientation and displacement. I am able to get Yaw, Pitch and Roll but unfortunately cant understand how to calculate displacement or position of my gun. I am using a 10-DOF GY-87 sensor that contains MPU-6050.

I am getting values in term of g and m/s2 format. From the research that i have studied yet is that i need to get acceleration/time2 and then add all the values. But cant understand what time difference i should use.

#include "I2Cdev.h"
#include "MPU6050.h"

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
    #include "Wire.h"

// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;
//MPU6050 accelgyro(0x69); // <-- use for AD0 high

int16_t ax, ay, az;
float dx, dy, dz = 0;
int16_t gx, gy, gz;

#define LED_PIN 13
bool blinkState = false;

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
        Fastwire::setup(400, true);


    Serial.println("Initializing I2C devices...");

    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

    Serial.println("Updating internal sensor offsets...");
    pinMode(LED_PIN, OUTPUT);

void loop() {

		accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
        Serial.print(dx); Serial.print("\t");
        Serial.print(dy); Serial.print("\t");
        Serial.print(dz); Serial.print("\t\n");

    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);

I want to track object as shown in the following youtube videos.

I would be grateful to you if anyone of you can guide me in this Regards.
Thank you

P.S: Sorry for my bad english and use of non-technical terms.

You can’t use a consumer grade accelerometer to measure X,Y,Z positions with any accuracy; they are too noisy. Here is an explanation: Using Accelerometers to Estimate Position and Velocity |

However, in some situations (like tossing a ball) you can roughly estimate the final trajectory from an initial large acceleration.