Player Movent and Rotation following an entity

Blitz3D Forums/Blitz3D Programming/Player Movent and Rotation following an entity

Clyde(Posted 2005) [#1]
Player Movement and Rotation following an entity...

Hiya!

Im trying to come up with a control system that has the players entity move towards and rotate depending on where an entity is moved to around the screen with the mouse. But im having a bit of bother with getting it to work right. And wonder if you great folks, could help me in getting it working. that would be brillo. :)

Here's the code:


Im after a control system along the lines of Plobb! I first tried this with sprites, and have opted for quad meshes as I was having problems with the entities alpha. Hopefully it'll work with both. And i havent figured out how to have the mouse pointer gfx doesnt effet the player.

What you'll notice is that from the code above, the rotating and movement isnt quite right. There maybe a better method. And if you could help me out, much appreciated.

Many many thanks,
Clyde :)


Ross C(Posted 2005) [#2]
Hey, try this code out. It makes a pivot point at the desination mesh and move towards it, smoothly. Then, it makes the cube point to the pivot and move towards that smoothly. The result is it will follow and turn towards the sphere. Arrow keys to move. Sorry i'm in a rush, so no time for coments :S

Graphics3D 800,600
SetBuffer BackBuffer()

cam = CreateCamera()
PositionEntity cam,0,0,-30

des = CreateSphere()

start = CreateCube()

pivot = CreatePivot()


While Not KeyHit(1)


	If KeyDown(200) Then MoveEntity des,0,0.1,0
	If KeyDown(208) Then MoveEntity des,0,-0.1,0
	If KeyDown(205) Then MoveEntity des,0.1,0,0
	If KeyDown(203) Then MoveEntity des,-0.1,0,0


	PointEntity pivot,des
	MoveEntity pivot,0,0,EntityDistance#(pivot,des)/50.0

	PointEntity start, pivot
	MoveEntity start,0,0,EntityDistance#(start,pivot)/50.0
	
	UpdateWorld
	RenderWorld
	Flip
Wend



IPete2(Posted 2005) [#3]
Clyde

a simple solution may be to use the Supercam code from the archives, it will allow you to set an entity to follow another entity, with rotation and distance etc.

It is a fab bit of code and will probably do you well for your task, here it is for you :

http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=798

Simply swap out the camera for the player and the other entity becomes the player.



IPete2.


Clyde(Posted 2005) [#4]
Wicked! :)

Cool and cheers dudes.I'll check them both out. Im a liitle lost with the supercam method though.


Clyde(Posted 2005) [#5]
Ross C, I get flickering on the objects when their both together. Is this to do with needing entity order?

I see that it works on the Z positions.

Will the firing work with the similar method too?


Ross C(Posted 2005) [#6]
Probably z order, because the two meshes are exactly the same Z co-ord. It won't really happen with sprites i don't think. If it does, move them a very small amount on the Z axis.

How do you mean, the firing? Will the bullets home in?


Clyde(Posted 2005) [#7]
Thanks Ross C. :)

Yeah, will they fire towards the direction of the pivot / mousepointer?


Ross C(Posted 2005) [#8]
So, even it the mousepointer moves, it will follow you mean?


IPete2(Posted 2005) [#9]
Clyde,

Let me help! With supercam you would do something like this:

Your programme so far plus...

SuperCam(player_entity, entity_player_has_to_follow, speed to follow #,distance to stay behind #,height above the entity you are following #,0,0)

updateworld
renderworld
etc.

plus at the end of your code so far put the supercam function, you shouldnt have to alter anything in it at all as the variables are passed by the function call. I have given them names you should be able to understand what is needed, but you do not need to know what it does, just that it works, trust me. Place this code at the end of your current programme, just like this...


Function SuperCam(cam,ent,cspeed#,dist#,hite#,xrot#,tilt#)

TFormPoint 0,hite#,-dist#,ent,0

cx#=(TFormedX()-EntityX(cam))*cspeed#
cy#=(TFormedY()-EntityY(cam))*cspeed#
cz#=(TFormedZ()-EntityZ(cam))*cspeed#

TranslateEntity cam,cx,cy,cz
PointEntity cam,ent
RotateEntity cam,xrot#,EntityYaw(cam),tilt#

End Function


Clyde(Posted 2005) [#10]
Ross C - I've added mouse movement. Not sure on how to position the entities differently so no flickering. Also How about the rotating?




IPete2 - Cheers for that. I'll give that a try out.

Many thanks,
Clyde :)


Clyde(Posted 2005) [#11]
Here's the supercam implementation, but there's still the exact same problem with the rotating. Have you played plobb? As thats what im trying to get towards.



Cheers and thankyou so much,
Clyde :)


octothorpe(Posted 2005) [#12]
What every happened to good old-fashioned trig?

I realize Plobb was likely done in 3d, but I made this as simple (and quickly) as possible to show how to code a Plobb-style movement scheme. It should be trivial to call PositionEntity() and RotateEntity() instead of drawing geometric shapes to the backbuffer.

Other than the edge-wrapping occurring farther outside the screen boundaries in the real Plobb, I don't believe I've forgotten anything.

If anyone has a better way to get an angle from dx/dy than what I'm doing, please let me know. I feel dirty using that many IF tests for something so simple.




Clyde(Posted 2005) [#13]
Thanks Octothorpe dude: - Thats great, if I were doing a 2D game. Will that method work and convert to be used with entites in a Blitz 3D environment?

Ross C and IPete2 - any luck with the rotating / firing part with the codes above, please dudes?

I have 3 and a half methods ( including mine, hehehe ) would be great now, if we can get them or at least one of them polished! :P

Cheers and many thanks once again for your great and much appreciated help.
-Clyde :)


octothorpe(Posted 2005) [#14]
Thats great, if I were doing a 2D game. Will that method work and convert to be used with entites in a Blitz 3D environment?


Yes. Sorry, I thought I made that clear above. The principles are the same - you're essentially making a 2d game, so the math and movement code don't have differ, just the display code.

Set up your entities, camera, and lights as you were doing earlier, then simply call PositionEntity(e,x,y,0) and RotateEntity(e,0,0,dir) to move your entities to the spots you've calculated for them instead of drawing ovals at those spots. Oh, and replace my sloppy use of 0 and GraphicsHeight() and GraphicsWidth() with variables for your map bounds.


Clyde(Posted 2005) [#15]
Im making a right pigs ear of things. Please could you show me the code working with B3D commands.

Cheers,
Clyde


octothorpe(Posted 2005) [#16]
I don't feel like doing it for you, but I wouldn't mind helping you to understand how to do it yourself. What exactly are you having trouble with?


Clyde(Posted 2005) [#17]
Thanks. Im going to go for a simple cursor key movement, as this is begining to annoy me now.

Yes. Sorry, I thought I made that clear above. The principles are the same - you're essentially making a 2d game, so the math and movement code don't have differ, just the display code.

Set up your entities, camera, and lights as you were doing earlier, then simply call PositionEntity(e,x,y,0) and RotateEntity(e,0,0,dir) to move your entities to the spots you've calculated for them instead of drawing ovals at those spots. Oh, and replace my sloppy use of 0 and GraphicsHeight() and GraphicsWidth() with variables for your map bounds.


That mate. As there'll be more then simply just putting in positionentity and rotatesprite or rotatentity.


Ross C(Posted 2005) [#18]
Clyde, you need to set you sprite view mode differently.


Ross C(Posted 2005) [#19]
Your Function should now look like:

Function InitializeTest()
	
	;
	; Setup camera.
	;
	MainCam = CreateCamera()
	
	PositionEntity MainCam,0,0,-30
	
	;
	; Setup Mouse pointer.
	;
	MousePointer 	= CreateSprite();Sphere()
	
	control 		= CreatePivot()
	controlsprite	= CreateSprite( control )

	ShowMe			= False
	
	;
	; Setup player.
	;
	Player 			= CreateSprite();Cube()
	SpriteViewMode Player,3

End Function


:o)

As for the flickering, just move one of the sprites slightly in the Z direction a very small amount.


Clyde(Posted 2005) [#20]
Cool and thanks mate. And how would the player rotating fit in?


Ross C(Posted 2005) [#21]
Hmm, you could grab the (x,y) co-ords of the mouse move, before you reset it's position to the middle of the screen. Then, work out the angle between the centre of the screen and the co-ords, and rotate the player to that. It might be a little jerky tho. I'm not entirely sure about that tbh...


Clyde(Posted 2005) [#22]
Thanks there Ross C :)

I'll have ago at this later ron. I did have ago in my intitial post.


octothorpe(Posted 2005) [#23]
And how would the player rotating fit in?


You've failed to explain what you want. The only reason I know what you're talking about is because I've played Plobb a lot.

As there'll be more then simply just putting in positionentity and rotatesprite or rotatentity.


Yes, you'll also need to set up your entities, camera, and lights as you were doing earlier (as I already mentioned above.) This is all that's required; create your entities, then in each update, calculate where they should move to and position them.

Have a look at how little I changed below. I simply moved my code into your Update*() functions. I also had to move the camera back to account for the larger scale of x,y coords in my code.




Clyde(Posted 2005) [#24]
Firstly, can I say a whooping big thanks to you dudes for helping me out and along with this. It's brilliant.

OctoThorpe - Just I had problems before with using mousex() / mousey() with entities. I used the translateentity and movemouse XRES/2,YRES/2.

And was ( and still am to be honest ) a little puzzled with the Camera setup. As im now trying to put in appropraiate boundary checks. Not quite sure if im going to opt for screen wrapping or with in the confines of the screen.

Cheers,
Clyde


octothorpe(Posted 2005) [#25]
A ha! I finally figured it out! The solution ended up being a lot simpler than the 6 lines of code I've got accomplishing the same thing in my GUI prototype!
positionentity MainCam, graphicswidth()/2, -graphicsheight()/2, -graphicswidth()/2
I've fixed my code above.

And was ( and still am to be honest ) a little puzzled with the Camera setup.


The camera set up and playfield area are both arbitrary, all that's important is that the camera shows the entire playfield. I chose to make the playfield stretch from (0,0,0) to (0,graphicwidth(),graphicsheight()), so I needed to move the camera back far enough to show it.


octothorpe(Posted 2005) [#26]
Just I had problems before with using mousex() / mousey() with entities. I used the translateentity and movemouse XRES/2,YRES/2.


Huh? Don't you have to use MouseX() and MouseY() to do your TranslateEntity() on your mouse-representing entity?

If you're having trouble with 3d stuff, I highly recommend you play around in 2d until you understand how everything works. There's a lot less to grasp for a beginning programmer in the land of two dimensions. Also, you might get help that's more to your taste if you post to the Blitz3D Beginners Area.


Clyde(Posted 2005) [#27]

Don't you have to use MouseX() and MouseY() to do your TranslateEntity() on your mouse-representing entity?


Nope as I was advised to use MouseXSpeed() and MouseYSpeed()

I've been using 2D for many years. You threw me when presenting the option of a 2D math routine within Blitz 3D. Moving entities based on the mouse in 3D is an area i've not done a busting amount on, and theres not a great deal on offer in the commands reference - hence my asking for help on it. Of which I thank you all once again.

Cheers and all the best,
Clyde


octothorpe(Posted 2005) [#28]
My bad, I wasn't aware there were such commands! :)

Either approach will work. If you wanted to use your translateentity and movemouse approach, you would need to get the mouse (entity's) x and y coords via EntityX() and EntityY() when you need them for any calculations. You'd also need bounds-checking on the mouse entity to prevent the player from rolling it off the "screen".


Clyde(Posted 2005) [#29]
Thanks mate, I'll give this a whirl over the weekend.