Jump to content

[SNIPPET] Faking dynamic objects (Bomb drop)


Ace_Gambit

Recommended Posts

Here's a code snippet that shows how to fake GTA world physics on objects. This is one of the earlier versions that I am using in my game mode. It demonstrates the faking of a dropping bomb with correct velocity over x, y and z axis. Unlike most falling bomb scripts this does not use the moveObject function which in turn allows it to follow more accurate trajectories (curved falling direction). It also does not take the altitude/distance to ground parameter into account to determine when to detonate. This sample tries to simulate a bomb that detonates when:

1) Velocity over z becomes greater than or equal to zero (bomb hit or bounced off of object)

2) Velocity over x, y, and z is equal to zero (bomb stopped moving)

The benefit of this method is that the bomb is able to interact with custom world objects. And it creates a cool effect when the bomb is bouncing off before exploding.

How does it work? It's very easy. Objects in MTA DM are static by default and you can not set its velocity. Vehicles however are perfectly capable of handling GTA's world physics. To fake the physics of my bomb I've attached a vehicle to the bomb model :mrgreen: . And it works ^ lol.

Be aware that this code snippet is a rather dirty solution to overcome the missing feature of making objects dynamic. DP3 will probably offer full support for dynamic objects (altering velocity and such) rendering this sample useless. But it may be useful to people who want to create dynamic objects in a different way than regular script have done so far. Please do not ask me to make this a resource because I am too lazy to do it :P .

Anyway, this is what it could look like:

And this is the code snippet

server.lua

  
local root = getRootElement() 
local bomber = false 
  
function callbackspawn() 
  if (spawnPlayer(source, 0, 0, 5)) then 
    fadeCamera(source, true) 
    if (bomber == false) then 
      bomber = createVehicle(476, 5, 5, 5, 0, 0, 360) 
      setVehicleColor(bomber, 44, 44, 44, 44) 
    end 
    setPlayerSkin(source, 61) 
  end 
end 
  
addEventHandler("onPlayerJoin", root, callbackspawn) 
  

client.lua

  
local root = getRootElement() 
local player = getLocalPlayer() 
local reloadspot = false 
local bombs = {} 
local objects = {} 
local armedtimer = false 
  
function inrustler() 
  if (isPlayerInVehicle(player)) then 
    return (getVehicleID(getPlayerOccupiedVehicle(player)) == 476) 
  end 
  return false 
end 
  
function callbackreload() 
  if (getElementType(source) == "player" and inrustler(source)) then 
    loadbomb() 
  end 
end 
  
function callbackspawn() 
  reloadspot = createColTube(30, 20, 2.0, 6.0, 100) 
  createMarker(30, 20, 2.0, "checkpoint", 12.0, 17, 80, 31, 75) 
  createBlip(30, 20, 0, 5) 
  -- some random world settings 
  -- just forget it if you don't care 
  resetSkyGradient() 
  setWeather(10) 
  setTime(12, 0) 
end 
  
-- arm the bomb 
function armbomb(playerName, armed) 
  local vX, vY, vZ = 0, 0, 0 
  local x, y, z = 0, 0, 0 
  if (not armed) then 
    playSoundFrontEnd(42) 
    detachElementFromElement(objects[playerName]) 
    setTimer(setElementCollisionsEnabled, 500, 1, bombs[playerName], true) 
    setTimer(armbomb, 2500, 1, playerName, true) 
    outputChatBox("* Bomb is armed") 
  else 
    -- velocity check over z-axis 
    vX, vY, vZ = getElementVelocity(objects[playerName]) 
    -- check if bomb bounced off (vZ becomes positive) or stopped moving 
    if ((vZ > 0) or (vX == 0 and vY == 0 and vZ == 0)) then 
      x, y, z = getElementPosition(bombs[playerName]) 
      -- detonate bomb 
      destroyElement(bombs[playerName]) 
      blowVehicle(objects[playerName], true) 
      destroyElement(objects[playerName]) 
      -- create explosion at bomb position 
      createExplosion(x, y, z, 10, true, 1.0, true) 
      -- create mini-flares 
      createProjectile(player, 21, x, y, z, 1.0, nil, 0, 0, 0, -(math.random(2, 5) / 10), math.random(2, 5) / 10, math.random(1, 5) / 10) 
      createProjectile(player, 21, x, y, z, 1.0, nil, 0, 0, 0, math.random(2, 5) / 10, -(math.random(2, 5) / 10), math.random(1, 5) / 10) 
      createProjectile(player, 21, x, y, z, 1.0, nil, 0, 0, 0, math.random(2, 5) / 10, -(math.random(2, 5) / 10), math.random(1, 5) / 10) 
      createProjectile(player, 21, x, y, z, 1.0, nil, 0, 0, 0, -(math.random(2, 5) / 10), math.random(2, 5) / 10, math.random(1, 5) / 10) 
    else 
      armedtimer = setTimer(armbomb, 50, 1, playerName, true) 
    end 
  end 
