Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / math help request!!

Author
Message
Alduce
22
Years of Service
User Offline
Joined: 26th Oct 2002
Location: Rama spaceship
Posted: 9th Nov 2013 14:18 Edited at: 9th Nov 2013 14:21
Hi guys!
I have a code to make my sun raising on the sky and back down over the horizont.
With the flow of the time so I have my light and shadow simulating the sun travelling on the sky.

Here my code:


I would to obtain a linear movement of my sun.. but at this moment I have just my sun moving every 30 minutes...

pls help with a good FOR loop!!
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 9th Nov 2013 18:25 Edited at: 9th Nov 2013 18:53
Your sun appears to just be moving up and down. Is this an artistic effect you want to keep or would you like a more realistic elliptical motion?

Staying with linear movement, here's how I'd approach it. I looked at your code and noted its behaviour in simple terms:

start sunY at -12000 at midnight
raise sunY incrementally until it peaks at 12000 at noon
lower sunY incrementally until it hits -12000 at midnight
repeat cycle


Using hours and minutes is a bit awkward so I'm going to use degrees; degrees are perfect for anything cyclical, we have a bunch of maths and commands for using degrees in this way.

Now let's begin writing code. Firstly, think about the nature of this code: it's a function, the sun rises and falls with predictable regularity, so no if statements. We should be in control of the sun's movement and tell it where to be at any moment.

sunY= ?

Right, that's the hard part... now we just need to figure out what goes on the other side of that equals sign. (I position the equals like that to remind myself it is assigning a value, when I am using equals to compare values I put it in the middle "a = b" or off to the other side "a =b")

If you don't know about sine and cosine they learn about them because they are very useful for producing natural-looking motion. All you need to know for now is that by plotting the motion of our sun on a sine curve it will slow down as it rises until reaching a peak, then it will accelerate back down again, trust me it will look cool! Plotting the sun's height on a sine curve is easy because the value of sine cycles between 1 and -1, so all we do is multiply that by the maximum height/depth we want (in your case 12000).




Asides
Why I renamed y_sun to sunY: I feel it is easier to read this way around, since the "Y" is of the "sun", if you made a type for the sun's data it would look very similar "sun.y". I don't use underscores because I think they look messy. Some people use certain symbols for specific things, such as, starting every label name with an underscore eg, "gosub _cheeseburger".


Formerly OBese87.
Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 9th Nov 2013 18:34
The way to do this is using linear interpolation (lerp) or spline interpolation.

If you google it you can find a ton of examples on how to implement this.

Lerp is simply ¬
result = (start + percent*(end - start))

What you'd do is setup interpolation points with different ranges and lerp between them.

"Get in the Van!" - Van B
Alduce
22
Years of Service
User Offline
Joined: 26th Oct 2002
Location: Rama spaceship
Posted: 9th Nov 2013 18:49 Edited at: 9th Nov 2013 18:49
Thank you Libervurto, yes I just want to have my sun going up and down (changing light and backdrop color).

I was near to use proportions to make this but unlucky my math level is really low to make this quick so I decided to ask in the forum

your code is cool but I need to fit it on my hour/minutes code. But I think I can do it


@Sasuke:
Really interesting! This lerp math example seems pretty good!

Thanks guys!
Derek Darkly
13
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 9th Nov 2013 22:51 Edited at: 9th Nov 2013 23:04
Here you go:



D.D.
Alduce
22
Years of Service
User Offline
Joined: 26th Oct 2002
Location: Rama spaceship
Posted: 9th Nov 2013 23:19
Derek Darkly thank you! really little and working code too!

Thanks a lot to you all guys!
Derek Darkly
13
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 10th Nov 2013 01:19
What? It actually worked?! LoL

D.D.
Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 10th Nov 2013 01:44
The problem with the method presented is it's hardcoded, if you wanted something more dynamic then you'd be stuck again, hence why I suggested interpolation methods.

This is basically animation, and all animation is key points that interpolate between each other.

"Get in the Van!" - Van B
wattywatts
15
Years of Service
User Offline
Joined: 25th May 2009
Location: Michigan
Posted: 10th Nov 2013 05:54
I was just thinking it would be easier to make an animated sun that rotates your world and loop it, until I remembered that the object position wouldn't change so updating the position of your light wouldn't work. Darn..

http://www.indiedb.com/games/max-vs-mars
chafari
Valued Member
19
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 10th Nov 2013 10:46
Quote: "I was just thinking it would be easier to make an animated sun that rotates your world"


Good idea wattywatts




I'm not a grumpy grandpa
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 10th Nov 2013 20:30 Edited at: 10th Nov 2013 20:51
Quote: "your code is cool but I need to fit it on my hour/minutes code. But I think I can do it"

It might not be totally obvious how to convert time to degrees but it's actually very intuitive.
A full day is 360°, so to turn this into a clock we simply slice that circle into hours/minutes/seconds.
From what I gather your smallest unit of time is one minute, and with a little calculating we find that there are 1440 minutes per day, so...


This could also be written:
but I wanted to reduce the amount of calculations inside the FOR loop. (When you have loops the time it takes to do calculations really stacks up and can slow down the program a lot.)

CAVEAT: I have not tested my code.


Formerly OBese87.

Login to post a reply

Server time is: 2025-05-15 14:37:06
Your offset time is: 2025-05-15 14:37:06