Mesh Object movment

Started by
9 comments, last by Slipper 16 years, 9 months ago
Im using DirectX and C# working on a networked game, what I am trying to do is as a player shoots the user input will send a packet to the server the packet information is type of mesh, location Vector, and direction Vector on the server I'm caculating collision and updating the clients with the new position all im doing is position += direction * .8f; this works great at position Vector 0,0,0 but if the player moves then shoots its not working right, its moving to fast and out of range. any help would be great!!!!
Advertisement
More information is needed. What doesn't work? When you move the player, the player doesn't respond properly or the bullet flies improperly? Can you give a little more detail? It's kind of hard to picture what exactly is happening.
Chris ByersMicrosoft DirectX MVP - 2005
sorry about the lack of info

when the player position moves and then fires again, the bullet is not showing up at the current players position, let me show you what I mean

this is the position packet data before the player is moved of the bullet

position Vector3(0.001653842, 0.01151682, 0.7999153)
position Vector3(0.003307683,0.02303363,1.599831)
position Vector3(38,0.004961525,0.03455045,2.399746)

nice smooth movment in a stright line

after the player is moved

Position Vector3(8.304617,-0.01448199,-10.01892)
Position Vector3(12.21811,-0.02896398,-14.08715)
Position Vector3(16.13161,-0.04344597,-18.15538)

both are using position += direction * .8f

it is clearly a math issue Im just not sure how to go about fixing it.
I just want the bullets to go in a stright line from the players current position and direction and .8f would be the volicity right?


It depends on how you want to define your velocity. The basic formula is:
delta_pos = vel*delta_time.
So in your case, your velocity is the direction variable, and 0.8 is your time difference. If this occurs every frame, and your direction is a unit vector (it should be, and this may be your problem), then you are moving the bullet at a max of .8 units per frame when the direction is aligned with the axis, which I suspect is really fast.

You need to be sure that the direction variable is normalized (has a magnitude of 1). If it is normalized, and you move but still have other issues, you should at least see the bullet moving at the same speed.

Also consider implementing each component of the equation. Calculate delta_time each frame (this frame's time minus last frame's time, use a performance timer for best results, or just use timegettime for now to get going). Normalize the direction vector, then multiply it by a magnitude, where the magnitude is your velocity. i.e. Direction is straight down X at 100 units/sec, and time difference is 0.033 seconds, so:
pos += direction_norm*100*0.033.

Hope that helps.
Chris ByersMicrosoft DirectX MVP - 2005
this is still not working and makes no sence.

I have used this before andseems to me it worked fine

from what I can tell when ever the players position is in the -Z
but is looking along the +z like
positionVector3(0, 0, -10.0f)

directionVector3(0, 0, -9.0f)

the bullet travels behind the player along the -z

if you move to the left/right it goes just crazy

any other suggestions?
I had to something like this with my camera. I accomplished this with a simple function:

[SOURCE]Vector3 direction = unit vector for the direction of travel.float magnitude = the rate of travel.direction.Multiply(magnitude);position.Add(direction); //position would be the object you are moving obviously.[/SOURCE]


This is probably the easiest way to do it. It's really important that your direction vector be a normalized/unit vector.
tried that and still same thing happening, let me get this right to Normalize
a vector3 you just go
direction.Normalize();

correct?

it has to be something to do with the direction im using,
the game uses a first person camera so im using the camera lookAt to determine
the direction, now if the player position is Vector3(0,0,-10) and the player is looking forward on the z the lookAt will be Vector3(0,0,-9). now when you add two -numbers they get bigger right?

so position(0,0,-10) += direction(0,0,-9) * time;

with this said the object will keep moving along the -z if the lookAt <= -1.0f
so how can I fix this problem?

is there something else I should do to that lookAt vector before sending it?
No, your direction vector should be (0, 0, -1). The x,y,z values should fall between -1 and 1. Always.

Say you want your object to move by 0.5f every time you press up. Your direction vector is (0, 0, -1), therefore it points along the -z axis. The object's position is (0, 0, -10);

magnitude = direction * 0.5f = (0, 0, -0.5);
position + magnitude = (0, 0, -9.5);

That should be the outcome. I don't know what else i could tell you because I am using that exact calculation and it works flawlessly for me. If it's still not working, try doing this with just the bullet on a keystroke. Also trace each time the bullet moves to see how the position progresses. Then see what happens when you add the player position in.
that was/is my problem, im using the firstperson camera in the sampleFramework and when you move the camera backwords the camera lookAt stays 1 vector in front of the position or eye of the camera
so I will change the vector3 to either a +1 or -1 and see if that works,

now how about the x and y are they going to go above +1 or -1 on the x or y?

thanks for your help
No, the x,y and z should all be between 1 and -1. Thats what i meant when i said unit vector.

This topic is closed to new replies.

Advertisement