Jump to content

math for moving ball (carball)


MisterQuestions

Recommended Posts

Hey everybody i was trying to make some carball thing... but i don't really know how to make when player hits the ball give the correct direction..

Any idea? Here is what i have...

Server-Side (At client side i just send current vehicle speed).

function onClientInteractedWithBall(hittedBall,x,y,z) 
    if isElement(hittedBall) then 
        setElementVelocity(hittedBall,x*2,y*2,z*2 + 0.2) 
    end 
end 
addEvent("ball:onServerSync",true) 
addEventHandler("ball:onServerSync",root,onClientInteractedWithBall) 

Link to comment
processLineOfSight returns some the normal of a surface, this might help.

o.O This couldn't help for getting the direction for the ball, what i need is when the player hits the ball, move it with the direction too, with my current function i just move ball but doesn't set the ball to a direction.. (i mean the direction is setted depending on how the player hitted with the car).

Link to comment
  • Moderators

When you subtract(-) the player position from the ball position, you get the oposide direction vector.

When you devide(/) each vector(x, y, z) by the distance between ball and player position, you get a relative direction vector.

Which you can multiply(*) by a value of choice to pick the Next moving distance.

And the last step is to add(+) the moving distance to the position of the ball. So you know where it is going to end.

Link to comment
When you subtract(-) the player position from the ball position, you get the oposide direction vector.

When you devide(/) each vector(x, y, z) by the distance between ball and player position, you get a relative direction vector.

Which you can multiply(*) by a value of choice to pick the Next moving distance.

And the last step is to add(+) the moving distance to the position of the ball. So you know where it is going to end.

Wow really awesome awnser and thanks, can you give me an example with it too please?

Link to comment
Wow really awesome awnser and thanks, can you give me an example with it too please?

If you use getElementPosition you'll get the element's 3D Vector(X, Y and Z). If you do this for both the vehicle, and the ball - you have both their vectors.

I would highly, highly recommend watching some of the vector tutorials over at Khan Academy. Absolutely brilliant videos. You can start here.

There's quite a few videos available, and it might seem very confusing at first. But it's definitely worth putting effort into as you can do some really cool stuff with this. For example, advanced physics ;)

Edit:

Here's an example of a little test I made;

Link to comment

Something like this if you just want to get started and might want to learn about vectors more later:

-- "ball" is the ball element 
-- "vehicle" is the vehicle element that collided with the ball 
  
local x, y, z = getElementPosition(ball) 
local x2, y2, z2 = getElementPosition(vehicle) 
local velX, velY, velZ = getElementVelocity(vehicle) 
local vel = math.sqrt(velX*velX + velY*velY + velZ*velZ) -- equal to getDistanceBetweenPoints3D(0, 0, 0, velX, velY, velZ) 
  
local dirX, dirY, dirZ = x - x2, y - y2, z - z2 
local dist = math.sqrt(dirX*dirX + dirY*dirY + dirZ*dirZ) -- equal to getDistanceBetweenPoints3D(x, y, z, x2, y2, z2) 
dirX, dirY, dirZ = dirX / dist, dirY / dist, dirZ / dist 
  
-- To change velocity of the ball now, i guess you might want to do: 
-- setElementVelocity(ball, dirX * vel, dirY * vel, dirZ * vel) 

Keep in mind that this will set the velocity of the ball to the velocity of the vehicle in the direction that the ball is compared to the center of the vehicle, and that's why you might want to instead get the point of collision in onClientVehicleCollision and use that point together with vehicle position and you could then add stuff like spin to the ball as well then, but that is a bit more advanced.

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