cannon.js Fire bullets, The bullet passed through the object without collision - cannon.js

I use cannon.js to simulate firing bullets. The bullet passed through the object without collision. I know this is because the speed is too fast. The bullet passes through the object at the interval of two frames. Is there any way to avoid this situation

Related

What is the best way to initialize character stats?

I have a character with a NavMeshAgent
I have a Movement MonoBehaviour attached to that same object.
Say I want to initialize the movement speed. Should I do it in the NavMeshAgent in inspector or make a serialized field in the Movement script that assigns it to the agent during initialization, since move speed is related to the Movement script.
Or maybe there's another way to do it that i'm missing? Was wondering what is the best practice.
Keep your data where it belongs.
NavMeshAgent consumes your 'speed' as input, its not his job to store it.
Movement script makes more sense, or even better a PlayerConfig:ScriptableObject to set the initial value.
But definitivelly keep on Movement script, its job description is on its name. Imagine the player now have a buff or a curse that make it faster/slower, you would change the Movement script for it (and the movement will update the NavMeshAgent).

Empty Sweep Hit Result On Overlap with projectile

I'm trying to make a projectile shooting system, but the Sweep Result is empty. I need it to see whether or not the projectile hit the bone 'head' and to see the hit location.
I made sure that I have a sweep collision on the projectile movement and the character movement.
Here is the overlapping event with the server event handling the ProjectileDamage:
https://blueprintue.com/blueprint/v9gzb-uf/
(Scroll up to see the OnComponentBeginOverlap Event)
The actual system works, but the sweep result is empty.
Thank you in advance
Physics or Projectile Movement
Use OnComponentHit, not OnComponentBeginOverlap, if using physics or a projectile movement component.
OnComponentHit
Event called when a component hits (or is hit by) something solid. This could happen due to things like Character movement, using Set Location with 'sweep' enabled, or physics simulation. For events when objects overlap (e.g. walking into a trigger) see the 'Overlap' event.
For collisions during physics simulation to generate hit events, 'Simulation Generates Hit Events' must be enabled for this component.
When receiving a hit from another object's movement, the directions of 'Hit.Normal' and 'Hit.ImpactNormal' will be adjusted to indicate force from the other object against this object.
NormalImpulse will be filled in for physics-simulating bodies, but will be zero for swept-component blocking collisions.
OnComponentBeginOverlap
Event called when something starts to overlaps this component, for example a player walking into a trigger. For events when objects have a blocking collision, for example a player hitting a wall, see 'Hit' events.
Projectile Movement
If using Projectile Movement Component, you can also use OnProjectileStop event.
Verify hit is impacting a skeletal mesh
You'll only get a Bone Name in the hit result if you are impacting a skeletal mesh.
Example Project
To see an example of a physics impact and a projectile movement impact hitting a skeletal mesh and returning a bone name, see this example project.
I now figured out how to fix my issue.
I just had to set the the object type, from the character, from 'Pawn' to World Dynamic. Now the projectile also has a Hit Result.

Unity3d - check for collision on non moving objects

I have a sphere build from multiple objects. What I want to do is when I touch/click an object, that object should find all adjunctive objects. But because none off them are moving, no collision detection can be used.
I can't find a way to detect these adjunctive objects even when the colliders do collide with each other, as I can see that in the scene. I tried all the possibilities, but none off them are working, because no objects are moving.
Is there a way to check for manual collision detection, or is there some sort of way to let Unity3d do the collision detection automatically?
You could keep a list of all those objects, then when your event happens you can send messages to all them to do what you want them to do.
Lets assume you want your sphere to break into little pieces. You send a Force message to the sphere. Then you use Newton's Laws of motion and find out how much velocity each piece gets. Remember velocity is a vector thus it has direction.
This is how I would do it and still keep the right amount of control over what happens in my game/simulation. Remember F = ma.
you could use RaycastHit (http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html) for your collision, this also works on non-moving objects but it needs more performance
You can add rigidbody to every objects; when you touch one of them, give a force onto it, then it is going to move and trigger event of the adjacent objects.
for the reason you do not want to move the object you touch, you can cancel movement in the OnCollider or OnTrigger event handler function.
I managed to work around this by checking the distance from the selected object and all other objects that are part of the sphere. If the distance is below a certain value, then it is an adjunctive object.
Although this is certaintly not fool proof, it works without problems so far.
I am sorry I was not clear enough. Thanks for all the advice what so ever.

