123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|201|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> On Topic -> Keeping a value in a range?

Sun, 15 Jan 2023, 01:56
Pakz

Keeping a value in a range?


I am having a hard time finding the right words for this situation.

I have a number. This could be any number. Positive or negative. How would I keep this in a range. This a range of array slots.

0...10
15 should become 5.

0....10
-15 should become 5.

I want to texture a line. And loop the texture pixels or colors.

I tried the mod command and various snippets. But I only got a for loop working that I ended up making myself.

And is it called wrapping, or clamping?
Sun, 15 Jan 2023, 02:12
cyangames
In pesudocode would it look something like this?



-=-=-
Web / Game Dev, occasionally finishes off coding games also!
Sun, 15 Jan 2023, 02:20
Pakz
I am experimenting with some code I remember seeing in a raycasting tutorial. I think this does what it needs to. But I need to test it further.

Doing the modula with the end of the range seems to align and cycle it correctly.


Sun, 15 Jan 2023, 07:00
steve_ancell
I would just go with what rychan said, put a cap on each end.
Sun, 15 Jan 2023, 07:10
Jayenkai
Here's the internal code that JSE uses


Bit of a fucking mess, but translated to a simpler form, it's..



-=-=-
''Load, Next List!''
Sun, 15 Jan 2023, 07:20
rockford
IF a>10 THEN a=a-10
IF a<0 THEN a=a+10
Sun, 15 Jan 2023, 07:40
Jayenkai
  --v


-=-=-
''Load, Next List!''
Sun, 15 Jan 2023, 07:45
Pakz
Well, I am going with the 3dSage raycasting method. That is only 1 line. I did another test and the lines drawn follow the right pattern with either positive or negative numbers given.

And I understand it is a wrap! Not a Clamp.

Edit: thanks for the help guys!
Edit: also jays method has the actual range working with ranges starting a..b. I need a 0...length right now.
Sun, 15 Jan 2023, 07:54
Jayenkai
Yeah, that'll do the trick. It's small and optimised, and .. as long as your code is ok working with negative numbers, should be just fine.
Especially if you're using it for values in textures and such, where you need to run really fast through the numbers.

It's good to know the two pitfalls, though. 1. Negative numbers, and 2. It always wraps from 0.
As long as you keep those in mind, then if you decide to redo this in future, you'll know what might be causing issues.
Jot them down in your mental list of possible reasons for obscure bugs

-=-=-
''Load, Next List!''
Sun, 15 Jan 2023, 07:56
Pakz
Screenshot'ed the function!
Sun, 15 Jan 2023, 09:08
rockford
@Jay, depends if the values start above/below the range values. If they don't then my code would work straight off, if not then put in a FOR/NEXT loop and it would work.
Sun, 15 Jan 2023, 09:18
Jayenkai
Yep.. And I remember having done that for my "WhichWayDoIGo" function in previous iterations.

... but do that repeatedly for every pixel on a texture, or every enemy on a tile map, or every wrapped vessel on an Orret Cha wraparound level, and you're going to have epic slowdown issues!
If you know you're roughly within the range, it'd work just fine, though, and I think in a lot of cases, it's good enough to do the trick.

-=-=-
''Load, Next List!''
Sun, 15 Jan 2023, 09:23
rockford
If you're doing it on a per-pixel level, then it would be better to use a shader.
Sun, 15 Jan 2023, 10:41
Pakz
I am not sure how I would start to use shaders right now. There's ThreeJs and WebGl. But I have no real idea how to use and connect it to my canvas. I have not really spent a good long time looking to be able to use this.

In march a new ThreeJs book wil be out that might have some useful ways to get started with that. I was going to buy that book anyway.

For the current paint app I only need to texturize a brush tip. These are pretty small area's.