Skip to content

Commit

Permalink
Lights turn on/off with rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Danilo1301 committed Nov 30, 2024
1 parent 44877cf commit d8469fe
Show file tree
Hide file tree
Showing 19 changed files with 179 additions and 12 deletions.
2 changes: 2 additions & 0 deletions GiroflexVSL/LightGroupRotateObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ struct LightGroupRotateObject {
bool rotateAlways = false;
float speed = 5.0f;
eRotateObjectAxis axis = eRotateObjectAxis::Z;
bool flipForward = false;
std::string object = "[none]";
RwMatrix* matrix = NULL;
};
2 changes: 1 addition & 1 deletion GiroflexVSL/Mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extern RpClump* (*RpClumpForAllAtomics)(RpClump* clump, RpAtomicCallBack callbac
extern RpGeometry* (*RpGeometryForAllMaterials)(RpGeometry* geometry, RpMaterialCallBack fpCallBack, void* pData);
extern char* (*GetFrameNodeName)(RwFrame* frame);

const char* Mod::m_Version = "3.8.0";
const char* Mod::m_Version = "3.9.0";

bool canTurnSirenOn = true;
bool canTurnPanelOn = true;
Expand Down
2 changes: 2 additions & 0 deletions GiroflexVSL/Point.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Point {
rotateObjectValue["object"] = rotateObject.object;
rotateObjectValue["axis"] = (int)rotateObject.axis;
rotateObjectValue["rotateAlways"] = rotateObject.rotateAlways;
rotateObjectValue["flipForward"] = rotateObject.flipForward;
value["rotateObject"] = rotateObjectValue;


Expand All @@ -58,6 +59,7 @@ class Point {
rotateObject.object = ValidateValue(rotateObjectValue["object"], rotateObject.object).asString();
rotateObject.axis = (eRotateObjectAxis)ValidateValue(rotateObjectValue["axis"], (int)rotateObject.axis).asInt();
rotateObject.rotateAlways = ValidateValue(rotateObjectValue["rotateAlways"], rotateObject.rotateAlways).asBool();
rotateObject.flipForward = ValidateValue(rotateObjectValue["flipForward"], rotateObject.flipForward).asBool();
}
}

Expand Down
86 changes: 86 additions & 0 deletions GiroflexVSL/Vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ int Vehicle::m_LightIdOffset = 1000;

int LightIdOffset = 10000;

float GetIntensityFromAngle(float angle)
{
if (angle > M_PI/2)
{
// A função é mapeada de modo que o valor de 0 a Math.PI se transforme de 0 a 1
// Ajuste o valor de "angle" para que ele esteja entre 0 e Math.PI.
float mappedValue = (angle - (float)(M_PI / 2)) / (float)(M_PI / 2);

// Converta o valor para o intervalo [0, 1], onde Math.PI / 2 é 0 e Math.PI é 1.
return std::clamp(mappedValue, 0.0f, 1.0f);
}
else
{
return 0;
}
}

Vehicle::Vehicle(int hVehicle, int modelId)
{
this->hVehicle = hVehicle;
Expand Down Expand Up @@ -387,6 +404,47 @@ void Vehicle::UpdateLightGroups(int dt)

//---------------------------------------

if(point->rotateObject.rotate && point->rotateObject.matrix != NULL)
{
//int lightId = 10000;

//RegisterTestCorona2(lightId++, coronaOffset, CRGBA(0, 255, 0), 2.0f);


auto objectMatPos = point->rotateObject.matrix->pos;
auto objectPosition = CVector(objectMatPos.x, objectMatPos.y, objectMatPos.z);
auto objectWorldPosition = TransformFromObjectSpace(pVehicle, objectPosition);

auto flipForward = point->rotateObject.flipForward;

auto forwardPosition = objectWorldPosition + TransformFromMatrixSpace(point->rotateObject.matrix, CVector(0, flipForward ? -1 : 1, 0));
auto cameraPosition = camera->m_matrix->pos;

auto vec1 = forwardPosition;

auto vec2 = objectWorldPosition;

auto vec3 = cameraPosition;

// pos Z is ignored :)
auto angle = GetAngleBetweenVectors(vec1, vec2, vec3);

float intensity = GetIntensityFromAngle(angle);

if(hVehicle == Globals::hPlayerVehicle)
{
// menuVSL->debug->visible = true;
// menuVSL->debug->AddLine("angle: " + std::to_string(angle));
// menuVSL->debug->AddLine("forward: " + CVectorToString(vec1));
// menuVSL->debug->AddLine("objectPos: " + CVectorToString(objectWorldPosition));
// menuVSL->debug->AddLine("cam: " + CVectorToString(vec3));
}

radius *= intensity;
}

//---------------------------------------

if(color.a == 0) enabled = false;

RenderCorona corona;
Expand Down Expand Up @@ -531,6 +589,34 @@ void Vehicle::RenderBefore()
RwReal angle = point->rotateObject.speed;

RwMatrixRotate(&frameAtomic->modelling, &axis, angle, rwCOMBINEPRECONCAT);

//by chatGPT

float yaw, pitch, roll;

// Verifica se a matriz está alinhada para evitar valores indefinidos
if (frameAtomic->modelling.at.y > 0.998f) { // Singularidade no polo norte
yaw = atan2f(frameAtomic->modelling.right.z, frameAtomic->modelling.right.x);
pitch = M_PI_2;
roll = 0.0f;
} else if (frameAtomic->modelling.at.y < -0.998f) { // Singularidade no polo sul
yaw = atan2f(frameAtomic->modelling.right.z, frameAtomic->modelling.right.x);
pitch = -M_PI_2;
roll = 0.0f;
} else {
yaw = atan2f(-frameAtomic->modelling.at.x, frameAtomic->modelling.at.z);
pitch = asinf(frameAtomic->modelling.at.y);
roll = atan2f(-frameAtomic->modelling.up.y, frameAtomic->modelling.right.y);
}

point->rotateObject.matrix = &frameAtomic->modelling;

if(hVehicle == Globals::hPlayerVehicle)
{
// menuVSL->debug->visible = true;
// menuVSL->debug->AddLine("roll: " + std::to_string(roll));
// menuVSL->debug->AddLine("matrix: " + std::to_string((int)point->rotateObject.matrix));
}
}
}