Box2D sleep time

I was just wondering if there was a way to change the amount of time until the sleep state is activated for a body in box2d (cocos2d).
I currently use the sleep state as a way to end a game so it is preferable if I can speed up the time it takes to achieve the sleep state.
Thanks
A physics engine doesn't put objects to sleep based on time. It only puts bodies to sleep which are at rest (idle). Typically the physics engine defines rules when it's safe to put a body to sleep, normally that's when the body has stopped moving at all, when there are no other moving bodies touching it, and when both conditions are met for a certain period of time.
In Box2D you can't modify this behavior unless you modify the Box2D source code (not recommended). In Chipmunk you can at least set the threshold for how long a body must be idle before it is put to sleep. Changing this value can sometimes lead to the effect that slow moving objects will suddenly fall to sleep.
To implement the behavior you want, you should define your own set of rules. Iterate over all bodies that may be moving slowly at the end of the game. Get the values for angular rotation and velocity, and check if they have fallen below a certain threshold that feels good for your game. Then end the game, or you can also manually put the object to sleep with body->SetAwake(false).

How to detect collision between three objects simultaneously in Box2D?

I am new to Objective-C. I am currently working on a game using Cocos2D and Box2D. My problem is that when 3 objects collide together, the game crashes. Now let me describe my game in details:
In my game I have a main character standing on top of a building. Below the building there's this the road. Enemies pass by the road at various random speeds entering the screen from right and exiting from the left. I have created the enemies as b2_kinematicBodies and set a random velocity for each of them using SetLinearVelocity(). The main character shoots the enemies. The projectile (the object being shot) is a b2_dynamicBody. When the projectile hits the enemies, both the projectile and the enemy are destroyed. During gameplay sometimes an enemy moving at a slow speed is crossed by one which is moving at a higher speed. If a projectile hits the two enemies just at the point when they are overlapping and one is about to pass the other one, the game crashes! Please help me with this.
I have detected collision using b2contactListener class.
One thing I didn't mention before is that I am not creating the enemies as individual distinct bodies. Instead, I am creating it once and making it move and I am calling this method (which creates the enemies and makes them move) inside init as below:
[self schedule:#selector(addRightTarget) interval:2.0];
I believe the issue is that the collisions are calculated before your handler gets any calls. Meaning that when your handler gets called, the bullet has hit 2 objects. So you get 2 call-backs as shown below.
Collision Detected: Bullet + Enemy1
Destroy Enemy1
Destroy Bullet
Collision Detected: Bullet + Enemy2
Destroy Enemy2
Destroy Bullet [CRAAAASH!!! You just tried to delete a non-existent object]
1st: You should not be removing anything except in your step function (as someone mentioned in another answer)
2nd: Pick one of these:
Make your list/array of objects-to-delete be a 'set' or implemented in such a way that duplicates are avoided.
Check for existence of your object in the world
The collision only happens between 2 objects in Box2D. So in your mentioned scenario your will get multiple collision events which could be,
Enemy-1 and Enemy-2
Enemy-1 and Bullet
Enemy-2 and Bullet
So one possible reason of crash could be that you are not expecting (Enemy-1 and Enemy-2) collision and you are handling it like you have collision between (Enemy-1 and Bullet) so you might be casting it into wrong class. Make sure you are checking the kind of class "isKindOf" before casting it.
Also you may want to use Contact Filtering and or assign category masks to your enemies so that they don't collide with each other and only collide with bullet.
But it will be more help full if you tell something about how and where you destroy your bodies (I hope its not inside your Collision Detection Functions) and also if you can share the exception text when your application crash, that will be helpful.
I used a rather cheap workaround. I alternately created enemy fixtures of different sizes(differing by few pixels). So now if i shoot them even when they overlap, the app doesn't crash(because only the bigger object collides and gets destroyed). This serves my purpose. Thanx a llot for your help. I really appreciate it! :)