-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|496|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- Socoder -> Basic Basement -> inaccurate angles :( Posted : Thursday, 24 October 2013, 00:29 spinal I don't know if this is a monkey issue, or a math issue, but the following code is a bit inaccurate It's meant to be a nice snake that follows the mouse, but the angles are all wrong, bits of the body float off in weird directions... bodyX = MouseX()/scaleX bodyY = MouseY()/scaleY For t = 1 To segments Local by1:Float = bodyY[t] Local by2:Float = bodyY[t-1] Local bx1:Float = bodyX[t] Local bx2:Float = bodyX[t-1] Local rotation:Float = ATan2(by1-by2, bx1-bx2) Local pos_x = bodyX[t-1] + (bodyS*2) * Cos(rotation) Local pos_y = bodyY[t-1] + (bodyS*2) * Sin(rotation) bodyX[t] = pos_x bodyY[t] = pos_y Next t For t = segments To 0 Step -1 SetCol(0,0,0) Oval(bodyX[t],bodyY[t],bodyS,bodyS) SetCol(255,255,255) Oval(bodyX[t],bodyY[t],bodyS-2,bodyS-2) Next t --v I'm sure I havent missed a step or anything. Any clues? -=-=- Check out my excellent homepage! Posted : Thursday, 24 October 2013, 02:26 Jayenkai Had a play. Got some nice effects, and got the snake going crazy, but nothing that I thought was the effect you were after! Any further clues as to your expected style, lemmie know! Quick Tip, though.. I've found that some Android devices absolutely HATE drawing ovals. Not sure why this is, but major slowdown if you do too many. (Hoppy Bobby became MUCH smoother when I switched the large circles to big sprites) -=-=- ''Load, Next List!'' Posted : Thursday, 24 October 2013, 03:28 shroom_monk From what I can tell, you're trying to move each node of the snake to within snake-body-segment distance of the previous node, with the had attached to the mouse, right? Unfortunately, without Monkey, I can't run your code to see if that's what happening or not. If that is what you're after, I think you can achieve the same effect without any trig, just using distance ratios, but I'm not sure if I've correctly deduced what you're going for here. -=-=- A mushroom a day keeps the doctor away... Keep It Simple, Shroom! Posted : Thursday, 24 October 2013, 09:47 spinal Thats exactly it! like this -- https://processingjs.org/learning/custom/snake/ I've done this a couple of times in different languages, it looks like the angles aren't close enough to what they *should* be, so the tail will float off to one side or would straighten up if dragged in a straight line etc. |edit| Dammit! pos_x and pos_y weren't floats.... fixed. Have I learned anything from this? Probably not. Expect the same damn post in a couple of years about the next language I try to learn -=-=- Check out my excellent homepage! Posted : Thursday, 24 October 2013, 15:29 spinal OK, now that the angles look right, time for a little collision detection to attempt to remove the possibility of the snake overlapping itself! #If TARGET="html5" Local MX = MouseX()/scaleX Local MY = MouseY()/scaleY If oldMX <> MX Or oldMY <> MY Then bodyX = bodyX + (MX-oldMX) bodyY = bodyY + (MY-oldMY) oldMX=MX oldMY=MY End If #Else If JoyX(0) >0.2 Or JoyX(0) <-0.2 Then bodyX = bodyX + (JoyX(0)*moveSpeed) If JoyY(0) >0.2 Or JoyY(0) <-0.2 Then bodyY = bodyY + (JoyY(0)*moveSpeed) #End If For t = 1 To segments Local by1:Float = bodyY[t] Local by2:Float = bodyY[t-1] Local bx1:Float = bodyX[t] Local bx2:Float = bodyX[t-1] Local rotation:Float = ATan2(by1-by2, bx1-bx2) For s = 0 To segments If s < t-1 Or s > t+1 Then Local x1 = (bodyX[t]-bodyX[s])*(bodyX[t]-bodyX[s]) Local y1 = (bodyY[t]-bodyY[s])*(bodyY[t]-bodyY[s]) Local p = Sqrt(x1+y1) If p < (bodyS*2) Then Local x2 = bodyX[t-1] + (bodyS*2) * Cos(rotation+1) Local y2 = bodyY[t-1] + (bodyS*2) * Sin(rotation+1) Local p1 = Sqrt(x2+y2) If p1 < p Then rotation = rotation + 1 If p1 > p Then rotation = rotation - 1 End If End If Next s bodyX[t] = bodyX[t-1] + (bodyS*2) * Cos(rotation) bodyY[t] = bodyY[t-1] + (bodyS*2) * Sin(rotation) Next t For t = segments To 0 Step -1 SetCol(0,0,0) Oval(bodyX[t],bodyY[t],bodyS,bodyS) SetCol(255,255,255) Oval(bodyX[t],bodyY[t],bodyS-2,bodyS-2) Next t --v As you can see from the above code, I am attempting to check each segment agains the others, other than the one directly before or after. Once a collision is detected, I check if a change in angle will result in less of an overlap, if so, do that. It seems to work OK if the collision is in a clockwise direction, however, if it is in the other direction, it goes a little nuts. Anyone got any ideas on how to improve it? -=-=- Check out my excellent homepage! Posted : Thursday, 24 October 2013, 15:37 Jayenkai First, you need only check "from this segment to the last", because anything before that has already been checked by a previous segment. Less checks necessary, and results in less crazy movement. .. Currently on iPad so no idea what else to tweak, just thought I'd jump in with that tidbit! -=-=- ''Load, Next List!'' Posted : Thursday, 24 October 2013, 16:02 shroom_monk I'm not quite sure what your algorithm is meant to be here, but it has two mistakes in it regardless: 1) Your calculation of p1 is incorrect, because you forgot to square x2 and y2. 2) Even then, comparing p and p1 is meaningless, since p is the distance between two nodes on the snake, while p1 is the distance from your new node to the origin. |edit| Also, is a collision algorithm even worth it? What if the head of the snake is dragged back through the body? Is there a sensible way of resolving that? |edit| -=-=- A mushroom a day keeps the doctor away... Keep It Simple, Shroom!