bouncing physics sprite off walls

Get help using Construct 2

Post » Mon Apr 23, 2012 9:17 pm

I have a physics based sprite that is moving around according to the direction its pointing and the impulse applied (currently I guess my sprite isn't rectangle so the impulse applied makes the sprite start rotating.

not only that, i want to have the sprite bounce off the walls when it htis the boundary. Currently I did ad the Bound by behavior to the object...and it does stop the sprite but doesn't reflect it back...
I am guessing there is a better way to do this?
B
5
S
1
Posts: 19
Reputation: 790

Post » Tue Apr 24, 2012 2:40 am

Here is an example where the walls are sprites with physics applied and Immovable property=Yes. You don't need the Bound by behavior. Just put the walls just outside the layout so they can't be seen and they will bounce the balls back into the layout. (Using bound by behavior seems to make the balls hang in the air, so I prefer just the walls.)

The balls use Circle as the collision mask, and to make them bouncier set physics behavior Elasticity property to a larger number. (By default it is .2, in my example it varies between .5 and 1.)

Click the mouse to make the balls bounce.

BouncingBallsPhysics.capxkittiewan2012-04-24 02:48:14
B
29
S
11
G
12
Posts: 281
Reputation: 11,888

Post » Tue Apr 24, 2012 6:15 am

hey thanks for this this helps. What I actually have in mind...might not be possible.

I want a huge world scrolling camera that follows the player (world limits makes u warp to the other side) but every so often LOCK the camera down with an artificial boundary (like the one you describe above) and have the player (and other stuff) bounce around with in it for a while... though not sure how I would figure out the edge of the current screen and add the solids there dynamically. But thanks for this post it helps!!! just gotta think in construct2 to land and figure out how to make things a bit more dynamic.

Its really figuring out what the camera edges is...then think i could set the four solids to those extends on top bottom left right. NO clue how to do this.

codejoy2012-04-24 06:15:55
B
5
S
1
Posts: 19
Reputation: 790

Post » Wed Apr 25, 2012 4:56 pm

This isn't hard at all. You'll use the ScrollX and ScrollY system expressions to figure out where the camera is centered and position the walls relative to that. Save the offset from that position for each wall at the start of the layout and then put them somewhere off camera. WHen you want them again, reposition using (scrollX-self.Xoffset, scrollY-self.Yoffset). Here you go--

WallsAroundWindow.capx

To make the walls and the balls appear, click the mouse. The walls disappear after a short time and the balls get away.

Good luck!kittiewan2012-04-25 18:00:01
B
29
S
11
G
12
Posts: 281
Reputation: 11,888

Post » Wed Apr 25, 2012 8:55 pm

Okay you are the man thanks! That is slick. I will incorperate this and try to figure out also how to get my ship to stop spinning when I add an impulse to it. If i make the collision a bounding box it works, but if i leave it as the rectangle then I cannot figure out how to get my center of mass to make it add the impulse to that...   They said in release 61:

"Physics: entering 0 for an object's image point now means its center of mass, rather than the actual object origin. This is so applying a force at image point 0 does not cause the object to rotate, which is usually the intention."

But I have that on a rectangle and it seems to still make it rotate.

http://www.scirra.com/construct2/releases/r61

So still investigating this,b ut the wall stuff is EXACTLY what I need. TY!
B
5
S
1
Posts: 19
Reputation: 790

Post » Wed Apr 25, 2012 9:25 pm

Your welcome.

Did you try setting Prevent rotation to Yes in the physics behavior properties? I don't see a way to do it as an action, only when you first create the object...
B
29
S
11
G
12
Posts: 281
Reputation: 11,888

Post » Wed Apr 25, 2012 10:27 pm

yah I did say prevent rotation, but I could see where other rotation bumps i would want it to rotate, just not when I add an impulse to it. But for now if i make it a bounding box instead of leaving it as what it loads in, (a rectangle) it seems to work fine for now. Thanks again!
B
5
S
1
Posts: 19
Reputation: 790


Return to How do I....?

Who is online

Users browsing this forum: 99Instances2Go, zenox98 and 27 guests