Yet another problem...

Blitz3D Forums/Blitz3D Beginners Area/Yet another problem...

Buggy(Posted 2006) [#1]
I'm having trouble... the idea is a simple sphere movement game, and all I have is the left key coded (when you press left, you go left), and already it doesn't work correctly. My problems:

1. When I animate it, the sphere refuses to budge. To get it to move you have to take out the animate command.

2. When you press left (assuming you have taken out the animate command so it can move) it not only turns left, but flies into the air - essentially turning left at an angle. I've rotated and turned the sphere's brains out, and still it doesn't work. Is there a way to find the sphere's axis? I don't know how to fix this.

Here's the code:

Graphics3D 800, 600

SeedRnd MilliSecs()



Const leftkey = 203, rightkey = 205, upkey = 200, downkey = 208
Const spacebar = 57, enter = 28

light = CreateLight()
PositionEntity light, 0, 0, 0

camera = CreateCamera()

PositionEntity camera, 0, 10, 0

sphere = CreateSphere()
PositionEntity sphere, 0, 1, 20
EntityType sphere, 1

spheretex = LoadTexture("blitzlogo.bmp")
ScaleTexture spheretex,.125,.25
RotateTexture spheretex, 90

EntityTexture sphere, spheretex


RotateEntity sphere, 0, 0, 90


For frame = 0 To 10
	
	TurnEntity sphere, 0, 36, 0
	
	SetAnimKey sphere, frame
	
Next

sequence = AddAnimSeq(sphere, frame - 1)



PointEntity camera, sphere

ground = CreatePlane()
PositionEntity ground, 0, -1, 0
EntityType ground, 2

Collisions 1, 2, 2, 2

SetBuffer BackBuffer()

Animate sphere, 1, .5

While Not KeyDown(1)
	

	
	

	

	
	If KeyDown(leftkey)
	
		TurnEntity sphere, -15, 0, 0
				
	EndIf	
	
	
	MoveEntity sphere, 0, 0, .5
	
	UpdateWorld
	RenderWorld
	
	Text 0, 0, EntityZ(sphere) - EntityZ(camera)
	
	If Animating(sphere)
	
		Text 0, 20, "Animating"
		
	EndIf
	
	Flip
	
	Delay 15
	
Wend
Any help would be much appreciated.


Stevie G(Posted 2006) [#2]
Your issue is that your animation changes the balls orientation on the y ( yaw ) axis, when you press left you change the x ( pitch) axis. Should this not also be the y axis??

By then using moveenttiy 0,0,.5, you're effectively moving the sphere in the direction that it's z axis is pointing. Sometimes upwards due to the x axis changes when pressing left.

What you could do is create a pivot, parent the sphere to the pivot, animate the sphere but turn the pivot on it's y axis and move the pivot as you're doing.

Stevie


octothorpe(Posted 2006) [#3]
Or use TranslateEntity if you want to move your entity independantly of its rotation.


Buggy(Posted 2006) [#4]
I don't really understand what it is you mean, Stevie G. When I create a pivot, the sphere refuses to animate.


Buggy(Posted 2006) [#5]
I just did away with the animation for now and it worked fine.


Stevie G(Posted 2006) [#6]
Graphics3D 800, 600

SeedRnd MilliSecs()



Const leftkey = 203, rightkey = 205, upkey = 200, downkey = 208
Const spacebar = 57, enter = 28

light = CreateLight()
PositionEntity light, 0, 0, 0

camera = CreateCamera()

PositionEntity camera, 0, 10, 0

pivot  = CreatePivot()
PositionEntity pivot, 0, 1, 20
EntityType pivot, 1

target = CreateSphere( 4, pivot )
PositionEntity target, 0, 0, 10
EntityColor target,255,0,0

sphere = CreateSphere( 4, pivot )

;spheretex = LoadTexture("blitzlogo.bmp")
;ScaleTexture spheretex,.125,.25
;RotateTexture spheretex, 90
;EntityTexture sphere, spheretex
;RotateEntity pivot, 0, 0, 90


For frame = 0 To 10
	TurnEntity sphere, 0, 36, 0
	SetAnimKey sphere, frame
Next

sequence = AddAnimSeq(sphere, frame - 1)



PointEntity camera, sphere

ground = CreatePlane()
PositionEntity ground, 0, -1, 0
EntityType ground, 2

Collisions 1, 2, 2, 2

SetBuffer BackBuffer()

Animate sphere, 1, .5

While Not KeyDown(1)
	

	
	

	

	TurnEntity pivot, 0 , ( KeyDown( leftkey ) - KeyDown( rightkey) )*5.0, 0
	MoveEntity pivot, 0, 0, ( KeyDown( upkey ) - KeyDown( downkey ) ) * .5
	
	UpdateWorld
	RenderWorld
	
	;Text 0, 0, EntityZ(sphere) - EntityZ(camera)
	
	If Animating(sphere)
	
		Text 0, 20, "Animating"
		
	EndIf
	
	Flip
	
	Delay 15
	
Wend


The red target is simply to show you what direction the pivot is pointing. You may have to set the animation key using the global param of turnentity .. not sure.

Stevie


Buggy(Posted 2006) [#7]
She's a beaut! I'm not quite sure how that works...

I just got rid of the animation altogether, but now I have another problem.

I set it so the camera is always pointing at the ball.

The problem? When I try to turn the ball to the side of the screen, it appears to go off into the distance. This is my fault, not the camera's, because I know that that's what it should look like, but it just doesn't look right. Any ideas?


Stevie G(Posted 2006) [#8]

I just got rid of the animation altogether.


A wise move ;)

Tell us exactly what your trying to do here? If it's a kinda supermonkeyball thing then there is code in the archive which will work pretty well for you. Look for a rolling ball example.

Stevie


Buggy(Posted 2006) [#9]
Ahh... thanks.


Buggy(Posted 2006) [#10]
I can't find it!


Stevie G(Posted 2006) [#11]
http://www.blitzbasic.com/codearcs/codearcs.php?code=856


Buggy(Posted 2006) [#12]
Thanks a bushel!


Sir Gak(Posted 2006) [#13]
Buggy: "You know you're a nerd when you get physics to do your dirty work.
"
Remember the 1950s movie Forbidden Planet? All their work, dirty or otherwise, was done by a machine, creation by mere thought! Were the members of that super-science race nerds also? Eh? Well? Well?
LOL :)


Buggy(Posted 2006) [#14]
Ya got me!


Buggy(Posted 2006) [#15]
I don't understand Jeppe Nielson's example - it seems too advanced for me. Can someone explain TFormVector please? It merely confuses me.

I also don't understand why - in my sphere game - when I added RotateEntity so that when I press down the up arrow, the sphere should move as well as rotate, all that happens is the sphere turns sideways - no matter how much I rotate it. I didn't want to use TurnEntity, because that turns it's axes.

Did that make sense?


Ross C(Posted 2006) [#16]
TFormVector basically takes the direction something is moving, and transforms these values into another entity's space. For instance, say you wanted to do do a linepick straight down from a mesh, taking into account it's rotation, then TFormVector would be useful.

Say,

LinePick EntityX(mesh), EntityY(mesh), EntityZ(mesh), 0, -5, 0


This will do a linepick -5 on the Y axis. Linepick is a global command, it means it will use the World Axis's. So, if you rotated the mesh, and tried to do a linepick straight down from the meshes point of view, you would need to know the distances to send the line pick. So, the 0,-5,0 of the linepick, is a vector. So, you would need to transform that into the meshes axis.

So, from the docs:

TFormVector x#, y#, z#, source_entity, dest_entity
Parameters
x#, y#, z# = components of a vector in 3d space 

source_entity = handle of source entity, or 0 for 3d world 
dest_entity = handle of destination entity, or 0 for 3d world 


So, you would need to do this
TFormVector 0,-5,0 , 0, mesh


The last two parameters there are the SOURCE and DESTINATION entities. 0 means use the WORLD co-ords. So, you transforming from the WORLD to the MESHES co-ords. And your transforming (0,-5,0)

Now, once you have called that command, the results get put in the following...

TFormedX()
TFormedY()
TFormedZ()


Those would be your new vectors. So, your line pick would be:

LinePick EntityX(mesh), EntityY(mesh), EntityZ(mesh), TFormedX(), TFormedY(), TFormedZ()


Which would properly send a linepick straight down from the entities rotations. The actual use of LinePick here isn't too important, just the point of what TFormVector does. I hope that helps :o)


Buggy(Posted 2006) [#17]
What does a LinePick do? I was told it finds the closest entity to another entity, but then why are there so many paramaters?


Buggy(Posted 2006) [#18]
Oh! Now I understand LinePick... well, sort of. But what does the radius paramater mean? Is there a default radius if you don't use it?

Am I right in that LinePick is like drawing a line through 3D space, and returning the first entity that that line touches? What is meant by "first" entity? If there are two, which will be "first"? When would you use it?


Buggy(Posted 2006) [#19]
I'm still kind of confused, Ross C, but that was the closest I got to understanding it! What are the values that TFormedX(), TFormedY(), and TFormedZ() get?

How does Blitz find the amount an entity is moving? And how does it give these values to another entity?


big10p(Posted 2006) [#20]
Buggy: can you please delete half the asterisks in your sig. I view in 800x600 and it's really annoying having to use the horizontal scrollbar to read your posts. Thanks.


t3K|Mac(Posted 2006) [#21]
800x600? god damned hardcore freak ;)
i use 1600*1200 - its pretty fine with all those asterisks ;)


Ross C(Posted 2006) [#22]
Blitz doesn't find the amount it's moving. It's numbers you put in. You need to code these things.It's if you were moving an entity, and you want to get the vector respective to another entities positions. Tbh though, i'd stick to basics more and you'll eventually get to grips with it, if you start using vectors more.

The raduis parameter in LinePick means the raduis of the line that gets sent out. And yes, their is a default. Every optional command has defaults that blitz will set if you don't specify them.


Buggy(Posted 2006) [#23]
I think I kind of understand LinePick, but don't really know why I'd use it. Vectors are way out of my mental capacity.

I normally wouldn't worry about this, but Jeppe Nielson's ball physics demo uses these commands all over the place, and I can't figure out how to make my own ball game.


Ross C(Posted 2006) [#24]
The linepick command is useful when you need to say, find anything directly below a certain location. The linepick is an invisble line, that will collide with the first entity the has a pickmode set. It returns the entity, so you can use it. So many uses :o)


Buggy(Posted 2006) [#25]
Well, back to my last question:

I also don't understand why - in my sphere game - when I added RotateEntity so that when I press down the up arrow, the sphere should move as well as rotate, all that happens is the sphere turns sideways - no matter how much I rotate it. I didn't want to use TurnEntity, because that turns it's axes.

Did that make sense?




Sir Gak(Posted 2006) [#26]
@t3K|Mac

I use 800x600 also.


Buggy(Posted 2006) [#27]
Anyone?


I also don't understand why - in my sphere game - when I added RotateEntity so that when I press down the up arrow, the sphere should move as well as rotate, all that happens is the sphere turns sideways - no matter how much I rotate it. I didn't want to use TurnEntity, because that turns it's axes.

Did that make sense?



I also found out that this happens even if RotateEntity is set to 0, 0, 0.


octothorpe(Posted 2006) [#28]
Can you post the latest code?


Buggy(Posted 2006) [#29]
Well, I just tried changing it to TurnEntity with a pivot inside the sphere, but something's still wrong. Check it out. Sorry if it's hard to understand.

Graphics3D 800, 600

SeedRnd MilliSecs()

;CONSTANTS

Const leftkey = 203, rightkey = 205, upkey = 200, downkey = 208
Const spacebar = 57, enter = 28

Const gravity# = -.04

;TYPES

Type sphere

	Field shape
	Field pivot
	Field vzlimit#
	Field collision
	Field slowdown#
	Field inertia#
	
End Type

light = CreateLight()
PositionEntity light, 0, 0, 0



camera = CreateCamera()

PositionEntity camera, 30, 10, 0

Global sphere.sphere = New sphere

	sphere\shape = CreateSphere(8)
	sphere\pivot = CreatePivot()
	sphere\collision = 1
	sphere\vzlimit# = 1
	sphere\slowdown# = .02
	sphere\inertia# = 0
	
	EntityType sphere\pivot, sphere\collision
	
	EntityType sphere\shape, sphere\collision
	ScaleEntity sphere\shape, .85, 1, 1

	spheretex = LoadTexture("blitzlogo.bmp")
	ScaleTexture spheretex,.125,.25
	RotateTexture spheretex, 90

	EntityTexture sphere\shape, spheretex


	RotateEntity sphere\shape, 0, 0, 0

ground = CreatePlane()
PositionEntity ground, 0, -1, 0

Global groundcollision = 2

EntityType ground, groundcollision
EntityTexture ground, spheretex

cube = CreateCube()
PositionEntity cube, 0, -5, 30
RotateEntity cube, 45, 0, 0
ScaleEntity cube, 5, 5, 5
EntityType cube, groundcollision

Collisions sphere\collision, groundcollision, 2, 2


SetBuffer BackBuffer()



While Not KeyDown(1)
	

	PointEntity camera, sphere\shape
	
	move()
	
	UpdateWorld
	RenderWorld
	
	Text 0, 0, "Onscreen Triangles: " + TrisRendered()
	Text 0, 10, "Player's Y Coordinates: " + EntityY(sphere\shape)
	Text 0, 20, "Pivot's Y Coordinates: " + EntityY(sphere\pivot)
	
	Flip
	
	Delay 15
	
Wend

Function move()
	
	
		
	If KeyDown(rightkey)
		
		TurnEntity sphere\pivot, 0, 5, 0
		TurnEntity sphere\shape, 0, 5, 0
			
	EndIf
		
	If KeyDown(leftkey)
	
		TurnEntity sphere\pivot, 0, -5, 0
		TurnEntity sphere\shape, 0, -5, 0
				
	EndIf	
	

	
	If KeyDown(upkey) Or KeyDown(downkey)
	
		If KeyDown(upkey)
			
			
			If sphere\inertia# < 1.1
			
				sphere\inertia# = sphere\inertia# + .1
			
			EndIf
						
			TurnEntity sphere\shape, sphere\inertia#*50, 0, 0
			MoveEntity sphere\pivot, 0, 0, sphere\inertia#
								
		EndIf
	
		If KeyDown(downkey)
	
			If sphere\inertia# > -1.1
			
				sphere\inertia# = sphere\inertia# - .1
			
			EndIf
			
			TurnEntity sphere\shape, 0, sphere\inertia#*10, 0
			MoveEntity sphere\pivot, 0, 0, sphere\inertia#
			
		EndIf

	Else
	
		If sphere\inertia# < 0
		
			sphere\inertia# = sphere\inertia# + sphere\slowdown#
			
		Else
		
			sphere\inertia# = sphere\inertia# - sphere\slowdown#
			
		EndIf
		
		If sphere\inertia# > -.06 And sphere\inertia# < .06
		
			sphere\inertia# = 0
			
		EndIf

	EndIf
	
		
	If sphere\inertia# > sphere\vzlimit#
	
		sphere\inertia# = sphere\vzlimit#
	
	ElseIf sphere\inertia# < -sphere\vzlimit#/2
	
		sphere\inertia# = -sphere\vzlimit#/2
		
	EndIf
	
	MoveEntity sphere\pivot, 0, 0, sphere\inertia#
		
	PositionEntity sphere\shape, EntityX(sphere\pivot), EntityY(sphere\pivot), EntityZ(sphere\pivot)
	
End Function



octothorpe(Posted 2006) [#30]
For future reference, use [codebox] instead of [code].

Also, try to post code that doesn't depend on external media (e.g. "blitzlogo.bmp".)

Don't indent code that is not inside a block (if, loop, function, etc.)

Constants look nicer in ALLCAPS.

Replace all the magic numbers (5, 1.1, 50, 10, .06) sprinkled throughout your code with NAMED_CONSTANTS.


octothorpe(Posted 2006) [#31]
Sorry, your code really makes no sense to me. I have no idea what you're trying to do.

The up and down keys change the mass of the sphere? What is the purpose of the pivot? What is a vz limit?

Seriously, I'd love to help but I have no idea what you're trying to do!


Buggy(Posted 2006) [#32]
Sorry about my poor programming skills. As for the [codebox], someone yelled at me when I did [codebox] on another post, so sorry.

Basically, without, the pivoting business, the code works fine. That is, except for the sphere rotating. Balls are supposed to turn when they roll, so I wanted it to be more lifelike. The problem? When I rotate it, the sphere goes up and down, because the ball is no longer facing forward - when it rolls, one frame it's facing forward, then down, then backwards, then up.

To fix this, I thought I could have a pivot. The pivot would be inside the sphere, and do the directional moving, so that it doesn't go up, down, backwards and forwards. Then the sphere would just turn and rotate, and would align itself with the pivot.

If that made sense, as I hope it did, then I desperately hope you can help me.

In any event, whether it made sense or not, thank you so much for your patience and help. I really do appreciate it.


Buggy(Posted 2006) [#33]
vzlimit# is how fast the ball can be going before it ceases to accelerate any more. Like the max speed of a car.

And if it helps, blitzlogo.bmp can be found on many of the sample codes.

Thanks a bushel!


GfK(Posted 2006) [#34]
Use TranslateEntity instead of MoveEntity. You'll need to use Sin/Cos to move the sphere but you won't have to worry about which way the sphere is actually pointing.


Buggy(Posted 2006) [#35]
Ooh! I didn't think about that! My switch to 3D was emphasized by me not having to worry about Sin/Cos, but if I must, I must! Any other ideas?


Buggy(Posted 2006) [#36]
Question: Would Sin/Cos slow down the program too much? Should I just color the ball one color and forget about spinning?


Ross C(Posted 2006) [#37]
Sin/Cos is super quick.


octothorpe(Posted 2006) [#38]
vzlimit# is how fast the ball can be going before it ceases to accelerate any more.


Oh! That explains your "inertia" field, which should be renamed "speed." "Inertia" is synonymous with "mass," and therefore an incorrect (and misleading!) name. Perhaps you were thinking of "momentum," which is mass * speed. In any case, "speed" would be the best name for that field.

Parenting your sphere to its pivot might be what you're looking for?


There were a bunch of examples of realistic-looking ball rolling posted somewhere on this site a year or so ago. One of them was rediculously simple and elegant. I'd recommend searching the forums and possibly the code archives for "rolling ball" or similar.


Buggy(Posted 2006) [#39]
Thanks. I looked for rolling ball stuff, and I found a really realistic, simple one by Jeppe Nielson, but I didn't understand it at all. I didn't understand it's main commands, so it didn't work for me.


octothorpe(Posted 2006) [#40]
I would recommend starting a new thread and asking people to explain Jeppe's code to you.


Buggy(Posted 2006) [#41]
Precisely my thought. Unfortunately, I already did it, and now not only do I feel miserable, I feel stupid because even with everyone's explanations, I still don't understand it!

The thread: http://blitzbasic.com/Community/posts.php?topic=57804

P.S. If anyone wants to reply to that thread, I'd be very appreciative.

P.P.S. How do you - in the forums - make a link that says one thing, but takes you to another page?