# how to calculate this?

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

• 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
return
end

if getElementType(contactElement) ~= "player" then
return
end

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)
end

local col = createColRectangle(-150, -150, 300, 300)

```

• 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
end
end
```

## Create an account

Register a new account