Hello i'm working on an AI that seeks a target and shot it with an bullet from distance. The bullet will be effected by the gravitation. My game is a 2D platformer and will be released for android. I'm using box2d for the physics.
How could I check if the target can be hit?
Example: AI can't hit the target because there is an object in the way.
I know I could use a ray from owner to target but the bullet won't fly linear to the target.
Related
I'm making a 2D (side view) car game with LibGDX using Box2D. The car has a front wheel and a rear wheel and it's "AWD". I would like to render a dust effect to each point where a wheel touches the ground when the player is "burning rubber". To do this, my plan was to
Implement ContactListener, and in beginContact(Contact contact) method find the Contacts where one of the fixtures is ground and the other one a wheel of the car.
Save these Contacts to a (wheel-specific) array, which is supposed to contain the active Contacts of the wheel (the wheel can touch the ground in multiple points, hence an array is required).
During each render call, for each Contact in each wheel's array: Get the contact position, and calculate velocity difference between the wheel body and the ground body at that position. If the length of the velocity difference vector is greater than a certain threshold, then draw the dust effect (using the velocity difference vector to define the speed and angle of the dust particles).
When endContact(Contact contact) of my ContactListener is called, remove contact from the array.
The problem is that LibGDX appears to use the SAME Contact instance for every Box2D contact, rewriting the existing Contact even if it's still active! This means, for example, that if the rear wheel touches the ground first and the front wheel after that, the Contact instance I saved to the rear wheel's contact array gets overwritten with front wheel's contact properties. Another problem is that when I receive endContact, I don't know which Contact really ended.
Have I understood something wrong, or is this how LibGDX is supposed to behave? Is it possible to somehow adjust this behavior, or is there an easy workaround?
(One option that comes to my mind is to keep listening to beginContact and endContact calls and just keep track of the ground bodies/fixtures that are touching each wheel, but then I would have to calculate the contact points manually each frame, which sounds somewhat difficult, expensive, and redundant - because Box2D anyway calculates it at each world step).
Maybe you can create a MyContactListener implementing the ContactListener class.
The MyContactListener class contains a MyContactInventory object which is a Singleton object containing your Contact Array and a couple of convenient method:
addContact
removeContact
The beginContact and endContact add/remove Contacts to your Array using the convenient methods above.
Then you call world.setContactListener( new MyContactListener());
Hope it helps!
i have been working with a kineticjs game where i have kept one layer for the background, one for the player's character, and others,all together having the same kinetic stage.
Then I ran into a situation where I needed a shape of one layer where i can add multiple events,but though the mouse event are working on the stage they are not getting called on particular shape.I have tried layer.on('mousedown',function(),false);
its not working
try shape.on('mousedown', function().....); as Ani says
I have created a composition in after effects with a video layer and 4 different shape layers. The shapes all move around tracking objects on the video. I want to be able to turn the visible property of these shapes on and off at will during playback.
I have converted their key frames to cue points, and then exported the flv. The shapes have key frames at the same time throughout the video resulting in multiple cue points at the exact same time.
In flash I am only able to listen to the cue point that is dispatched from the lowest layer of my after effects composition. So for example on frame 10, layer 1 and 3 both have a key frame changing the position of the shape. Using the script in after effects I've converted these to cue points. So I now have 2 cue points on frame 10. My program in flash will only hear the cue point dispatched by layer 1, and seemingly ignore the cue point from layer 3.
Is there anyway for me to listen to multiple cue points at the same time? Or am I going to have to go about this another way entirely?
I can't find any information that multiple simultaneous cue points isn't supported but trying to add two cue point at the same time code in Adobe Media Encoder gives an error – indicating that you should probably avoid it.
Another solution would be to collect all the transformations for a given keyframe and collect them in the parameter list for a single cue point. An example could be:
layerChangeCue
layer1 : "x:123;y:456;s:0.78"
⋮
layer4 : "x:123;y:456;s:0.78"
Is there an easy way to calculate the expected trajectory of the bullet in the Nape? I need to build something like this path reflected no more than one or two bodis. Like billiard games. My game do not have gravity.
Start with a stationary rectangle. I assume you can determine which edge the bullet will strike. If it strikes a horizontal edge (constant Y), then reverse the Y-component of the trajectory; If it strikes a vertical edge (constant X), then reverse the X-component of the velocity.
In the case of a circle, you must find the ray from the center of the circle to the point of impact, then reverse the component of the velocity parallel to that ray (do you know how to find the component of a vector parallel to a given vector?).
If the objects can move, and the speed of the bullet is much greater than the speed of the objects, then all you need is the instantaneous positions of the objects-- as far as the bullet is concerned, they are stationary.
If the objects are moving with speed comparable to the speed of the bullet, you must be able to do simple coordinate transformation, in order to shift in and out of the object's frame. For instance, if the object has velocity (2,3) and the bullet has velocity (-5, 9), then in the frame of the object the object is stationary and the bullet has velocity (-7, 6). Then you can calculate the collision in this frame (where the object is stationary), then transform back. For instance, if the object is a rectangle and the bullet hits its bottom edge, the bullet's velocity becomes (-7, -6), which we transform back into the world frame: (-5, -3).
For practice, I'm coding a really simple 2D platforming game in Flash using AS3. I'd like to define two different types of terrain surfaces that the player can walk on based on classic platforming elements. Type1: the player can walk on, and if the player jumps, they will hit their head on it and bounce back to the ground. Type2: the player can also walk on, but if the player jumps and hits their head, they will simply pass through the surface and not bounce back to the ground.
I am using hitTestPoint to resolve collisions for this. My question is: What would be the best method to test for what TYPE of ground I am colliding with? Each ground type has it's own Class associated with it in my Flash IDE and all the different terrain surface types are in the same movie clip on the stage.
Currently I'm testing to see if it hit one type of ground surface, then i'm testing if it hit the other, and then based on those results, I process what I want to happen. This seems to work okay right now, but I'm imagining that I may want to create more than 2 types of ground to collide with. For example, moving platforms. It seems like the code will start to get complex
Eg.
if(_groundType1.hitTestPoint(_player.x, _player.y, true))
{
if(_groundType2.hitTestPoint(_player.x, _player.y, true))
{
//don't hit the players head
}
//hit the players head
}
Try using instanceof - http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary389.html