end 
  
-- create bomb model 
function createbomb() 
  local bomb = bombs[getPlayerName(player)] 
  if (bomb ~= nil and bomb ~= false) then 
    -- destroy object 
    destroyElement(bomb) 
  end 
  -- create actual bomb model 
  bombs[getPlayerName(player)] = createObject(3786, 0, 0, 0) 
  setElementCollisionsEnabled(bombs[getPlayerName(player)], false) 
end 
  
-- drop the bomb (not armed) 
function dropbomb() 
  local vehicle = false 
  local object = objects[getPlayerName(player)] 
  if (inrustler(player) and #getAttachedElements(getPlayerOccupiedVehicle(player)) > 0) then 
    createbomb() 
    if (object ~= nil and object ~= false) then 
      -- destroy object 
      destroyElement(object) 
    end 
    -- create rc vehicle 
    objects[getPlayerName(player)] = createVehicle(441, 0, 0, 0) 
    setElementAlpha(objects[getPlayerName(player)], 0) 
    vehicle = getPlayerOccupiedVehicle(player) 
    if (vehicle ~= false and getVehicleController(vehicle) == player) then 
      attachElementToElement(bombs[getPlayerName(player)], objects[getPlayerName(player)], 0, 0, 0, 0, 0, -90) 
      -- attach to player vehicle 
      attachElementToElement(objects[getPlayerName(player)], vehicle, 0, 0, -1.2, 0, 0, 0) 
      setTimer(armbomb, 500, 1, getPlayerName(player), false) 
    end 
  end 
end 
  
-- attach bomb to airplane 
function loadbomb() 
  if (inrustler(player) and #getAttachedElements(getPlayerOccupiedVehicle(player)) == 0) then 
    playSoundFrontEnd(46) 
    if (armedtimer ~= false) then 
      killTimer(armedtimer) 
    end 
    createbomb() 
    attachElementToElement(bombs[getPlayerName(player)], getPlayerOccupiedVehicle(player), 0, 0, -1.2, 0, 0, -90) 
    outputChatBox("* Bomb is attached") 
    outputChatBox("* Press LSHIFT to drop the bomb on target") 
  end 
end 
  
function callbackKeyFunc(key, keyState) 
  if (key == "lshift") then 
    dropbomb() 
  end 
end 
  
addEventHandler("onClientPlayerSpawn", player, callbackspawn) 
addEventHandler("onClientElementColShapeHit", root, callbackreload) 
bindKey("lshift", "down", callbackKeyFunc) 
  

Use this code if you want but don't keep scripts to yourself. Give other people the opportunity to learn as well.

Link to comment

this looks really cool, but if we need a lot of dynamic objects using cars might be too heavy on the server. how about the car wheels and doors that appear after a car blows up? those have physics and when you touch them they'll move.

Link to comment

Yes but this sample is not very practical to begin with. I just wanted to show that you can create pretty neat stuff if you think outside the box once in a while. My advice is to play around with this if you want but wait to implement object dynamics in your script until they have released DP3.

Link to comment
  • 2 weeks later...
hello

i hope u dont mind me asking but is there a way to set the bomb on any plane that flys through the checkpoint

and not just a rustler

thanx for any reply even to say u dont want me messin with your script :D

I don't mind at all. You can change it by replacing the inRustler function with something that checks if the player is in an airplane. Depending on the airplane you should also adjust the z offset value of the attached bomb in order to make appear correctly. I know I said earlier that I wasn't going to make this a resource. But maybe I will script a resource that accepts all kind of airplanes and upload it to the community base (maybe even today).

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