May 23, 2012, 01:17:39 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Please report any bugs or issues that you might be encountering with the Beta in the Support System so that we can better keep track of any oustanding issues that may come up.

GameCore Support System
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Picking up and Throwing?  (Read 927 times)
Squat
Hero Member
*****
Posts: 592


View Profile
« on: May 19, 2009, 03:07:54 PM »

Are there any sample scripts for picking up an object in the world and throwing it? I'm having issues with aligning the object to the front of your view instead of always North of you in the world.

I don't quite get how to align something to another object's axis and then branch out from there. Like how can I hold something in front of me that always stays in front of me? I know it's to do with matrices but I'm not quite sure how to go about it.

Any help would be appreciated.
Logged
zknack
Full Member
***
Posts: 207


View Profile
« Reply #1 on: May 19, 2009, 07:49:28 PM »

Squat,

My heart script has pickup and throw as well in it.

If you open the script, the following is from the rock.gsl file within:

Code:

//debug attributes
bool DEBUG_ENABLED = true;
//attributes
bool CanBePickedUp = false;
bool CanBeSmashed = false;
bool CanBeExploded = false;


//player event states
bool PlayerInProximity = false;

//states
bool PickedUp = false;
bool Thrown = false;
bool Destroyed = false;
bool Exploded = false;
bool Smashed = false;

//timer for input
float inputTimer = 0.2f;
float lastInput = 0;
float orientTimer = 1;
float lastOrient = 0;
bool orientEnabled = false;
bool inputEnabled = true;

//position and direction if carried
Vector direction;
Vector startingPosition;
Vector startingRotation;
Object@ thisObject;
Object@ playerObject;
ObjectCollide@ thisObjectCollision;

void Initialize( Object@ object)
{
CanBePickedUp = object.GetControllerSettingBool("CanBePickedUp");
CanBeSmashed = object.GetControllerSettingBool("CanBeSmashed");
CanBeExploded = object.GetControllerSettingBool("CanBeExploded");
startingPosition = object.GetPosition();
startingRotation = object.GetRotation();
@playerObject = GetGameManager().GetWorld().GetObject("Player");
@thisObject = object;
@thisObjectCollision = thisObject.GetCollideObject("Base");
thisObject.SetCollisionEnabled(true);

}

void DynamicsAdvance( Object@ object, float seconds)
{
//input code
if(!inputEnabled)
{
lastInput+=seconds;
if(lastInput >= inputTimer)
{
inputEnabled = true;
lastInput = 0.0f;
if(DEBUG_ENABLED)
Print("Input Timer Stopped");
}
}

if(orientEnabled)
{
lastOrient+=seconds;
}


if(PickedUp && !Thrown)
{
Vector pos = playerObject.GetPosition();
pos.y+=2.4;//above players head
thisObject.SetPosition(pos);
thisObject.SetRotation(playerObject.GetRotation());
thisObject.UpdateCollisionObjectPositions();
return;
}
else if(Thrown) // pickedup is implied
{
//object has to fly

Vector pos = playerObject.GetPosition();
pos.y+=2.4;//above players head
thisObject.SetPosition(pos);
thisObject.SetRotation(playerObject.GetRotation());
//thisObject.UpdateCollisionObjectPositions();

//
direction = playerObject.GetControllerVariableVector("shotDir");
direction.Normalize();

if(DEBUG_ENABLED)
Print(direction.x + "," + direction.y + "," + direction.z);

//direction.x=0;
//direction.z = 0;
direction.y=1.2;
direction*=4000;
Vector throwVelocity = playerObject.GetCollideObject("base").GetLinearVelocity();
//if(throwVelocity.y < 0)
throwVelocity.y=1;

thisObjectCollision.AddForce(direction);
thisObjectCollision.SetLinearVelocity(throwVelocity);
thisObjectCollision.SetAngularVelocity(Vector(0,0,0));
//thisObjectCollision.SetBouncyness(0);
thisObject.UpdateCollisionObjectPositions();
//thisObject.SetCollisionEnabled(false);
Thrown = false;
PickedUp = false;
Destroyed = true;
inputEnabled = false;

return;
}
else if(Exploded)
{
//todo - add some explosive properties
return;
}
else if(Smashed)
{
//todo perhaps we wish to hammer this thing?
return;
}
else
{
if(!Destroyed)
{
thisObject.SetPosition(startingPosition);
thisObject.SetRotation(startingRotation);
thisObjectCollision.SetAngularVelocity(Vector(0,0,0));
thisObjectCollision.SetLinearVelocity(Vector(0,0,0));
thisObject.UpdateCollisionObjectPositions();
}
else
{
//experimental
/*
Matrix mtx = thisObjectCollision.GetMatrix();
mtx.YAxis.Set( 0, 1, 0);
mtx.ZAxis = thisObject.GetCurMatrix().ZAxis;
mtx.XAxis = thisObject.GetCurMatrix().XAxis;
mtx.Orthogonalize( 1, 2);
thisObjectCollision.SetMatrix( mtx);
thisObject.GetCurMatrix().XAxis = mtx.XAxis;
thisObject.GetCurMatrix().YAxis = mtx.YAxis;
thisObject.GetCurMatrix().ZAxis = mtx.ZAxis;
//thisObjectCollision.SetAngularVelocity(Vector(0,0,0));
//thisObjectCollision.SetLinearVelocity(Vector(0,0,0));
thisObject.UpdateCollisionObjectPositions();*/

}
}

//experimental, quite buggy
//thisObjectCollision.SetAngularVelocity(Vector(0,0,0));
//thisObject.UpdateCollisionObjectPositions();

}
bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
{
//player interaction only
if (params.player == null)
return false;
//This code seems pointless so comment it out for now
/*
if(event == "EnterProximity") //we are in proximity, set the value
{
PlayerInProximity = true;
return true;
}
if(event == "ExitProximity") //we are in proximity, now we leave
{
PlayerInProximity = false;
return true;
}
*/

if(event == "Input")
{
if(params.stringValue == "Pickup")
{
if(params.floatValue > 0 && inputEnabled)
{
inputEnabled = false;
if(DEBUG_ENABLED)
Print("Input Timer Started");

if(!PickedUp)//we havent picked it up yet
{
PickedUp = true;
if(DEBUG_ENABLED)
Print("Player picked up " + thisObject.GetName());
return true;
}
else if(PickedUp && !Thrown) //we have picked it up and have not thrown it, so throw it already
{
//we have picked it up, lets see if we are throwing it or not
Thrown = true;
//BeingDestroyed = true; //since this is going to be thrown its going to be destroyed
if(DEBUG_ENABLED)
Print("Player is throwing " + thisObject.GetName());
return true;
}
}
}
}

return false;

}

