^ character, what is that in Monkey?

Monkey Forums/Monkey Beginners/^ character, what is that in Monkey?

Pakz(Posted 2015) [#1]
I was converting code from the internet and came upon a error message with monkey called unexpected token. I wanted to make a circle to rectangle collision function.

What does this ^ character do in c++? And what do you do to replace that?

Here the collision function with the characters inside it.

Function circlerectcollide:Bool(cx:Int,cy:Int,cr:Int, rx:Int,ry:Int,rw:Int,rh:Int)

    Local circledistancex = Abs(cx - rx)
    Local circledistancey = Abs(cy - ry)

    If (circledistancex > (rw/2 + cr)) Then Return False
    If (circledistancey > (rh/2 + cr)) Then Return False

    If (circledistancex <= (rw/2)) Then Return True 
    If (circledistancey <= (rh/2)) Then Return True

    Local cornerdistancesq = (circledistancex - rw/2)^2 + (circledistancey - rh/2)^2

    Return (cornerDistancesq <= (cr^2))
End Function



CGV(Posted 2015) [#2]
It's the bit-wise XOR operator.

Just Google "C++ bit-wise XOR"

This "~" appears to be the Monkey equivalent. Without the quotes.


Pakz(Posted 2015) [#3]
Thank you for the info :)


ImmutableOctet(SKNG)(Posted 2015) [#4]
Just for the record, if you need to raise something to a specific power, the correct thing to do is to use 'Pow'. Assuming this was pseudo code, this might have intended "Pow(X, 2)" (Where X is the number in question). If it was C++ code, however, you probably should be using XOR. For a full list of operators and their meanings, click here. The '~' operator in Monkey can also be used as a unary (Single "argument"; appears on the left side: ~X, -X, etc) bitwise compliment (Also known as "bitwise not", or bit inversion). And as CGV said, '~' is also the XOR operator within binary "argument" contexts (X ~ Y).


Pakz(Posted 2015) [#5]
Xor and shl and commands like that make my head spin :) When I was young I bought a 68000 assembler book and memorised all the commands. I never understood how to use them. I found Amos and Blitz Basic then and that made things simpler.

On the same page on stackoverflow where I found the c++ code I found another c++ function that I was able to convert and it worked directly. The clamp command was in monkey so it was no problem.

The code I have now is below. I was planning on programming a part of Pang (amiga game) and needed to get collisions between balls and blocks.

Function circlerectcollide:Bool(cx:Int,cy:Int,cr:Int, rx:Int,ry:Int,rw:Int,rh:Int)
	Local closestx:Float = Clamp(cx, rx, rx+rw)
	Local closesty:Float = Clamp(cy, ry, ry+rh)
	Local distancex :Float = cx - closestx
	Local distancey:Float = cy - closesty
	Local distancesquared:Float = (distancex * distancex) + (distancey * distancey)
	Return distancesquared < (cr * cr)
End Function



Gerry Quinn(Posted 2015) [#6]
I tend to explicitly multiply x by x when I want to square it, rather than call Pow() or similar. Maybe I'm behind the times, but my instincts are it could be faster.


Steve Ancell(Posted 2015) [#7]
CGV:
It's the bit-wise XOR operator.


I XOR is a tilde symbol though isn't it?, or do both symbols (~ ^) do the same thing? :-/


EdzUp(Posted 2015) [#8]
If I'm reading correctly ^ is c++ and ~ is monkey :)


Pakz(Posted 2015) [#9]
I modified the first code I posted with the ~ character but the function did not give the right results. I have not really investigated into what was wrong since I have the other version with the Clamp command used that works without problems.


Pharmhaus(Posted 2015) [#10]
The way it looks it's probably a Pow (as suggested by ImmutableOctet) and not XOR.
Try to replace the lines with this and check if it works as expected:
Local cornerdistancesq = Pow(circledistancex - rw / 2, 2) + Pow(circledistancey - rh / 2, 2)

Return (cornerDistancesq <= Pow(cr,2))

Other potential pitfalls include misread C++ source code / porting mistakes which might not be visible immediately.
Happens to me every now and then and later you're wondering why things dont work as expected...


Pakz(Posted 2015) [#11]
This is code where the first collision function now works with the Pow command. Thing is I need to decrease the circle x and y with half the radius or it to be correct. But it works :)




Jesse(Posted 2015) [#12]
No. It doesn't work. If you increase the size of the rectangle it returns inaccurate results.


ImmutableOctet(SKNG)(Posted 2015) [#13]
Are you sure you weren't looking for 'Sqrt' instead of dividing by two, Pakz? Because that'd be the Pythagorean theorem, and likely the approximation you want.


Steve Ancell(Posted 2015) [#14]
EdzUp:
If I'm reading correctly ^ is c++ and ~ is monkey :)


I haven't touched C++ for a few years, but yeah!, I'm pretty sure you're right.


Jesse(Posted 2015) [#15]
it expects center handles: