Swing back and forth after colliding
Approw
Member Posts: 703
I cant figure out the math to swing back and forth, and maybe up and down naturally.
I have this code, but its a static movement.
Constrain Self.Position.X to 160*cos( self.Time *200%360)+384
It has to be able to bounce on an object, and then swing back to the middle of the screen until its velocity/position is zero (centre of the scene). If possible it has to be able to collide again.
This demo looks really dynamic, but I cant find the project file.
http://null.gamesalad.com/93280922-3f03-436e-ab5b-7a13ed0a4f50/play.htm
Comments
I haven't really got a clue what your question is, but that code should work fine, it should oscillate back and forth horizontally across 320 pixels. Position 'zero' is not the centre of the scene, zero is located in the lower left hand corner.
@Socks haha, Im sorry I wan't very clear. The object starts at the centre, and hangs on a rope. when the tiny box collides with an object from the side for example, I want to make it swing (like in the real world). So the speed of the swing should get less and less untill it stays in the middle, just like the example I posted above. Does it make sense now?
This is what it looks like
Kind of, but I'm still not sure what you are asking for, how to make something behave the like the example supplied in your first post, how to make it start swinging when something hits it from the side, how to make the rope connect to and track the swinging object at one end and the anchor point at the other, how to make it settle into the middle from the swing, or maybe you want someone to work out the whole mechanic for you . . . ?
The example file in your first post is simple enough, it's just something moving in a circular path, but with the position in the path (the angle) constrained to a sine wave.
It should look something like this:
x = radius *cos((range of swing - self.Time *speed of deceleration of swing) *sin(self.time * speed of the swing)plus or minus the angular offset + the x pivot point
y = radius *sin((range of swing - self.Time *speed of deceleration of swing) *sin( self.time * speed of the swing)plus or minus the angular offset + the y pivot point
Also the example in your first post remains upright as it swings, which will look odd with your artwork, so you would want to add the following . . .
rotation = (range of swing - self.Time *speed of deceleration of swing) *sin( speed of the swing)
And you need to finish the swing when it settles to the centre, at the moment it is using self.time for the angle, but this could be a separate value that you just interpolate to 0 - or maybe wrap all this in a rule that says when self.time < range of swing / speed of deceleration of swing then do all the above . . . . . hope that makes sense.
. . . . there may well be a mistake or two in there, not at my computer right now, but I'll check it out a little later
Here you, it was actually as straightforward as I said above (although my explanation is a little messy) basically get an object to move in a circle, limit the range it orbits to a sine wave and then slowly ramp down the range of that sine wave.
A second version with a slightly more dampened settle. (attached)
@Socks thank you very much for the template files! it gives me a great inside on how it works, very clever:)!
However (god when do I stop asking), i'm not able to make the box collide with another
object because the box has a constrained movement. I'm sure there is a way, but the one that I will figure out is most probably jerky and not very efficient.
I made some screenshots to explain the behavior of the tiny white box.
I hope this explains the behavior I'm chasing. If it's to much work or impossible just let me know! I appreciate it anyway:)
Thanks again for all your great help!
@socks by the way, I don't think we need the rope to be part of this behavior. it is now constrained to a top X and Y position, and to the box itself.
I had a sort of working behavior before this.
When self.position.X > 384 , Accelerate 400 to the left.
When self.position.X < 384 , Accelerate 400 to the right.
The problem with this method is that the X velocity will never be 0. when it's close to the centre (384) it keeps going left and right. Then we can do an interpolate behavior to make it stop, but if it collides while interpolating looks very odd.
A worrying use of the word 'we'
I don't understand what any of that means. What is a top X and Y position ?
I don't understand what that means, I have no clue about how your project works, so telling me that the X velocity will never be 0 is like telling me the angle of the ball is always equal to the Z position of the mouse !
There's that 'we' again ! Lol . . .
@Socks The rope in my project doesn't have anything to do with the physics of the box. It is just constrained to two positions in the scene
I officially suck in explaining this project. I guess "I" have to figure this out myself...
Wow, that's a lot of constrains for one actor, maybe just make the rope twice as long as it needs be, make one half of the length invisible (in the artwork) and just use a single constrain rotation ?
I'm sure it can be done, but I suspect is going to be easier and quicker and more successful if you fake it rather than build a fully functioning physics system that does everything. So the box doesn't actually sit on the thing you move, it just gets its angle from the Y position of the thing you move, and when it starts to swing it swaps over to a difference set of rules (or even a different actor) . . . etc, that kind of thing.
@Socks hmm interesting. I think I found the solution (I probably explain it wrong again haha). If I put an accelerate down behavior in the box, and give it a boundary on where it can travel to, so it can only stay within this red line (see attachment). but how do I calculate the maximum radial distance it can travel to?
You don't have to calculate it, it's arbitrary, it can be whatever you want it to be.
@Socks ?? shouldn't I give it a maximum magnitude or something?
I haven't got a clue as I don't know how your solution works, you've not explained it, what is it you would be giving a maximum magnitude to ?
You say you have found the solution and you've worked out a way of constraining the actor to a radial perimeter, how are you doing this ? Calculating the distance is not an issue, as you can make it whatever you want.
. . . . . . . . .
You could possibly use a physical barrier, it might work, this is just a rough sketch.
@Socks No I meant I have the idea on how it should work. Your video is the exactly what i'm looking for! How did you accomplish this?? #SocksTheHero
Oh! Lol, I see, well thinking up how it should work is the easy bit !
How about this for a solution, find some way of getting the box to swing on a rope and also be moved around by a second actor and collide with a third one ?
Basically spawn a bunch of actors in a circle and have an actor collide with it/them, I didn't keep the project, but it's pretty simply to set up. It might work for what you want with some work, but it might also have its own issues.
@Socks Thanks, I get your method now. I saw that the blue circle was made out of straight lines. So the big question now is, how do you spawn a circle made out of straight lines xD? I tried to find it on the forums but I don't think its there (or maybe in your spare code thread?)
Attached.
@Socks Thank you sooooo much for all the help and the demo's! I can finally continue. I will add you to the credits if you would like too. Cheers!
Also, keeping the box actor oriented to always face the centre of the circle really helps it look more natural (file attached).
Flexible rope idea . . . . a bit rough around the edges at the moment, but could add a little more realism to the motion.
@Socks I already figured out how to make the swing look more natural. I also swapped the spawn timers with loop. this makes loading the circle a bit quicker with less frame drops during startup.
Whaaaaat... how on earth did you make the rope look flexible??? Do you realize you just made a cut the rope template hahaha. cheers!
@Socks Any chance on sharing how the flexible rope works:P ?
It's just a bunch of images (I'm sure you could do a much better version), which image is use is defined by the magnitude between the swinging object and the pivot point.