From 3b453369296377826c20a6ed99c89ac052c4044c Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 30 Dec 2015 13:34:00 +0100 Subject: Reviewed function: CheckCollisionCircleRec() --- src/shapes.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'src/shapes.c') diff --git a/src/shapes.c b/src/shapes.c index 071fa63c..a4761536 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -391,16 +391,24 @@ bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, floa } // Check collision between circle and rectangle +// NOTE: Reviewed version to take into account corner limit case bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec) { - bool collision = false; + int recCenterX = rec.x + rec.width/2; + int recCenterY = rec.y + rec.height/2; + + float dx = abs(center.x - recCenterX); + float dy = abs(center.y - recCenterY); - float dx = fabs((rec.x + rec.width/2) - center.x); - float dy = fabs((rec.y + rec.height/2) - center.y); + if (dx > (rec.width/2 + radius)) { return false; } + if (dy > (rec.height/2 + radius)) { return false; } - if ((dx <= (rec.width/2 + radius)) && (dy <= (rec.height/2 + radius))) collision = true; + if (dx <= (rec.width/2)) { return true; } + if (dy <= (rec.height/2)) { return true; } - return collision; + float cornerDistanceSq = pow(dx - rec.width/2, 2) + pow(dy - rec.height/2, 2); + + return (cornerDistanceSq <= (radius*radius)); } // Get collision rectangle for two rectangles collision -- cgit v1.2.3 From 08da91047e8a2c593c3bc40b31c4796ae6cbb26d Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 23 Jan 2016 13:22:13 +0100 Subject: Some code tweaks --- src/audio.c | 17 +++++------------ src/core.c | 2 ++ src/models.c | 2 +- src/raylib.h | 2 +- src/shapes.c | 4 ++-- 5 files changed, 11 insertions(+), 16 deletions(-) (limited to 'src/shapes.c') diff --git a/src/audio.c b/src/audio.c index 6313c9dc..e40fdd41 100644 --- a/src/audio.c +++ b/src/audio.c @@ -166,15 +166,8 @@ void CloseAudioDevice(void) // Load sound to memory Sound LoadSound(char *fileName) { - Sound sound; - Wave wave; - - // Init some default values for wave... - wave.data = NULL; - wave.dataSize = 0; - wave.sampleRate = 0; - wave.bitsPerSample = 0; - wave.channels = 0; + Sound sound = { 0 }; + Wave wave = { 0 }; // NOTE: The entire file is loaded to memory to play it all at once (no-streaming) @@ -236,7 +229,7 @@ Sound LoadSound(char *fileName) // Load sound from wave data Sound LoadSoundFromWave(Wave wave) { - Sound sound; + Sound sound = { 0 }; if (wave.data != NULL) { @@ -290,7 +283,7 @@ Sound LoadSoundFromWave(Wave wave) Sound LoadSoundFromRES(const char *rresName, int resId) { // NOTE: rresName could be directly a char array with all the data!!! --> TODO - Sound sound; + Sound sound = { 0 }; #if defined(AUDIO_STANDALONE) TraceLog(WARNING, "Sound loading from rRES resource file not supported on standalone mode"); @@ -791,7 +784,7 @@ static Wave LoadWAV(const char *fileName) WaveFormat waveFormat; WaveData waveData; - Wave wave; + Wave wave = { 0 }; FILE *wavFile; wavFile = fopen(fileName, "rb"); diff --git a/src/core.c b/src/core.c index 9b068300..7a5de04e 100644 --- a/src/core.c +++ b/src/core.c @@ -1230,7 +1230,9 @@ Vector2 GetTouchPosition(void) return position; } +#endif +#if defined(PLATFORM_ANDROID) // Detect if a button has been pressed once bool IsButtonPressed(int button) { diff --git a/src/models.c b/src/models.c index 80d9a13a..e90f455a 100644 --- a/src/models.c +++ b/src/models.c @@ -557,7 +557,7 @@ void DrawGizmo(Vector3 position) // Load a 3d model (from file) Model LoadModel(const char *fileName) { - Model model; + Model model = { 0 }; Mesh mesh = { 0 }; // NOTE: Initialize default data for model in case loading fails, maybe a cube? diff --git a/src/raylib.h b/src/raylib.h index 49a320d4..73200556 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -18,7 +18,7 @@ * * Used external libs: * GLFW3 (www.glfw.org) for window/context management and input -* GLAD for OpenGL extensions loading (3.3 Core profile) +* GLAD for OpenGL extensions loading (3.3 Core profile, only PLATFORM_DESKTOP) * stb_image (Sean Barret) for images loading (JPEG, PNG, BMP, TGA, PSD, GIF, HDR, PIC) * stb_image_write (Sean Barret) for image writting (PNG) * stb_vorbis (Sean Barret) for ogg audio loading diff --git a/src/shapes.c b/src/shapes.c index a4761536..3b4be071 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -397,8 +397,8 @@ bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec) int recCenterX = rec.x + rec.width/2; int recCenterY = rec.y + rec.height/2; - float dx = abs(center.x - recCenterX); - float dy = abs(center.y - recCenterY); + float dx = fabs(center.x - recCenterX); + float dy = fabs(center.y - recCenterY); if (dx > (rec.width/2 + radius)) { return false; } if (dy > (rec.height/2 + radius)) { return false; } -- cgit v1.2.3 From 9cbfcbb8200252d846f447438a33cf47d32ffaf9 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 11 Feb 2016 14:56:00 +0100 Subject: Add a note about an issue --- src/shapes.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/shapes.c') diff --git a/src/shapes.c b/src/shapes.c index 3b4be071..65e3621b 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -412,6 +412,7 @@ bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec) } // Get collision rectangle for two rectangles collision +// TODO: Depending on rec1 and rec2 order, it fails -> Review! Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) { Rectangle retRec = { 0, 0, 0, 0 }; -- cgit v1.2.3