^ character, what is that in Monkey?
Monkey Forums/Monkey Beginners/^ character, what is that in Monkey?
| ||
| 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
|
| ||
| It's the bit-wise XOR operator. Just Google "C++ bit-wise XOR" This "~" appears to be the Monkey equivalent. Without the quotes. |
| ||
| Thank you for the info :) |
| ||
| 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). |
| ||
| 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 |
| ||
| 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. |
| ||
| 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? :-/ |
| ||
| If I'm reading correctly ^ is c++ and ~ is monkey :) |
| ||
| 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. |
| ||
| 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... |
| ||
| 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 :) |
| ||
| No. It doesn't work. If you increase the size of the rectangle it returns inaccurate results. |
| ||
| 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. |
| ||
| 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. |
| ||
| it expects center handles: |