#include "game.h" ssize_t WaterTank::is_inside(vec3 pos, float margin) { size_t highest_instance; float highest_height = -1000000; for(size_t i = 0; i < models.size(); i++){ /* How close is the floor panel represented by m? */ /* This really doesn't work when rotating! */ vec3 size = positions[i].size; vec3 cmpos = positions[i].location; /* Is it inside? */ float squared_distance = powf(pos.x - cmpos.x, 2) + powf(pos.z - cmpos.z, 2); float squared_radius = powf(size.x, 2) + powf(size.z, 2); if(fabs(cmpos.y - pos.y) < size.y + margin && squared_distance < squared_radius){ if(highest_height < (cmpos.y + size.y)){ highest_height = cmpos.y + size.y; highest_instance = i; } } } if(highest_height == -1000000) return -1; else return highest_instance; } float WaterTank::get_topbottom(size_t instance, bool bottom){ if(bottom) return positions[instance].location.y - positions[instance].size.y; else return positions[instance].location.y + positions[instance].size.y; } void WaterTank::trigger_action(size_t instance, int action){ switch(action){ case PLAYER_ON_OBJECT: positions[instance].oncount = 5; break; } } void WaterTank::movement(float time){ for(size_t i = 0; i < models.size(); i++){ float angle_change = positions[i].speed * time/5000.0f; positions[i].angle += angle_change; mat4 nm = mat4(1.0); nm = translate(nm, positions[i].location); nm = scale(nm, positions[i].size); nm = rotate(nm, positions[i].angle, vec3(0, 1, 0)); models[i] = nm; if(positions[i].oncount){ positions[i].oncount--; g.heading -= angle_change; double dx = g.player_position.x - positions[i].location.x; double dz = g.player_position.z - positions[i].location.z; double radius = sqrt(pow(dx, 2) + pow(dz, 2)); double angle = atan2(dz, dx); angle -= angle_change; float new_x = positions[i].location.x + radius * cos(angle); float new_z = positions[i].location.z + radius * sin(angle); g.player_position.x = new_x; g.player_position.z = new_z; } } }