Expand Down
1 change: 1 addition & 0 deletions GiroflexVSL/Vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ class Vehicle {
void SetGiroflexEnabled(bool enabled, bool forceOn = false);
void ResetObjectRotation(std::string object);

//void RegisterTestCorona2(int lightId, CVector position, CRGBA color, float radius);
void RegisterTestCorona(int lightId, CVector position, CRGBA color, float radius);
};
2 changes: 1 addition & 1 deletion GiroflexVSL/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// ---------------------------------------

//MYMODCFG(net.danilo1301.giroflexVSL, GiroflexVSL, Mod::m_Version, Danilo1301) //whoops
MYMODCFG(net.danilo1301.giroflexVSL, GiroflexVSL, 3.8.0, Danilo1301)
MYMODCFG(net.danilo1301.giroflexVSL, GiroflexVSL, 3.9.0, Danilo1301)

// ---------------------------------------

Expand Down
56 changes: 56 additions & 0 deletions GiroflexVSL/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,62 @@ static double GetAngleBetweenVectors(CVector v1, CVector v2, CVector v3)
return acos((pow(v12, 2) + pow(v13, 2) - pow(v23, 2)) / (2 * (v12 * v13)));
}

static RwMatrix* CloneRwMatrix(const RwMatrix* originalMatrix)
{
// Cria uma nova matriz
RwMatrix* clonedMatrix = RwMatrixCreate();
if (!clonedMatrix)
return NULL; // Retorna NULL em caso de falha

// Copia os vetores da matriz original para a nova matriz
clonedMatrix->right = originalMatrix->right;
clonedMatrix->up = originalMatrix->up;
clonedMatrix->at = originalMatrix->at;
clonedMatrix->pos = originalMatrix->pos;

// Define o status como idêntico ao da original
clonedMatrix->flags = originalMatrix->flags;

return clonedMatrix;
}

