Mounting to player & making player "jump" (Solved)
by Matthew · in Torque X 2D · 07/21/2009 (3:28 pm) · 7 replies
A couple of questions/concerns. If I mount an object to the player object, when I go to move him, he flies across the screen, as in, I tap a button and he slingshots across the screen. The only way I've figured out how to fix this is to disable collisions on the mounter but if I do that, logically, I can't have the object mount to the player upon collision. (I want to put a gun in his hand when he collects it.)
Also, I searched the forums looking for a way to simulate the classic side-scrolling "jump" concept. John K. suggests using a PhysicsComponent and setting a Y velocity. Velocities won't make the player move. Only non-player objects seem to be affected by the PhysicsComponent. Am I missing something or is there a better way to create "Mario jump" behavior upon user input?
Also, I searched the forums looking for a way to simulate the classic side-scrolling "jump" concept. John K. suggests using a PhysicsComponent and setting a Y velocity. Velocities won't make the player move. Only non-player objects seem to be affected by the PhysicsComponent. Am I missing something or is there a better way to create "Mario jump" behavior upon user input?
About the author
#2
07/22/2009 (11:32 am)
If I do it your way, aren't I then preventing the two objects from colliding? That is the opposite of what I want. I want the player to collide with the gun, THEN mount the gun to the player as if he picked up a power up.
#3
Opps! I didn't catch that part. I guess in this case you might have both CollidesWith properties set so they collide with each other and then on the first collision of the player and the gun remove the CollidesWith so they will no longer collide with each other. I'm not sure if this will work, but theoretically it seems it should.
07/22/2009 (3:39 pm)
Quote:If I do it your way, aren't I then preventing the two objects from colliding? That is the opposite of what I want. I want the player to collide with the gun, THEN mount the gun to the player as if he picked up a power up.
Opps! I didn't catch that part. I guess in this case you might have both CollidesWith properties set so they collide with each other and then on the first collision of the player and the gun remove the CollidesWith so they will no longer collide with each other. I'm not sure if this will work, but theoretically it seems it should.
#4
Are you using BindMove and then setting the velocity in ProcessTick when the right control is applied?
07/22/2009 (10:31 pm)
I think the part about velocity not moving the player control object doesn't sound right. I remember from the Space Warrior demo that the ships were both player objects and their movement was made by setting the velocity.Are you using BindMove and then setting the velocity in ProcessTick when the right control is applied?
#5
07/23/2009 (10:54 am)
Im not writing any code for this part. People just made it sound as simple as attaching both a MovementComponent & a PhysicsComponent and setting the velocity but clearly attaching the MovementComponent overrides the physics. I guess that means Ill have to write a custom movement component that applies physics when I press the jump button?
#6
OK, it's not quite plug-n-play, but it still would be fairly straight-forward. You won't need to write a custom component, but can modify the MovementComponent to suit your needs.
The default behavior is that each frame when ProcessTick is called, this happens:
You can keep that for the X part of the velocity, but change the way the Y part is done. Something like if button 0 is pushed and an "inAir" flag (boolean) is false, velocity.Y = some initial value and inAir is set to true. Anytime it is in that method (ProcessTick) again, if inAir is true, then velocity is changed by acceleration due to gravity * elapsed time (which is one of the argument to ProcessTick) until the point it contacts ground again, at which point velocity.Y is set to 0 and inAir is set to false.
I'm just throwing this out off the top of my head, so it may not be totally kosher, but I hope it makes sense conceptually, anyway.
Bottom line: change velocity in the ProcessTick method, handling the x and y separately.
***EDIT*** I see now that Matthew found the way to use the force component for gravity. I totally forgot about that one.
07/23/2009 (6:35 pm)
@Matthew:OK, it's not quite plug-n-play, but it still would be fairly straight-forward. You won't need to write a custom component, but can modify the MovementComponent to suit your needs.
The default behavior is that each frame when ProcessTick is called, this happens:
if (move != null)
{
// set our test object's Velocity based on stick/keyboard input
_sceneObject.Physics.VelocityX = move.Sticks[0].X * 20.0f;
_sceneObject.Physics.VelocityY = -move.Sticks[0].Y * 20.0f;
}So, when the controller stick is not pushed in any direction, the object's velocity gets set to 0 in both dimensions.You can keep that for the X part of the velocity, but change the way the Y part is done. Something like if button 0 is pushed and an "inAir" flag (boolean) is false, velocity.Y = some initial value and inAir is set to true. Anytime it is in that method (ProcessTick) again, if inAir is true, then velocity is changed by acceleration due to gravity * elapsed time (which is one of the argument to ProcessTick) until the point it contacts ground again, at which point velocity.Y is set to 0 and inAir is set to false.
I'm just throwing this out off the top of my head, so it may not be totally kosher, but I hope it makes sense conceptually, anyway.
Bottom line: change velocity in the ProcessTick method, handling the x and y separately.
***EDIT*** I see now that Matthew found the way to use the force component for gravity. I totally forgot about that one.
#7
Sadly, in EITHER case, my character does still randomly, on occasion, FALL through the floor! I set his world limit to the ground and he'll "bounce" up a little so I won't completely lose him, but it's still really odd and I still haven't figured out WHY it happens.
Thank you, though, for your help.
08/06/2009 (7:40 pm)
Well, actually, I decided to go with your implementation eventually. It allowed me to just change velocities using the T2DTriggerComponent so that when you "land" on the ground, I could set velocity to 0 using an OnEnter delegate. The OnLeave delegate is then used to simulate walking off the edge of something. Sadly, in EITHER case, my character does still randomly, on occasion, FALL through the floor! I set his world limit to the ground and he'll "bounce" up a little so I won't completely lose him, but it's still really odd and I still haven't figured out WHY it happens.
Thank you, though, for your help.
Torque Owner Lucas Shinkovich
I had this same problem with a tank turret rotating upon a tank base. You are correct, what is happening is the objects are colliding and their velocities are being changed by the physics engine. What you need to do is setup certain object types and in the CollidesWith property set objects that the player can collide with, excluding the gun. Do the same for the gun and ensure that it cannot collide with the player.