BitmapData - triangular transformation - actionscript-3

Hey, this is really annoying me.
I have a big code, but essentially, this part comes to transformation of a BitmapData to triangle points, using affine transformation.
Also - I know that BitmapData is rectangular - the last, fourth point of the transformation is just the first one reflected over the axis defined by the other two points.
A small thing so you can see which point is which one:
1........2
. / / / /|
./ / / /
. / / / |
./ / /
. / / |
./ /
. / |
./
3 - - - -4 - 4th point reflected
This whole process would come to scaling, rotating and skewing the BitmapData based on the angles between the points...
But somehow my code still does not work.
Looking at a similar thing in PaperVision 3D - still didn't work?
Can anybody please post a code, or a link to code for this?
Thanks.

have a look at this, might help:
triangles and uvs in flash

Related

How to do slow motion effect in side view flash game with box2d

As i am working on side view bike stunt game in which am trying to add slow motion effect when a bike performs stunts.
Is there any solution for showing such kind of slow motion effect in Box2D. can anybody help me in this regard
Thanks and regards,
Chandrasekhar
As mentioned already, changing the length of the time step can give a slow motion effect. It also has the side-effect of altering the way gravity affects bodies, and can complicate other things like recording a replay or syncing state across multiplayer games for example.
Another option is to use a fixed time step length for every time step, and keep track of the previous position and angle for all bodies. Then you can interpolate between the last frame and the current frame to draw them at an in-between frames position. This means that you are always drawing things a tiny bit behind their current position in the physics engine, but it should not be noticeable with typical frame-rates of 30-60fps.
An easy way to do this effect with or without Box2D is to step up a time modifier.
so lets say you move the player in a run function like so:
player.x += vel_x;
player.y += vel_y;
you could setup a time modifier variable that is initialized to 1
var time_mod:Number = 1;
then update all your movements as followed
player.x += vel_x * time_mod;
player.y += vel_y * time_mod;
then when you want "the slow motion effect" change your time_mod variable. For half of real time change your time_mod to 0.5. If you want to super speed change it to 2 or 3, super slow? change to 0.3
you get the idea?

html5 javascript webgl finding x and y coordinates

I wonder if you could/would help me.
I have a page (java script/ html5/webgl)
and I am displaying a set of points,
then when the user pressess on a point I would lik to find the x and y coordinates that I set in the first place.(range of 0 to 1)
what I get from the even is the x and y screen coordinates (pixals)
from even.offsetX and so.
what is the right math to get to the acsual points?
please help
In regular OpenGL, the command you need is gluUnProject:
http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml
For WebGL, you will have to roll your own since it doesn't have any fixed function pipeline support. Assuming that you are using standard transformation matrices, you can just modify the formulas in that man page. It would be hard to say anything more specific without the details of you set up your coordinate systems.

AS3 calculating ScrollPane.verticalScrollPosition

I spend almost 3 hours trying to figure this out, hope someone can help me.
I have a a set up like this:
ScrollPane > Sprite > multiple Loader one after another along y coordinate > external swf.
The setup is to allow scrolling of the loader/swf vertically.
All the objects have unit scale (no scale) except for Sprite which may have a scale factor due to zoom.
The external swf are pages from a PDF, one page per swf.
All is good so far.
Now, I have a set of next and back navigation button to step through the pages. The problem is I cannot find the correct formula to calculate the ScrollPane.verticalScrollPosition to set to.
Can you spot the mistake here? Thanks...
scroll_per_height = scrollpane.maxVerticalScrollPosition / (sprite.height - scrollpane.height);
height_per_page = sprite.height / sprite.loaders.Count
scroll_position = (page - 1) * height_per_page * scroll_per_height;
Usually I just delete my question if i find out the answer before anyone replies. But I think this will help some people at least:
The verticalScrollPosition is not the scrollbar's scroller position, instead, it's actually the y-coordinate offset of the scrollpane.content

Actionscript collisions: solving exceptions and strange cases

