1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #include "core/Box.h"
- #include <stdio.h>
- CoreBox* coreSetBox(CoreBox* box, const CoreVector3* size) {
- for(size_t i = 0; i < 3; i++) {
- if(size->data[i] < 0.0f) {
- box->min.data[i] = size->data[i];
- box->max.data[i] = 0.0f;
- } else {
- box->min.data[i] = 0.0f;
- box->max.data[i] = size->data[i];
- }
- }
- return box;
- }
- CoreBox* coreOffsetBox(CoreBox* box, const CoreVector3* offset) {
- coreAddSetV3(&box->min, offset);
- coreAddSetV3(&box->max, offset);
- return box;
- }
- bool coreCollidesWithBox(const CoreBox* box, const CoreBox* other) {
- return box->max.data[0] > other->min.data[0] &&
- box->min.data[0] < other->max.data[0] &&
- box->max.data[1] > other->min.data[1] &&
- box->min.data[1] < other->max.data[1] &&
- box->max.data[2] > other->min.data[2] &&
- box->min.data[2] < other->max.data[2];
- }
- CoreBox* coreExpandBox(CoreBox* box, const CoreVector3* offset) {
- for(size_t i = 0; i < 3; i++) {
- if(offset->data[i] > 0.0f) {
- box->max.data[i] += offset->data[i];
- } else {
- box->min.data[i] += offset->data[i];
- }
- }
- return box;
- }
- CoreBox* coreGrowBox(CoreBox* box, const CoreVector3* growth) {
- CoreVector3 half = {0};
- coreMulV3F(&half, growth, 0.5f);
- CoreVector3 nMin = {0};
- coreSubV3(&nMin, &box->min, &half);
- CoreVector3 nMax = {0};
- coreAddV3(&nMax, &box->max, &half);
- for(size_t i = 0; i < 3; i++) {
- if(nMin.data[i] > nMax.data[i]) {
- nMin.data[i] = (box->min.data[i] + box->max.data[i]) * 0.5f;
- nMax.data[i] = nMin.data[i];
- }
- }
- box->min = nMin;
- box->max = nMax;
- return box;
- }
- size_t coreToStringBox(const CoreBox* box, char* buffer, size_t n) {
- int w = snprintf(buffer, n, "Box([%.3f, %.3f, %.3f], [%.3f, %.3f, %.3f])",
- (double)box->min.data[0], (double)box->min.data[1],
- (double)box->min.data[2], (double)box->min.data[3],
- (double)box->min.data[4], (double)box->min.data[5]);
- return w < 0 ? 0 : (size_t)w;
- }
|