Jump to content

how to calculate this?

John Smith

Recommended Posts

hi i need somebody to help me with this


how can i calculate the requried velocity x,y,z arguments to throw player away from the colshape in exact opposite direction?

like if i was coming from left side to the colshape it would throw me on left side, but behind and not front.

please help me if you know how to calculate this direction for setElementVelocity

Link to comment
  • MTA Team

No compilation errors, but I am not sure if it works.

In one sentence: It takes the angle from the player to the colshape, turns it around (negative) and then applies the velocity (before the impact) to push the player away.

function handleShapeHit(contactElement, matchingDimension) 
    if not matchingDimension then 
    if getElementType(contactElement) ~= "player" then 
    local x, y = getElementPosition(source) 
    local px, py, pz = getElementPosition(contactElement) 
    local vx, vy, vz = getElementVelocity(contactElement) 
    local speed = math.sqrt(math.pow(vx, 2) + math.pow(vy, 2) + math.pow(vz, 2)) + 0.25 
    local angle = -math.atan2(px - x, py - y) 
    setElementVelocity(contactElement, speed * math.cos(angle), speed * math.sin(angle), vz + 0.1) 
local col = createColRectangle(-150, -150, 300, 300) 
addEventHandler("onColShapeHit", col, handleShapeHit) 

Link to comment
  • 1 month later...

Your example wasn't working in correct angle

anyway i have tried this thing with a marker(it can be done with colshape too if prefered)

and it's actually very easy to achieve what i tried to do

here's it if anyone needs it

local marker = createMarker(0,0,4,"corona",3,255,0,0,255) 
function ion(hitElement) 
   if source == marker then 
      local x,y,z = getElementVelocity(hitElement) 
      setElementVelocity(hitElement,-x,-y,z+0.5) -- throw element away from marker in opposite direction 

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