void CollisionCallback( Object@ object, Object@ otherObject, ObjectCollide@ collide, ObjectCollide@ otherCollide, const Vector& in worldPos)
{
if(otherObject.GetName() == "Basic_Terrain")
{
//collision with terrain
if(Destroyed)
{
if(!orientEnabled)
orientEnabled = true;
if(lastOrient >= orientTimer)
{
orientEnabled = false;
lastOrient = 0;
float distance = -1;
Vector intersect;
Vector normal;
object.Hide();
Object@ hitObject = object.GetWorld().RayCast(object.GetPosition(), Vector(0,-1,0), distance, intersect, normal, false);
object.Show();
normal.Normalize();
object.SetRotation(normal);
Matrix mtx = collide.GetMatrix();
mtx.YAxis = normal;
object.GetCurMatrix() = mtx;

startingPosition = object.GetPosition();
startingRotation = object.GetRotation();
Destroyed = false;
}
}


}
}

void SetControllerVariable( Object@ object, const String& in name, const String& in value)
{

}
void SetControllerVariable( Object@ object, const String& in name, float value)
{

}
void SetControllerVariable( Object@ object, const String& in name, int value)
{

}
void SetControllerVariable( Object@ object, const String& in name, bool value)
{
if(name == "PickedUp")
PickedUp = value;
if(name == "Exploded")
Exploded = value;
if(name == "Smashed")
Smashed == value;
}
void SetControllerVariable( Object@ object, const String& in name, const Vector& in value)
{
if(name == "Direction")
{
direction = value;
direction.Normalize();
}
}

int GetControllerVariableInt( Object@ object, const String& in name)
{
return 0;
}
bool GetControllerVariableBool( Object@ object, const String& in name)
{
if(name == "CanBePickedUp")
return CanBePickedUp;
if(name == "CanBeSmashed")
return CanBeSmashed;
if(name == "CanBeExploded")
return CanBeExploded;
if(name == "InProximity")
return PlayerInProximity;
return false;
}
float GetControllerVariableFloat( Object@ object, const String& in name)
{
return 0.0f;
}
Vector GetControllerVariableVector( Object@ object, const String& in name)
{
return null;
}
String GetControllerVariableString( Object@ object, const String& in name)
{
return null;
}
Object@ GetControllerVariableObject( Object@ object, const String& in name)
{
return null;
}
« Last Edit: May 20, 2009, 10:37:14 AM by zknack » Logged
gekido
Guest
« Reply #2 on: May 20, 2009, 10:06:18 AM »

