Gamestudio Links
Zorro Links
Newest Posts
Executing Trades on Next Bar Open
by Zheka. 06/20/24 14:26
Lapsa's very own thread
by rki. 06/19/24 11:27
A simple game ...
by VoroneTZ. 06/18/24 10:50
Face player all the time ...
by bbn1982. 06/18/24 10:25
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (Martin_HH, TipmyPip), 1,279 guests, and 10 spiders.
Key: Admin, Global Mod, Mod
Newest Members
squik, AemStones, LucasJoshua, Baklazhan, Hanky27
19060 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Trig ? scaling integers up and down #126976
04/28/07 15:29
04/28/07 15:29
Joined: Sep 2003
Posts: 281
Arkansas\USA
raiden Offline OP
Member
raiden  Offline OP
Member

Joined: Sep 2003
Posts: 281
Arkansas\USA
Hi all, looking for a math wiz who can show me if it's possible to scale a number up and down using sin or cos.

The idea is to have a 1 line calculation for increasing and decreasing a whole number back and forth, like this:
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 etc.

I can do this with flags, here is the code:
Code:

var numCtr = 1;
function testIncrement()
{
var flagCtr = 1;
while(1)
{
numCtr += flagCtr;
if(numCtr == 10 && flagCtr == 1)
{
flagCtr = -1;
}
if(numCtr == 1 && flagCtr == -1)
{
flagCtr = 1;
}
wait(-1);
}
}on_t testIncrement;



Anyone have a calculation that does this in less code using sin or cos?

Thanks

-raiden


"It doesn't matter if we win or lose, it's how we make the game."
--------------------
Links: 3DGS for Dummies
Re: Trig ? scaling integers up and down [Re: raiden] #126977
04/28/07 15:54
04/28/07 15:54
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline
User
sheefo  Offline
User

Joined: Jul 2006
Posts: 783
London, UK
I'm no maths wiz, but I found this way works:

It will run up and down between 0 and 100 at 'speed' speed.
Code:

int value = (int)(50 * abs(sinv(total_ticks * speed)));



EDIT: It's Lite-C BTW.

Last edited by sheefo; 04/28/07 15:54.
Re: Trig ? scaling integers up and down [Re: sheefo] #126978
04/28/07 16:11
04/28/07 16:11
Joined: Mar 2006
Posts: 2,503
SC, United States
xXxGuitar511 Offline
Expert
xXxGuitar511  Offline
Expert

Joined: Mar 2006
Posts: 2,503
SC, United States
sin/cos won't give you a linear result though. It would be exponentialy curved.


xXxGuitar511
- Programmer
Re: Trig ? scaling integers up and down [Re: xXxGuitar511] #126979
04/28/07 16:46
04/28/07 16:46
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline
User
sheefo  Offline
User

Joined: Jul 2006
Posts: 783
London, UK
I use it to make my RTS buildings placement glow to show valid/invalid placement. I guess it will not cycle through each number... but it's still useful

Re: Trig ? scaling integers up and down [Re: sheefo] #126980
04/28/07 16:50
04/28/07 16:50
Joined: Sep 2003
Posts: 281
Arkansas\USA
raiden Offline OP
Member
raiden  Offline OP
Member

Joined: Sep 2003
Posts: 281
Arkansas\USA
Thank you sheefo, it is indeed useful. I haven't found a conversion to cscript for that, but I'll post it here if I do.

For now, the cscript I posted above will be fine, unless anyone knows of a cscript 1 liner that can do it.

Thanks again everyone for suggestions and comments.

-raiden


"It doesn't matter if we win or lose, it's how we make the game."
--------------------
Links: 3DGS for Dummies
Re: Trig ? scaling integers up and down [Re: sheefo] #126981
04/28/07 17:01
04/28/07 17:01
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Code:
int value = (int)(50 * abs(sinv(total_ticks * speed)));


is in c-script
Code:
var value;
value = int(50 * abs(sin(total_ticks * speed)));


you can achieve better and smoother results with
Code:
var value;
var speed = 4;
value = 50 + (50 * sin(total_ticks * speed));


This will cycle between 0 and 100. The above code cycles between 0 and 50. An even faster variant would be
Code:
var value;
value = 50 + (50 * sin(total_ticks << 2));


For a real linear change use the function cycle(var x, var a, var b);


Always learn from history, to be sure you make the same mistakes again...

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1