Quantcast
Channel: Questions in topic: "physics 2d"
Viewing all 132 articles
Browse latest View live

Trigger problem on devices

$
0
0
I have the following problem. If i put an object with collider and animation, and dont use a rigid body, when i use OnTriggerStay2D the trigger registers without even the player touch the trigger. If i put a rigidbody2D the object does not appear at all. or trigers other trigger objects on the level depend if i have kinematik on or off. The game works fine at the editor, the problems appear when i build them for ios and android Devices. I have Checked everything, object is with 2D Collider, is trigger is enable, i use trigger 2d code, and the rigidbody is for 2d.

How to use Unity Terrain with 2D Physics?

$
0
0
[Unity's 2D Physics](http://docs.unity3d.com/Manual/Collider2D.html) are constrained to X and Y axes. [Unity's Terrain](http://docs.unity3d.com/Manual/terrain-UsingTerrains.html) is oriented along X and Z axes, with the Y axis begin the "up" direction. As Far As I Know, you can't rotate or change axes of neither of those. This means that you can't ever use these together in a game. Is there any way to work this around? For example, it would be great if you could make the 2D Physics Engine work on other axes - X, Z in this case.

How to calculate angular velocity after a collision?

$
0
0
Hi all! I'm developing a Peggle-style game and I need to calculate the angular velocity to apply to a ball as a result of bouncing off a wall. My ball has a Rigidbody2D and a CircleCollider2D attached to it, but at some point I need to simulate the trajectory so I set it to kinematic and disable the collider to move it around and resolve the collisions myself using CircleCast2D. I managed to implement the formulas to resolve the collision and get the resulting linear velocity and position, but I'm still struggling to calculate the angular velocity. I found some formulas and tried them all, but the results I'm getting are nothing close to what Unity sets to the rigidbody if I let the physics engine do its own thing. I followed [this][1] approach, implementing formula number 7 with no luck at all. Values are not even linearly dependent, so I assume it is not a matter of units of measurement. My setup is as follows: - A ball with a radius of 0,192 and no physics material in its CircleCollider2D with a Rigidbody2D attached to it (mass = 1, linearDrag = 0, angularDrag = 0, gravityScale = 1). - A kinematic wall with a physics material (friction = 0.1, bounciness = 0.6). - Incoming velocity for the collision: (-11,01391, -6,336001). - Incoming angular velocity of the ball: 0 degress/sec. (it is not rotating at all) - Collision normal: (1, 0). - Vector.Angle(normal, incomingVelocity) = 150,0894. Using this setup, if the ball is moved by the physics engine the resulting Rigidbody.AngularVelocity will be -1323,647 degress/sec. However, my calculations give me a value of 44,78571, that when converted to degrees/sec (the output of the formula is supposed to be in radians/sec) gives a total of 2566,032165. As you can see, the values are not even close to Unity's output. What am I missing here? Thanks for your time ;) [1]: http://www2.physics.umd.edu/~mfuhrer/course/spr02/AJP/AJP00482.pdf

How do I create 2D swirling wind patterns? (Use fluid dynamics?)

$
0
0
Hi guys, We're making a 2D game and I want to test some 2D wind patterns which will swirl in different directions (so, not a simple horizontal directional windzone, but involving curves). A good visualization for it is this Van Gogh painting: ![alt text][1] [1]: /storage/temp/66532-1280px-van-gogh-starry-night-google-art-project.jpg ... Imagine a player surfing along these wind currents in the air. The wind current should therefor be able to effect GameObjects physics (the player). So, I'm not sure how to achieve this. I've been looking at fluid dynamics with directional force applied, but I'm not sure if that's going to allow the same 'feel' as wind. I've also looked at adding force along a spline bezier curve, but I'm not sure this makes sense. How would you guys go about this? What should I research in order to get something like this working? Also, it would be ideal if I could randomize these wind currents, but I realize that may be just complication things. Thanks for reading!

Physics.OverlapSphere not working

$
0
0
if (isactive == true) { ParticleManager.instance.PlayPlarticles(1, 0.2f, Color.yellow, 1f, transform.position); explodetime--; if (explodetime == 0) { Collider[] hitColliders = Physics.OverlapSphere(transform.position, exploderadius); for (int i = 0; i < hitColliders.Length; i++) { Debug.Log(hitColliders[i].name); hitColliders[i].SendMessage("Damage", 10); } Destroy(gameObject); } } it doesnt seem to work, no names are displayed in log and no damage is made.