Picking something up is simply a matter of adding it to the player's inventory.

Here's the 'DropObject()' function from the in-progress character script i'm working on.  I have updated the weapons & pickup scripts so that the player can drop & pickup weapons and they remember how much ammo they have etc.

The part under 'spawn the pickup' is basically what you would want to do, except instead of just positioning the object, you would apply a force to it as well to 'throw' it.

Code:
// the only items that the player can drop at the moment are weapons, but we'll call this DropObject
// for the sake of thinking ahead
void DropObject( Player@ player, Object@ playerObject, Object@ inventoryObject)
{
// remove the object from inventory so we can pick up the new one
String pickupName = inventoryObject.GetControllerSettingString("PickupObject");
if( !pickupName.IsEmpty())
{
// spawn the pickup
Object@ dropObject = GetGameManager().GetWorld().AddObject( pickupName, pickupName);
// position it at waist height somewhere in front of the player
Vector pos = playerObject.GetPosition();
pos.y += 1;
pos -= playerObject.GetCurMatrix().ZAxis * 0.7;
dropObject.SetPosition( pos);
dropObject.UpdateCollisionObjectPositions();
dropObject.ResetCollisionObjectVelocities();
// update the pickup object with the amount of ammo remaining in this weapon
int magCount = inventoryObject.GetControllerVariableInt("MagCount");
int ammoCount = inventoryObject.GetControllerVariableInt("AmmoCount");
dropObject.SetControllerSetting("PickupCountSecondary", ammoCount);
dropObject.SetControllerSetting("PickupCount", magCount);

// clear the handle
@weapon = null;
// and remove it from the player's inventory (deleting the object automatically removes it from the inventory)
inventoryObject.Delete();
}
}

Hope this helps
Logged
zknack
Full Member
***
Posts: 207


View Profile
« Reply #3 on: May 20, 2009, 10:47:46 AM »

In case anyone's wondering- feel free to do what you like with it. If you come up with any improvements you'd like to
contribute, please share- if you don't I'm not worried.

Credit isn't necessary, but always appreciated ;-)


Hope it helps, and let me know if it does :-)
« Last Edit: May 20, 2009, 11:44:10 AM by zknack » Logged
zknack
Full Member
***
Posts: 207


View Profile
« Reply #4 on: May 20, 2009, 12:03:12 PM »

For those that don't want to seach for it, here's my script again for modular health system, and picking up
and throwing objects; either should be readily usable without the other, with a little tinkering.

http://hylian-legends.net/uploads/HartV4.zip

It's just a project file (non-compiled) so you can try it and alter it on the fly if you'd like.

Please let me know if it helps,  Squat or anyone else.


Logged
gekido
Guest
« Reply #5 on: May 20, 2009, 12:41:12 PM »

Just as a side note:  we're aware that the forum css screws up displaying code segments and are looking into a fix for it...
Logged
Squat
Hero Member
*****
Posts: 592


View Profile
« Reply #6 on: May 21, 2009, 01:20:45 PM »

Thanks for the point in the right direction guys. I should be able to make my "capture the cheese" gamemode now.

Any news on the player character samples? I'm pretty much roadblocked right at that part. I have a demo world I can test in, I now need to get the characters working properly. After that I can make more worlds. Before that, I'm only praying that it will work.
Logged
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« Reply #7 on: May 21, 2009, 02:35:53 PM »

i import custom models without problems so far. What seems to be the problem?
Logged
Squat
Hero Member
*****
Posts: 592


View Profile
« Reply #8 on: May 23, 2009, 01:03:39 PM »

The problem I have right now is that any time I bring in a custom character and match the settings from the lego-man, I get a very strange camera pivot point and odd controls that I can't fix. For one, I have to look perfectly straight ahead in order to walk forward with W. If I look more than 5 degrees to the ground, I start walking backwards with the W key and forwards with the S. It's fine with the lego-man, I dunno what's up. I need to try some more tests.
Logged
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« Reply #9 on: May 24, 2009, 06:05:35 AM »

that is very strange, have you checked collision and naming it right? I managed to modify legoman to a custom character and when I use it on other characters it works fine now
Logged
Pages: [1]
  Print  
 
Jump to:  

 
Powered by MySQL Powered by PHP bluBlur Skin © 2006, hbSkins
Powered by SMF 1.1.14 | SMF © 2006-2011, Simple Machines LLC
Valid XHTML 1.0! Valid CSS!