I have created a collision class to detect collisions using pixels. In my class I've also developed some functions for determining the collsion angle. Based on this I created some examples:
http://megaswf.com/serve/25437/
http://megaswf.com/serve/25436/
(Space to change gravity, right/left to give some speed to the ball.)
As you will probably notice, there are strange things that happen:
When the ball speed is very low
When the direction of the ball is
almost tangent to the obstacle.
collision http://img514.imageshack.us/img514/4059/colisao.png
The above image shows how I calculate the collision angle.
I call the red dots the keypoints. I search for a maximum number of keypoints (normally 4). If I find more then 2 keypoints, I choose the 2 farthest ones (as shown in one of the blue objects). Thats how I then find the normal angle to where in the surface the object collided. I don't know if this is an obsolete way of doing things.
based on that angle, I rotate the speed vector to do the bouncing.
The piece of code to do the maths is here:
static public function newSpeedVector(speedX: Number, speedY: Number, normalAngle: Number): Object{
var vector_angle: Number = Math.atan2(speedY, speedX) * (180/Math.PI);
var rotating_angle: Number = (2*(normalAngle - vector_angle) + 180) % 360;
var cos_ang: Number = Math.cos(rotating_angle/DEGREES_OF_1RAD);
var sin_ang: Number = Math.sin(rotating_angle/DEGREES_OF_1RAD);
var final_speedX: Number = speedX * cos_ang - speedY * sin_ang;
var final_speedY: Number = speedX * sin_ang + speedY * cos_ang;
return {x_speed: final_speedX, y_speed: final_speedY};
}
This is how the new speed vector is calculated...
My question is, has anyone faced this kind of problem or has some idea on how to avoid this from happening?
Without seeing your code, this is the best I can provide.
Collision physics should have some velocity threshold that is considered "stopped". That is, once the velocity gets small enough you should explicitly mark an object as stopped and exempt it from your collision code. This will help stabilize slow moving objects. Trial and error is required for a good threshold.
When a collision happens, it is important to at least attempt to correct any inter-penetration. This is most likely the reason why tangent collisions are behaving strangely. Attempt to move the colliding object away from what it hit in a reasonable manner.
Your method of determining the normal should work fine. Game physics is all about cheating (cheating in a way that still looks good). I take it rotation and friction isn't a part of what you're going for?
Try this. You have the contact normal. When you detect a collision, calculate the penetration depth and move your object along the normal so that is isn't penetrating anymore. You can use the two points you have already calculated to get one point of penetration, you'll need to calculate the other though. For circles it's easy (center point + radius in direction of normal). There are more complex ways of going about this but see if the simple method works well for you.
I have read and recommend this book: Game Physics Engine Development
I did something a little like that and got a similar problem.
What I did is that when the pixels from the bouncing object (a ball in your case) overlap with the pixels from the obstacle, you need to move the ball out of the obstacle so that they are not overlapping.
Say the ball is rolling on the obstacle, before your render the ball again you need to move it back by the amount of 'overlap'. If you know at what angle the ball hit the obstable just move it out along that angle.
You also need to add some damping otherwise the ball will never stop. Take a (very) small value, if the ball velocity is bellow that value, set the velocity to 0.
You can try one more thing, which I think is very effective.
You can make functions such as getNextX() and getNextY()(Which of course give their position coordinated after the next update) in your game objects and Check collision on objects based on their next position instead of their current position.
This way, the objects will never overlap, you'll know when they are about collide and apply your after collision physics gracefully!

From Rectangle into trapezoid

how can i transform rectangle into trapezoid with ActionScript3
my trapezoid is a floor of 3D room, and i want to texturize it (bitmap tile).
_____________
| | | |
| |_____| |
| / \ |
| / trapez.\ |
|/__________\|
It depends on how the "rectangle" is represented in your program. Few options:
If your rectangle is vector, and you have access to the anchor points:
---> o------o <---
| |
| |
o------o
Becomes:
o--o
/ \
/ \
o------o
Simply translate the two top points and condense them to each other.
If that 'rectangle' is actually a DisplayObject, you will need to 'stitch' two of the same DisplayObject to create a new one. This page has examples of what you want along with example code.
Another option would be to use Papervision3D - which is using the above rendering method as a base.
This one's a bit tricky, but it's an option. You can use displacement maps as described here. The code is AS2, but it should be fairly simple to "port".
EDIT
As par this answer, I suggest you use Papervision3D (see 3rd option) to do that, as you might want to move the camera around the "room". It will also take care of the other walls as well.
the idea is to divide it into triangles and then perform an affine transformation (using Matrix) on them ...
senocular provided sample code (for AS2) ...
there's also a handful of libs online, but I can't find any right now ...
If you're targetting FlashPlayer 10 you can do it by rotating the clip's rotation values, see This post form Mike Chambers for details.
Otherwise you're going to need a 3D engine like Papervision. There's no simple way to do non-affine transformations in Flash Player 9.
Good luck.