Roll on ..

Blitz3D Forums/Blitz3D Programming/Roll on ..

Paul "Taiphoz"(Posted 2004) [#1]
Hay, Ok here is the problem. I have an asteroid traveling along a vector, and I want to turn/rotate the roid in the direction that its moving. as if its rolling through space.

Although im working in 3D its on a 2D plane, x,z

Second problem, I am using distance collision detection between each asteroid. so its circle to circle, Im trying to get them to reflect off of each other like a snooker ball would.

Now there is no mass involved, or extra force, so All im looking for is the new angle that each roind in the collision should be traveling along.

There are some really good examples of this already in the code archives but tbh its a little to much for me, so im looking for a really basic nuts and bolts example.

thanks in advance.


Vorderman(Posted 2004) [#2]
I think there is a snooker ball example in the code archives, which is really simple - have you seen that?

re. the rolling asteroids, you could for example parent the asteroid model to a pivot, then orient and move the pivot. You are then free to roll the asteroid in any way you want without affecting its direction of motion.

Alternatively, store the pitch, yaw and roll of the asteroid in 3 variables, then on each program loop orient the object to point in the direction of motion, move it forwards (positive in the local Z axis), then orient it according to the stored pitch, yaw and roll variables. Then just increment the variables to make it spin.

This is just quickly typed into the browser and not tested, but roughly speaking -


global xa#,ya#,za#  ;angles of asteroid spin
global yaw#         ;angle of asteroid motion

;main loop
repeat

   ;orient it along dir of travel and move it forwards
   RotateEntity asteroid,0,yaw#,0
   MoveEntity asteroid,0,0,1

   ;alter the spin variables by some amounts
   xa# = xa# + 0.5
   ya# = ya# + 0.25
   za# = za# - 0.15

   ;spin it visually
   RotateEntity asteroid,xa#,ya#,za#

   ;UpdateWorld and RenderWorld etc.. down here

until done



Ross C(Posted 2004) [#3]
Or for moving it, you just turn it as normal, and use sin and cos to move it along that angle:


angle# = 35
speed# = 1.5

PositionEntity asteroid, EntityX(asteroid)+ cos(angle)*speed , EntityY(asteroid) , EntityZ(asteroid) + sin(angle) * speed

TurnEntity asteroid,0,1,0




Paul "Taiphoz"(Posted 2004) [#4]
Im moving each asteroid along a vector using x and z velocity values.

So im free to turn it and rotate it at will without affecting the direction its moving in.

So what im trying to work out is by looking at the angle which its moveing, is there a way to make it turn in that direction. I recon I could just create a lookup table with set pitch yaw and roll values for each direction, but I think there must be a better way.


Ross C(Posted 2004) [#5]
So, you wanting the asteroid to trun and face the way it's moving?


Paul "Taiphoz"(Posted 2004) [#6]
ok think of the asteroids as a ball. and I want it to roll in the direction its moving in.

So if its going from the top left towards the bottom right then I want it to look as if its rolling in that direction.


Ross C(Posted 2004) [#7]
Ah, got you :) Well, you have the angle it's moving at. What i don't get is you say:


Im moving each asteroid along a vector using x and z velocity values



Why don't you just generate an angle variable for each asteroid and use the sin and cos to move it. Then you free to the point the asteroid in the direction of the angle and turn it. Like so: ( press the 1 key to generate asetroid)

Graphics3D 800,600
SetBuffer BackBuffer()

Global cam = CreateCamera()

PositionEntity cam,0,50,0
RotateEntity cam,90,0,0

Global light = CreateLight()

Global cube = CreateCube()

Type asteroid
	Field angle#
	Field speed#
	Field rotate_speed#
	Field ent
	Field time
	Field timer
End Type


While Not KeyHit(1)


	If KeyHit(2) Then
		generate_ast()
	End If


	update_ast()
	UpdateWorld
	RenderWorld
	Flip
Wend
End

Function generate_ast()

	a.asteroid = New asteroid
	
	a\ent = CopyEntity (cube)
	
	a\angle = Rnd(0,359)
	
	a\speed = Rnd(0.1,0.3)
	
	a\rotate_speed = Rnd(0.5,4)

	a\time = 1000
	a\timer = MilliSecs()
	
	RotateEntity a\ent,0,a\angle-90,0 ; point the asteroid in the direction it will be traveling
	
End Function

Function update_ast()
	For a.asteroid = Each asteroid
		PositionEntity a\ent,EntityX(a\ent)+Cos(a\angle)*a\speed,EntityY(a\ent),EntityZ(a\ent)+Sin(a\angle)*a\speed ; move asteroid using the angle and cos/sin
		TurnEntity a\ent,a\rotate_speed,0,0
		If MilliSecs() > a\time + a\timer Then
			FreeEntity a\ent
			Delete a.asteroid
		End If
	Next
End Function



Paul "Taiphoz"(Posted 2004) [#8]
Yeah thats a good way of doing it, BUT, it requires that you face the asteroid in the direction its traveling in first.

The Roids in my game could be facing in any direction, so if I then make them face in the direction they want to go after a collision, it will be a visible change in rotation that the user will notice.

this does give me an idea though. so thanks for posting the code.