Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Teir 1: Physics on 2-D objects with no mass (i.e. lasers).

Author
Message
mrradd
9
Years of Service
User Offline
Joined: 24th Feb 2015
Location: CA, USA
Posted: 25th Feb 2015 01:34
I have been trying to utilize the physics engine for the nice collision handling it gives me for objects which should not be affected by friction or gravity like lasers. Initially I was updating the trajectories of my ballistics manually, but decided to utilize native physics since I noticed i was essentially made my own physics engine (derp).... Utilizing the bundled physics engine emulating the bounciness of lasers became an issue. No matter what they are affected by gravity--even with a mass of 0.



-mrradd-
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 25th Feb 2015 03:39
You need to apply a force to your lasers that is exactly opposite to the effects of the constant acceleration applied by the engine's gravity.

From the help:
Quote: "Gravity is a constant acceleration applied to all physics objects equally, regardless of mass. Whereas a force applies an acceleration proportional to the objects mass. To counteract gravity with a force will require accounting for mass. "


A test:


V2 T1 (Mostly)
Uzmadesign
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 25th Feb 2015 05:45
Or you could use a kinematic sprite which you can apply velocity to but will not be affected by gravity or other objects. Not sure what you're really trying to do though i have used kinematic sensor sprites for balistics recently...


Using AppGameKit V2 Tier 1
mrradd
9
Years of Service
User Offline
Joined: 24th Feb 2015
Location: CA, USA
Posted: 25th Feb 2015 06:54
Thank you, for your replies!

@CJB
This is similar to something I tried and noticed it is initially affected by gravity, but after the first bounce it flies constant along the X-axis. Basically what I was trying to accomplish except for the initial application of gravity. I can play with this even more though.

@baxslash
I tried this, but wanted to get the bounce action from the dynamic physics.

More or less I am trying to make some lasers bounce off the walls. I have other ballistics flying about, but the lasers didn't look right being affected by gravity. I changed the 'perspective' of my game from a side scroller to a more beatem-up feel. Given the change I am just setting gravity to 0,0. Kind of a cop out but, meh.

-mrradd-
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 25th Feb 2015 08:50
Then it sounds like you're doing the right thing. I would set the gravity to zero and apply a force directly to each sprite you do want gravity working on. It doesn't slow the game down noticeably to loop through a couple of hundred sprites and apply a force equivalent to gravity.


Using AppGameKit V2 Tier 1
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 25th Feb 2015 09:54
Quote: "but after the first bounce it flies constant along the X-axis."


This is likely due to the sprite shape. It'll need to be a circle to reflect off other objects at a more realistic angle.

V2 T1 (Mostly)
Uzmadesign
mrradd
9
Years of Service
User Offline
Joined: 24th Feb 2015
Location: CA, USA
Posted: 25th Feb 2015 17:37
@CJB
Quote: "
Quote: "but after the first bounce it flies constant along the X-axis."

This is likely due to the sprite shape. It'll need to be a circle to reflect off other objects at a more realistic angle.
"


Actually I was happy to see it not affected by gravity after bouncing. I did look into the sprite shape thing and noticed that squares worked for me. It looks funny (in a good way), because my laser image is 'laser.png' from the Space Game that is bundled in the examples. So on a bounce the sprite looks like a laser propeller of doom haha from the rotation. I think I may leave it in Goat Simulator style.

@baxslash
Quote: "Then it sounds like you're doing the right thing. I would set the gravity to zero and apply a force directly to each sprite you do want gravity working on. It doesn't slow the game down noticeably to loop through a couple of hundred sprites and apply a force equivalent to gravity."


I didn't consider applying force later to simulate gravity. Thanks for the tip!

Having never really participated in a coding forum before you two have made it a nice first experience. Have a beer !

-mrradd-
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 25th Feb 2015 19:35
Thanks for the beer it's pretty friendly around here. It's why I keep coming back...


Using AppGameKit V2 Tier 1
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 26th Feb 2015 16:37
Sorry... when I said sprite shape, I didn't mean the shape of the laser image, rather the shape of the BOX2d representation of that sprite. You can still have a box-shaped laser with a circular physics shape (which will make it bounce like a laser being reflected off a surface)

More beer!!! Woohoo!

V2 T1 (Mostly)
Uzmadesign
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 26th Feb 2015 16:58
If you set the shape to circular you may want to rotate the sprite manually or position a none physics sprite over the position of a none visible physics sprite. Then use the velocity of the physics sprite to set the angle of the image sprite. Just a thought.


Using AppGameKit V2 Tier 1
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 26th Feb 2015 17:23
You're going to have a problem with a circular shape on a linear laser. The collision area becomes very "wide" and you'll collide with anything in the near vicinity.

Here's another thought, which will fix the directional challenge too.

1. Create a small circular physics object (invisible), about the desired width of your laser.
2. Create a box shaped physics object for your laser. Make the mass minimal.
3. Attach the laser to the circle with a revolute joint (no friction).
4. Apply all of your forces to the circle. Theoretically, the laser will follow like a tail.

How will it look? I have no idea until you try it. I guess it will all depend on how fast it is travelling.

Quidquid latine dictum sit, altum sonatur
mrradd
9
Years of Service
User Offline
Joined: 24th Feb 2015
Location: CA, USA
Posted: 26th Feb 2015 21:12
I never considered multiple physics shapes and using layering-ish type stuff (That's the nomenclature, right?) to resolve the Spinning Laser Propeller of Doom effect. Before your replies I had actually implemented an angle correction algorithm, but it looked kind of odd--mainly due to the effects of gravity at the time.

One thing that is funny about the laser.png: when set to have a physics shape of polygon, and physics debug is on, the shape around it looks kind of phallic. My immaturity got the best of me and I lol'd a bit.



-mrradd-

Login to post a reply

Server time is: 2024-11-25 18:28:50
Your offset time is: 2024-11-25 18:28:50