Compass & Qibla Finder

Users will be able to use a compass with the Kaaba marked on one side, allowing them to both use the compass and see the direction of the Kaaba. I asked artificial intelligence how to do this, and it gave me a bunch of formulas to calculate. Is it really that difficult to calculate or is there a simpler way to do this within Thunkable? I need to understand the logic behind this topic; artificial intelligence couldn’t help me understand it. (Kaaba Lat: 21.42253659824817 Long: 39.82620321214199) I did with polyline but didn’t on compass.

Thank you.

Qibla Compass Bearing Formula (Shortest Version)

To calculate the Qibla direction (bearing) from any location to the Kaaba:

1. Convert degrees to radians

φ1 = userLatitude * PI / 180
λ1 = userLongitude * PI / 180
φ2 = 21.4225 * PI / 180      // Kaaba latitude
λ2 = 39.8262 * PI / 180      // Kaaba longitude

2. Delta longitude

Δλ = λ2 - λ1

3. Bearing formula

bearing_deg = ( atan2( sin(Δλ) * cos(φ2),
                       cos(φ1) * sin(φ2) - sin(φ1) * cos(φ2) * cos(Δλ) )
                * 180 / PI + 360 ) mod 360

  • atan2(y, x) in Thunkable takes two inputs.

    • y = sin(Δλ) * cos(φ2)

    • x = cos(φ1) * sin(φ2) - sin(φ1) * cos(φ2) * cos(Δλ)

This gives you the bearing from the user’s current location to the Kaaba, in degrees from North (0 = North, 90 = East, 180 = South, 270 = West).

4. Relative angle with device heading

Use Thunkable’s Magnetometer Heading (0–360° relative to North):

relativeAngle = (bearing_deg - deviceHeading + 360) mod 360

You can then rotate your arrow or Kaaba sprite by relativeAngle.

1 Like

so your block working fine right now?

Not yet, I’m still trying to fix everything.