How to make flips, backflips like in game Backflip madness

$
0
0
Hello, how to create flips, backflips physics like in game Backflip Madness? Or is there any assets ? https://youtu.be/_wPDk-5SCVU

2D Snail-like movement

$
0
0
Hello, I want to create a snail-like moving creature in 2D which is just made of muscles and nodes. The muscles can expand and contract and the nodes hold the muscles together. This is how I want my creature to move: ![alt text][1] [1]: /storage/temp/69724-snailmovement.png I tried doing this with changing the friction of the nodes on the surface and by changing the scale of the muscles but that didn't work. How do I do this?

How do you make 2D bounce physics work? (on a small scale)

$
0
0
= EDIT: I have either solved or found a work around for this by scaling up the objects in question after seeing mention of something vaguely related elsewhere. But if that is the fix that leads to another question: why does scale break physics? I'm making an extremely simplistic breakout model at the moment but the 2D physics refuse to work as I'd expect. Objects in question at the moment: - square 'ball' with 2D box collider and rigid body 2D - rectangular 'block' with 2D box collider Both are set to: - 0 friction - 1 bounciness 'Ball' is set to: - 0 gravity - 0 angular drag - 0 linear drag - 1 mass* - freeze rotation Z - "is kinematic" is off *I've tried many magnitudes of difference here with changes to the applied force to match. No matter what the scale, the same speed has the exact same effects as described below. When the game starts I add a force to the 'ball' and it travels towards the block. The collision is detected fine and I'm able to script destroying the block. The 'ball' loses all velocity on the Y axis, but continues to move using only the X axis portion of its previous velocity. On hitting the edge of a second block it again triggers the collision*, destroys that block as well, but loses all velocity on the X axis as well. *to be clear using OnCollisionEnter2D Oddly it seems to work perfectly fine when the initial velocity is higher, though it is not remarkably slow right now. This same thing happened the last time I tried to use Unity's built-in physics. I tried to code the physics directly myself with 'is kinematic' (again this is now off) but would like to learn how to use Unity's system. What am I overlooking?

Prevent physics shaking

