How can I make my character jump in my game? I was thinking of using a prismatic joint to fire a piston down to hit the ground and fire me up in the air. would that work alright? How could I fire the piston? Any tips? Hints? Tricks? Also, how could I make the player move well against the level?
Prismatic joint use for jumping?
FlashI was thinking about this the other day. I made an example for the testbed which does pretty much what you are suggesting, just by applying a strong force to the 'piston' for a few timesteps. Then there is a smaller force applied to pull the piston back when the character is not jumping. I guess one nice thing about this method is you dont need to check if you are standing on something before jumping.
Here is the source code and a build of the testbed which includes it.
See if you can get the character onto the upper platform with the default settings.
As far as moving in the level goes, you might want to apply a sideways force to the characters body instead of turning a 'wheel' underneath them as this example does (especially if your character doesn't even have a wheel...), or even use a mixture of both.
Using forces and torques to get things done will always make things move realistically (as opposed to setting position, velocity etc directly) but it depends on your game whether that is actually a good thing or not.
Attachment:
piston.png
Here is the source code and a build of the testbed which includes it.
Attachment:
CharacterJumping.rar
See if you can get the character onto the upper platform with the default settings.
As far as moving in the level goes, you might want to apply a sideways force to the characters body instead of turning a 'wheel' underneath them as this example does (especially if your character doesn't even have a wheel...), or even use a mixture of both.
Using forces and torques to get things done will always make things move realistically (as opposed to setting position, velocity etc directly) but it depends on your game whether that is actually a good thing or not.
Use an applyImpulse:
//reverse force for upwards jump
var amount:Number = Math.abs( 10 ) * -1 ;
this.element.applyImpulse( 0, amount );
But remember to check whether your character is grounded - you can work that out yourself for now 😉
//reverse force for upwards jump
var amount:Number = Math.abs( 10 ) * -1 ;
this.element.applyImpulse( 0, amount );
But remember to check whether your character is grounded - you can work that out yourself for now 😉
I've tried figuring that out but I dunno how collisions work. I wanna know how to find out if an object flung at my character should hurt or not. Like if it's a soup can being flung or a boulder dropped, how much it should hurt. Also, if explosive barrels should explode depending on the damage
Lol eCheese.
Your on a bit of a runaway train here mate. The guy above posted specifically what you requested which i have to say im surprised at. To follow it on with a bunch of questions that are complex in themselfs is not really a great approach. As soon as you get code to fling objects, detect damage and explode then will come more questions until the entire application is built.
I wouldnt expect too many replies dude. Sit down one night and study the manual and API and most of what you require (along with the examples on the test bed) is there.
Your on a bit of a runaway train here mate. The guy above posted specifically what you requested which i have to say im surprised at. To follow it on with a bunch of questions that are complex in themselfs is not really a great approach. As soon as you get code to fling objects, detect damage and explode then will come more questions until the entire application is built.
I wouldnt expect too many replies dude. Sit down one night and study the manual and API and most of what you require (along with the examples on the test bed) is there.
Ok - you need to implement the ContactListener as Erin says in the docs if you want to detect collisions, and then loop through the contacts looking for what's hitting what.
If you don't understand the code below - (a) you've not read the API docs or (b)you're out of your depth with AS3 and Box2D, or both 🙂 Custom ContactEvent not enclosed 🙂
The code below shows how we're programming to interfaces, too.
If you don't understand the code below - (a) you've not read the API docs or (b)you're out of your depth with AS3 and Box2D, or both 🙂 Custom ContactEvent not enclosed 🙂
The code below shows how we're programming to interfaces, too.
Code:
public class ContactListener extends b2ContactListener {
/**
* Invoked when a contact is added in the physics world.
*
* @param point The contact point data.
*/
override public function Add( point:b2ContactPoint ):void
{
var event:ContactEvent =
new ContactEvent( ContactEvent.ADD, point );
if( point.separation < 0 )
{
if( event.primaryElement && event.secondaryElement )
{
if( ( event.primaryElement is IContactAdded )
&& !( event.secondaryElement is IContactListenerExempt ) )
{
( event.primaryElement as IContactAdded ).contactAdded( event );
}
if( ( event.secondaryElement is IContactAdded )
&& !( event.primaryElement is IContactListenerExempt ) )
{
( event.secondaryElement as IContactAdded ).contactAdded( event );
}
sendEvent( event );
}
}
}
}
/**
* Invoked when a contact is added in the physics world.
*
* @param point The contact point data.
*/
override public function Add( point:b2ContactPoint ):void
{
var event:ContactEvent =
new ContactEvent( ContactEvent.ADD, point );
if( point.separation < 0 )
{
if( event.primaryElement && event.secondaryElement )
{
if( ( event.primaryElement is IContactAdded )
&& !( event.secondaryElement is IContactListenerExempt ) )
{
( event.primaryElement as IContactAdded ).contactAdded( event );
}
if( ( event.secondaryElement is IContactAdded )
&& !( event.primaryElement is IContactListenerExempt ) )
{
( event.secondaryElement as IContactAdded ).contactAdded( event );
}
sendEvent( event );
}
}
}
}
Original thread: https://box2d.org/forum/viewtopic.php?t=1272