# Rotating an object based on parts below

Hi Developers.

I’m currently working on a wave system using triangles and I am struggling to come up with a less taxing method to rotate a part (later a boat) based on the triangles rotation below it.

My current idea:

Use the vertices of the triangle to draw two vectors from two vertices onto the third vertice (that makes up a triangle) and then cross-product that. Comparing the change in the x, y and z axis’. This would be taxing because it would require firing ray casts to determine which set of vertices the boat is in.

Some notes:

• The triangle parts are rendered on the client. The math behind the waves is stored on the server.

• The part is currently able to float because it uses DistrubtedTime and the math stored on the server to calculate it’s position.

I know some people on these forums have/are working on hover vehicles and I assume similar principles could apply to this.

2 Likes

You could try firing a single raycast straight down. The third value returned is the normal of the surface, which is probably what you want.

(http://wiki.roblox.com/index.php?title=API:Class/Workspace/FindPartOnRay)

5 Likes

If you are not raycasting every single frame, it shouldn’t be too expensive; so yeah, by all means, go ahead.

1 Like

If you have a 2D lookup array for the vertices, you should be able to write a function that can round your current position onto the grid, which you can then use to figure out what triangle you are on.

Given the 3 points of the triangle, you can calculate the normal vector by computing `(B-A):Cross(C-A).Unit`.

2 Likes

If you can reduce the height map for your vertices to a function `h(x, z, t)` (h=height, x/z=positions, t=time), you can take the partial derivatives dh/dx and dh/dz and get the tangent of the boat’s angle along the x and z axes at a given point in time & space. I suspect this would be very performant.

However, did you consider just doing a raycast with a whitelist (the water) and using the normal vector that’s returned from that function? Unless you have hundreds of these boats floating around, I doubt this would be a performance problem, and if you do have hundreds of these boats, you should consider changing your algorithms so you don’t recalculate each one every frame. Humans won’t notice imperfections in the ones that are farther away.

If you are unfamiliar with calculus, but your height map function is sane enough to look like math, feel free to bring it here and I can help you differentiate it.

2 Likes

Since the triangles are moving up and down on the client. Sending a ray in the client to determine the rotation of the ship for everyone on board seems quite exploitable?