$
0
0
Hi people.. I'm having some problems with physics, my character keeps passing through moving floors/objects. This (poorly) animation is what I expect: ![expectation](https://s31.postimg.org/sw6qyj1wr/expectation.gif "")
As you can see in the animation above, the character keeps nice on the floor. Now the reality is this (not animation, used unity): ![reality](https://s31.postimg.org/6livyk4mj/reality.gif "")
Here we have two problems:

1. The character is moving to both sides (and jumping), shouldn happen, since the floor where he is standing is not rotating.

2. The character gets into the floor, sometimes he pass through the floor completely. If I increase the velocity of the rotation, it's almost impossible to make the character stand on the ground.

This is a prototype (thats why the sprites are sh**y). **Code - Rotator.cs** public class Rotator : MonoBehaviour { public float DegreesPerSecond = 80.0f; void Update () { transform.Rotate(new Vector3(0, 0, Time.deltaTime * DegreesPerSecond)); // The grund where the player will stand... Transform block = transform.FindChild("Block"); if (block != null) { block.localRotation = Quaternion.Euler(-transform.rotation.eulerAngles); } } } **Structure** ![alt text][1] The rotator is using Polygon Collider, but that Block child (where the player will be) is using Box Collider 2D. Tried to switch from Polygon to Box Collider and still same issues.

I think the player script is irrelevant, since I'm only moving the player to prevent him fall off the screen. I can disable the player script, the issues listed above will happen, the difference is that the character will fall off, so I don't think the player script is the problem.

I bet that this will happen on every moving object/ground, so, there's a way to fix this? PS. Newbie on unity and game developing.
PS²: English is not my native language.
PS³: Using Unity 5.3 x64. [1]: /storage/temp/75153-structure.png

My computer does not know how to add I think

$
0
0
I am trying to implement the physics of a canon-ball (a little cumbersome code and that is fine and I don't really need to change it as I am learning). I think I have done physics numerical method correctly but the problem is that my computer adds the numbers wrong, and it gives me terrible results. ![alt text][1] [1]: /storage/temp/79374-c9c2525bc06551417d58a1e37971240f.png Just take a look at this specific variable in one example of a loop in a "for" loop. I print out the loop number, do calculations and then print out the variables. That means that for this calculation I need to use the previous vy. The top vy is 22.47091. As you can see at the bottom which I have highlighted the C is: -0.0009003537 This is a code snippet: float c = (a2y * dt * 0.5f); float v3y = (vy + c); V3y according to Unity becomes 22.69472 but that cannot be correct because 22.47091+(-0.0009003537) = 22.46190646 The loop: for (int i = 0; i < 1000; i++) { Debug.Log(i); RungakuttaX(vx, sx, vy, sy, dt); gameObject.transform.position = new Vector3(sx, sy, 0); } The rest of the code: private void RungakuttaX(float vx, float sx, float vy, float sy, float dt) { float v1x = vx; float s1x = sx; float v1y = vy; float s1y = sy; float a1x = FormelX(v1x, v1y); float a1y = FormelY(v1x, v1y); float v2x = (vx + (a1x * dt * 0.5f)); float s2x = (sx + (v1x * dt * 0.5f)); float v2y = (vy + (a1y * dt * 0.5f)); float s2y = (sy + (v1y * dt * 0.5f)); float a2x = FormelX(v2x, v2y); float a2y = FormelY(v2x, v2y); float v3x = (vx + (a2x * dt * 0.5f)); float s3x = (sx + (v2x * dt * 0.5f)); float c = (a2y * dt * 0.5f); float v3y = (vy + c); float s3y = (sy + (v2y * dt * 0.5f)); float a3x = FormelX(v3x, v3y); float a3y = FormelY(v3x, v3y); float v4x = (vx + (a3x * dt)); float s4x = (sx + (v3x * dt)); float v4y = (vy + (a3y * dt)); float s4y = (sy + (v3y * dt)); float a4x = FormelX(v4x, v4y); float a4y = FormelY(v4x, v4y); this.vx = vx + ((dt/6) * (v1x + 2*v2x + 2*v3x + v4x)); this.sx = sx + ((dt/6) * (s1x + 2*s2x + 2*s3x + s4x)); this.vy = vy + ((dt/6) * (v1y + 2*v2y + 2*v3y + v4y)); this.sy = sy + ((dt/6) * (s1y + 2*s2y + 2*s3y + s4y)); Debug.Log("vy:"+vy+" v1y:" + v1y + " a1y:" + a1y + " v2y:" + v2y + " a2y:" + a2y + " c:" + c +" v3y:" + v3y + " a3y:" + a3y + " v4y" + v4y + " a4y" + a4y + "---"); } public float FormelX(float vx, float vy) { float v = Mathf.Sqrt((vx * vx) + (vy * vy)); float d = (0.5f * p * (v * v) * c * a)/m; float dx = -d * (vx / v); if (v != 0) { return dx; } else { return 0; } } public float FormelY(float vx, float vy) { float v = Mathf.Sqrt((vx * vx) + (vy * vy)); float d = (0.5f * p * (v * v) * c * a)/m + g; float dy = -d * (vy / v); if (v != 0) { return dy; } else { return 0; } }

OnTriggerStay2D do not called every fixedUpdate

$
0
0
i am using unity 5.4.2f2 Personal, and i think 5.3 has the same issue. OnTriggerStay2D do not called every fixedUpdate. to reproduce, do like below: (1) two gameObject objA and objB, both with a trigger2D. (2) put objA and objB close enough so their triggers are overlapped. (3) add below script to objA: public class objAControl : MonoBehaviour { void OnTriggerStay2D(Collider2D coll){ Debug.Log("----OnTriggerStay2D"); } void FixedUpdate(){ Debug.Log("----FixedUpdate"); } } (4) run the scene and see log. i expect the log is like that: ----FixedUpdate ----OnTriggerStay2D ----FixedUpdate ----OnTriggerStay2D .... but actually the log is lik this: ----FixedUpdate ----OnTriggerStay2D ----FixedUpdate ----FixedUpdate ----OnTriggerStay2D .... i mean, sometimes two or more FixedUpdate log near each other and no OnTriggerStay2D between them! i think this is a bug, because in early version of unity, like unity 5.2.1, no such problem. i already subimited a bug here: https://fogbugz.unity3d.com/default.asp?849290_d2ha0o2mgkkj1lfa

Increasing performance with physics 2d settings

$
0
0
can anyone give me an example of increased performance physics 2d settings with low quality physics? My app is lagging in my android phone because of physics. I want to decrease quality of its settings. But I really dont know how to adust this settings by optimum way.

Frictionless space

$
0
0
Hey I'm making a 2D space shooter, I have my player moving friction-less like I want and collisions with asteroids work, but when the player collides with an explosion the player slows down which is not what I want, the explosion is a circle that expands as the circle expands it pushes the player away, but when the explosion is destroyed the player no longer being pushed slows down, I want the player to keep it's velocity, but when the explosion is expanding and pushing the player the player's velocity does not seem to increase. I am using a physics material that is friction-less and I have tried using add force in OnCollisionEnter and Exit, but it does not seem to work right, I hope someone has a good answer here is my explosion code: public class Explosion : MonoBehaviour { CircleCollider2D cc; void Start() { cc = GetComponent(); } void Update() { if (GameManager.gameState != GameManager.GameStates.play) return; //increase the collider cc.radius += Time.deltaTime/2; //destroy object after 1.5 seconds Destroy(gameObject, 1.5f); } } Update: Sorry I didn't post my player code because there is nothing in it that slows down the player, also it happens with the asteroids when they hit the explosion as well, the increasing size of the collider is not causing the problem I just tried it with a fixed sized circle, the player and asteroid collisions work fine, it seems as the explosion collider expands it pushes the other objects away but does not effect other object's velocity for some reason.. Update 2: Here is the code I spawn the explosion with: public class LaserBeam : MonoBehaviour { float speed = GameManager.LIGHT_SPEED; public Explosion explosion; void Start() { this.GetComponent().AddForce(transform.up * speed); } void Update() { if (GameManager.gameState != GameManager.GameStates.play) return; //destroy object after 1 second Destroy(gameObject, 1); } void OnCollisionEnter2D(Collision2D col) { if (GameManager.gameState != GameManager.GameStates.play) return; //destroy target if (col.gameObject.tag == "Asteroid") //if (col.gameObject.tag != "Player") { Vector2 size = col.transform.localScale*4; Destroy(col.gameObject); //create explosion explosion = Instantiate(explosion); explosion.transform.position = col.transform.position; explosion.transform.localScale = size; //destroy object Destroy(gameObject); GameManager.playerScore += 50; } } } also my objects all have a 2d physics material with 0 friction and .333 bounciness Here is the ridgidbody for my player: ![alt text][1] [1]: /storage/temp/90569-playerrb.png

Rigidbody2D.IsSleeping issue

$
0
0
When checking the velocity of an object with ...velocity.x == 0 && velocity.y == 0 everything works fine but if i use Rigidbody2D.IsSleeping there seems to be a small delay? if (rb.IsSleeping) { // Calls after the rigidbody has stopped moving +0.2 - 0.3s } if (rb.velocity.x == 0 && rb.velocity.y == 0) { // Everything works fine but for reasons i can't use this }

OnCollsionExit2D gets invoked wrongly even when the character is colliding with the object

$
0
0
Hi All, This is my first post. I am trying to use Physics2D.IgnoreLayerCollision to ignore collision between the player and the platform when the player jumps from under it. It works fine without any issues. But when the platform is moving the code that I added for making the player to stick on the platform does not work. Below is the Player update method: void Update(){ if (transform.position.y <= fallBoundary) { damagePlayer (99999); } //_vy is the velocity of the player in the y direction _vy = r2d.velocity.y; //ignore collision when jumping from below the platform Physics2D.IgnoreLayerCollision(_playerLayer, _platformLayer, (_vy > 0.0f)); } I add the speed of the moving platform to the player when he is standing on it by using OnCollisionStay2D method and when the player leaves the platform I remove the speed by using OnCollsionExit2D method. The problem is that OnCollisionExit2D gets called even when the player is standing on the platform(**Note**:This does not happen when I dont use Physics2D.ignoreLayerCollsion). Below are the OnCollsionStay2D and OnCollisionExit2D methods: void OnCollisionStay2D(Collision2D colliderInfo){ //Debug.Log ("moving"); if (colliderInfo == null || colliderInfo.gameObject == null) { Debug.Log ("return"); return; } if (colliderInfo.gameObject.tag.Equals ("Player")) { PlatformerCharacter2D d = player.GetComponent (); if (rightToleft) { //Debug.Log ("rightToleft"); d.platformSpeed = -xvel; } else { //Debug.Log ("righttoleftfalse"); d.platformSpeed = xvel; } } } void OnCollisionExit2D(Collision2D colliderInfo){ PlatformerCharacter2D d = player.gameObject.GetComponent (); d.platformSpeed=0f; } I am just curious as to why this happens. Thanks in advance for the help.

Snake Movement Like in Snake vs Box

$
0
0
I am stuck at achieving snake movement of game Snake VS Box. can anyone help me.

How to enable the collision between two objects after using Physics2D.IgnoreCollision() ?

$
0
0
Hi, After using the Physics2D.IgnoreCollision() fonction, I've been trying to re enable the collision between the object but I can't find any solution. Is there any fonction that can do the opposite of Physics2D.IgnoreCollision() ? Thanks for your help :) (Ps: Disable and Enable the collider rather than using Physics2D.IgnoreCollision() is not a good alternative in my case)

OnTriggerEnter2D first collision causes lag/freeze

$
0
0
Hey all, I'm currently developing a mobile game and I ran into a small problem. When my player first collides with an enemy, the game freezes for a bit. The weird thing is that this happens only when first time OnTriggerenter2D is called. Enemies that collide with my player afterwards do not cause any lag. I profiled the game and it shows that three things cause this issue, specifically: Physics2D.CompileContactCallbacks, Physics2D.SendContactcallbacks and Physics2D.SendTriggerContactCallbacks. My player has a dynamic rigidbody2D attached as well as BoxCollider2D that has set OnTrigger to true. The same applies to the enemies, except the rigidbody2D is set to kinematic. Here's a screenshot from the profiler: https://i.imgur.com/RDVHeXg.png I'm testing it on Samsung S4 mini. Any ideas how to fix this? Thanks!

How can I make 2D physics bounce the same as the 'multiply' bounce combine mode as seen in 3D Physics materials

$
0
0
In 3D physics materials, we have the setting of 'Bounce Combine' which dictates how the two physics materials bounciness values combine together to determine the overall bounce of the collision. The choices are Average (of the two bounciness values), Minimum, Maximum, and Multiply which just multiplies the bounciness values together Multiply is the mode I really want, but this setting of 'Bounce Combine' is just not available at all for 2D Physics materials, and the way it works by default is the equivalent of 'Maximum' in 3D. One thing I tried to get multiplicative bounce was setting physics materials bounciness to 0 and instead use my own Bounce code: void OnCollisionEnter2D(Collision2D collision) { float multipliedBounce = bounciness * collision.otherRigidbody.GetComponent().bounciness; foreach (ContactPoint2D contact in collision.contacts) { Vector2 impactVelocity = contact.relativeVelocity; float velocityInNormalDirection = Vector2.Dot(impactVelocity, contact.normal); Vector2 force = contact.normal * velocityInNormalDirection * rb.mass * multipliedBounce; rb.AddForceAtPosition(force, contact.point, ForceMode2D.Impulse); } } This *kinda* works, but I feel like I'm doing something wrong because it doesn't act the same as the equivalent bounciness on a normal physics material, it bounces less on flat ground, but sometimes goes chaotic when colliding with other dynamic things, especially with box shapes, who's contact points can be in the far corners of the shape. I would like to know if I'm doing something wrong in this code, or if there's any other solution entirely. Thanks!

2D muscle joint?

$
0
0
Hello! I have two RigidBody2D's connected to each other by a HingeJoint2D. I'm looking for a way of connecting one or many "muscles" between them. Like a spring/distance joint that I can contract/expand, and thereby make the two rigidbodies move, like an arm for example. But if I have two of the muscle joints (I've tried with both distance and spring joints), and set the distance, the two muscle joints will fight each other making the rigid bodies swirl around like crazy. Hope you understand what I mean. The best way I can show you what I'm after is this video: https://www.youtube.com/watch?v=pgaEE27nsQw but it's 3D, so I want to mimic muscles like this for 2D rigidbodies. As you can see in the video each rigid body has many muscles connected to them. Looking forward to see what approach you can recommend. :) Thanks in advance!
Viewing all 132 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>