static CVector TransformFromMatrixSpace(RwMatrix* originalMatrix, CVector pos)
{
RwMatrix* matrix = CloneRwMatrix(originalMatrix);

RwV3d forward = { matrix->up.x, matrix->up.y, matrix->up.z };
RwV3d right = { matrix->right.x, matrix->right.y, matrix->right.z };
RwV3d up = { matrix->at.x, matrix->at.y, matrix->at.z };

RwV3dNormalize(&forward, &forward);
RwV3dNormalize(&right, &right);
RwV3dNormalize(&up, &up);

RwV3d translate;
translate.x = forward.x * pos.y;
translate.y = forward.y * pos.y;
translate.z = forward.z * pos.y;

translate.x += right.x * pos.x;
translate.y += right.y * pos.x;
translate.z += right.z * pos.x;

translate.x += up.x * pos.z;
translate.y += up.y * pos.z;
translate.z += up.z * pos.z;

//RwIm3DTransform

// Translate the matrix
RwMatrixTranslate(matrix, &translate, rwCOMBINEPOSTCONCAT);

auto finalPos = CVector(matrix->pos.x, matrix->pos.y, matrix->pos.z);

RwMatrixDestroy(matrix);

return finalPos;
}

static CVector TransformFromObjectSpace(CEntity* entity, CVector pos)
{
RwMatrix* matrix = RwMatrixCreate();
Expand Down
8 changes: 4 additions & 4 deletions GiroflexVSL/windows/WindowPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ void WindowPatterns::Create(Window* parent, LightGroup* lightGroup)

UpdateOptions(lightGroup);

auto selectPatterns = window->AddButton("> Select patterns", CRGBA(0, 0, 0, 0));
auto selectPatterns = window->AddButton("> Select patterns");
selectPatterns->onClick = [window, lightGroup]() {

auto newWindow = menuVSL->AddWindowMultiOptionsString("Select patterns", window, &selectedOptions, &allOptions);
newWindow->m_OnCloseWindow = [lightGroup]() {
menuVSL->debug->AddLine("Closed, now update");
newWindow->onCloseWindow = [lightGroup]() {
menuVSL->ShowMessage("Updating lightgroup...", 1000);

lightGroup->Update();

Expand Down Expand Up @@ -72,7 +72,7 @@ void WindowPatterns::Create(Window* parent, LightGroup* lightGroup)
itemInfo->m_Text = "Selected patterns: " + std::to_string(selectedOptions.size()) + " / " + std::to_string(allOptions.size());
};

auto close = window->AddButton("> ~r~Close", CRGBA(0, 0, 0, 0));
auto close = window->AddButton("> ~r~Close");
close->onClick = [window]() {
window->SetToBeRemoved();
Menu::m_Visible = true;
Expand Down
6 changes: 4 additions & 2 deletions GiroflexVSL/windows/WindowRotate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void WindowRotate::CreatePointRotate(Window* parent, LightGroup* lightGroup, Poi
selectObjectStrVec.push_back(name);
}

auto selectObject = window->AddButton("Select object", CRGBA(255, 255, 255));
auto selectObject = window->AddButton("Select object");
selectObject->m_StringPtrAtRight = &point->rotateObject.object;
selectObject->onClick = [window, point] () {
auto newWindow = menuVSL->AddWindowOptionsString("Select object", window, &point->rotateObject.object, &selectObjectStrVec);
Expand All @@ -70,7 +70,9 @@ void WindowRotate::CreatePointRotate(Window* parent, LightGroup* lightGroup, Poi

window->AddCheckbox("Rotate always", &point->rotateObject.rotateAlways);

auto close = window->AddButton("> ~r~Close", CRGBA(0, 0, 0, 0));
window->AddCheckbox("Flip forward dir", &point->rotateObject.flipForward);

auto close = window->AddButton("> ~r~Close");
close->onClick = [window]() {
window->SetToBeRemoved();
Menu::m_Visible = true;
Expand Down
2 changes: 1 addition & 1 deletion cleo/giroflex v3/GiroflexVSL.fxt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ DGFX2 ~1~, ~1~
DGFX3 ~1~, ~1~, ~1~
DGFX4 <
DGFX5 >
DGFX6 Giroflex VSL v3.8.0 (by ~y~Danilo1301~w~)
DGFX6 Giroflex VSL v3.9.0 (by ~y~Danilo1301~w~)
DGFX7 Fechar
DGFX8 Voltar
DGFX9 Editar posicao
Expand Down
2 changes: 1 addition & 1 deletion cleo/giroflex v3/GiroflexVSL_en.fxt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ DGFX2 ~1~, ~1~
DGFX3 ~1~, ~1~, ~1~
DGFX4 <
DGFX5 >
DGFX6 Giroflex VSL v3.8.0 (by ~y~Danilo1301~w~)
DGFX6 Giroflex VSL v3.9.0 (by ~y~Danilo1301~w~)
DGFX7 Close
DGFX8 Back
DGFX9 Edit position
Expand Down
Binary file modified files/versions/3.8.0-menu/GiroflexVSL-3.8.0-menu__en.zip
Binary file not shown.
Binary file modified files/versions/3.8.0-menu/GiroflexVSL-3.8.0-menu__pt-br.zip
Binary file not shown.
Binary file added files/versions/3.9.0/GiroflexVSL-3.9.0__en.zip
Binary file not shown.
Binary file added files/versions/3.9.0/GiroflexVSL-3.9.0__pt-br.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion include/menu/IItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class IItem {
eItemType m_Type = eItemType::ITEM_NONE;
std::string m_Text = "Item";
CRGBA m_TextColor = CRGBA(255, 255, 255);
CRGBA m_BackgroundColor = CRGBA(50, 50, 50);
CRGBA m_BackgroundColor = CRGBA(0, 0, 0, 0);
bool m_FitInWindow = true;
bool m_CanBeSelected = true;
bool m_DrawTextAtCenter = false;
Expand Down
15 changes: 15 additions & 0 deletions include/menu/IMenuVSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "IDebug.h"

#define _debug menuVSL->debug
#define GetLanguageLine menuVSL->GetLanguageLineFormatted

class IMenuVSL {
public:
Expand Down Expand Up @@ -67,6 +68,20 @@ class IMenuVSL {
// using false is usefull with ConvertWorldPositionToScreenPosition
virtual void SetDrawWithFixedScale(bool enabled) = 0;

virtual IWindow* AddVector2WindowEx(IWindow* parent, CVector2D* vec, float min, float max, float addBy, std::function<void()> onChange, std::function<void()> onBack) = 0;
virtual IWindow* AddVector2Window(IWindow* parent, CVector2D* vec, float min, float max, float addBy) = 0;

virtual IWindow* AddVectorWindowEx(IWindow* parent, CVector* vec, float min, float max, float addBy, std::function<void()> onChange, std::function<void()> onBack) = 0;
virtual IWindow* AddVectorWindow(IWindow* parent, CVector* vec, float min, float max, float addBy) = 0;

/* 1.4.0 */

virtual IWindow* ShowSelectLanguageWindow(IWindow* parent = NULL) = 0;
virtual void LoadLanguagesFolder(std::string folder) = 0;

virtual std::string GetLanguageLineFormatted(std::string key, ...) = 0;

virtual void ShowMessage(std::string key, int time) = 0;

virtual void AddModCredits(std::string key) = 0;
};
2 changes: 1 addition & 1 deletion include/menu/IWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class IWindow {
int m_MaxItemsPerPage = 7;
bool m_ShowBackButton = false;
IWindow* m_Parent = NULL;
std::function<void()> m_OnCloseWindow;
std::function<void()> onCloseWindow;

/*1.0.0*/
virtual void SetPosition(CVector2D position) = 0;
Expand Down
3 changes: 3 additions & 0 deletions info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ OBS: I also found a bug that detects a lightpanel button press before the panel
[X] ADD: support to multi color on patterns
[X] ADD: sounds to the buttons (turn lights only, not turn sounds yet)

[3.9.0]
[X] ADD: lights rotate with object

-----------------------
NOTES:

Expand Down

0 comments on commit d8469fe

Please sign in to comment.