# Clamp help?

For questions about using Classic.

### » Thu Mar 04, 2010 8:28 am

Anyone have any idea why the expression:

clamp(Player.Value('Xcomp'), cos(Player.Value('Vector')) * -200, cos(Player.Value('Vector')) * 200)

is returning -200 as soon as Player.Value('Xcomp') goes below zero?

cap here if you need:
[url:36d79pxz]http://dl.dropbox.com/u/1289341/TimeTest.cap[/url:36d79pxz]

W and S to move, player looks at the mouse
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

### » Thu Mar 04, 2010 10:10 am

What happens when cos(global('Vector')) returns -1?
B
62
S
21
G
13
Posts: 1,910
Reputation: 13,685

### » Thu Mar 04, 2010 10:21 am

I'm assuming it would return 200 unfortunately
Not sure how I would do this, but still the current problem is that for some reason, as soon as Xcomp is a negative, BOTH Xcomp and Ycomp variables get messed up
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

### » Thu Mar 04, 2010 10:33 am

I think I fixed it:

clamp(Player.Value('Xcomp'), abs(cos(Player.Value('Vector')) * global('MaxSpeed')) * -1, abs(cos(Player.Value('Vector')) * global('MaxSpeed')))

Absolute value ftw
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

### » Thu Mar 04, 2010 10:39 am

I can't really look at the .cap right now, so I'll just do some guesswork.

I am assuming the Xcomp and cos(Vector) get into negative values when facing left, right? In that case, you have the following case:

clamp(Xcomp,200,-200). This is erroneous, try making sure the min and max values are correctly set.

I'd suggest something like this:

cosV = cos(Vector)
minSpeed = min(cosV*-200, cosV*200)
maxSpeed = max(cosV*-200, cosV*200)
clamp(value, minSpeed, maxSpeed)

That way, the minimum and maximum values will be correctly ordered. Let me know how it goes.
B
62
S
21
G
13
Posts: 1,910
Reputation: 13,685

### » Thu Mar 04, 2010 10:58 am

In theory that looks like it should work, I even made a little text box display those values to make sure they were the inverse of each other at all times, which they were, the minimum one was always a negative and the other, always a positive.

Yet when I move my player around using that method, he now moves MEGA twitchy, basically when Xcomp goes from a negative to a positive, he then goes max speed in the positive direction, and vice versa when going from positive to negative. It doesn't make sense!
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

Return to Help & Support using Construct Classic

### Who is online

Users browsing this forum: No registered users and 1 guest